删除文件夹的批处理文件

删除文件夹的批处理文件

有一篇帖子来自埃里克·G很久以前就讨论过这个问题,情况如下:

@echo off 
REM set the name of the directory you would like to target for deleting
set dirname=SAMPLE

REM set the following to "true" if you want to select any directory that includes the name, e.g., a wildcard match
set usewildcard=true


REM --DO NOT EDIT BELOW THIS LINE---------------

REM sentinel value for loop
set found=false

REM If true surround in wildcards
if %usewildcard% == true (
    set dirname=*%dirname%*
) 

REM use current working directory or take the directory path provided as the first command line parameter
REM NOTE: do not add a trailing backslash, that is added in the for loop, so use "C:" not "C:\"
set directorytosearch=%cd%
if NOT "%1%" == "" (
    set directorytosearch=%1%
)
echo Searching for %dirname% in %directorytosearch%


REM /r for files
REM /d for directories
REM /r /d for files and directories
for /d %%i in (%directorytosearch%\%dirname%) do (
    IF EXIST %%i (
        REM change the sentinel value
        set found=true

        echo Deleting the folder %%i
        REM Delete a folder, even if not empty, and don't prompt for confirmation
        rmdir /s /q %%i
    )
)

REM logic to do if no files were found
if NOT "%found%" == "true" (
    echo No directories were found with the name of %dirname%
)

我稍微把它改成了这样:

@echo off 

REM set the name of the directory you would like to target for deleting
set dirname=QBBackupTemp

REM set the following to "true" if you want to select any directory that includes the name, e.g., a wildcard match
set usewildcard=true


REM --DO NOT EDIT BELOW THIS LINE---------------

REM sentinel value for loop
set found=false

REM If true surround in wildcards
if %usewildcard% == true (
    set dirname=*%dirname%*
) 

REM echo Folder to delete is %dirname%

REM use current working directory or take the directory path provided as the first command line parameter
REM NOTE: do not add a trailing backslash, that is added in the for loop, so use "C:" not "C:\"
REM set directorytosearch=%cd%
REM if NOT "%1%" == "" (
    REM set directorytosearch=%1%
    set directorytosearch=D:\Quickbooks

pause

echo %directorytosearch%

REM )
echo Searching for %dirname% in %directorytosearch%

pause

REM /r for files
REM /d for directories
REM /r /d for files and directories
for /d %%i in (%directorytosearch%\%dirname%) do (
    IF EXIST %%i (
        REM change the sentinel value
        set found=true

        echo Deleting the folder %%i

        pause

        REM Delete a folder, even if not empty, and don't prompt for confirmation
        rmdir /s /q %%i
    )
)

REM logic to do if no files were found
if NOT "%found%" == "true" (
    echo No directories were found with the name of %dirname%
)

pause

不幸的是,它不起作用,我得到以下输出:

Press any key to continue...
D:Quickbooks
Searching for *QBBackupTemp* in D:\Quickbooks
Press any key to continue...
Deleting the folder D:\Quickbooks\QBBackupTemp Mon, Dec 19 2016 10 01 24 PM
Press any key to continue...
The system cannot find the file specified.
(That line is repeated another 8 times)
Deleting the folder D:\Quickbooks\QBBackupTemp Mon, Dec 19 2016 10 05 32 PM
Press any key to continue...
The system cannot find the file specified.
(That line is repeated another 8 times)
Press any key to continue...

代码有什么问题?

答案1

您的批处理和源批处理的问题在于,您获取了带空格的目录名称,但您没有引用它们。
删除了一些注释。

@Echo off
set dirname=QBBackupTemp
set usewildcard=true
set found=false
if %usewildcard% == true set dirname=*%dirname%*
set directorytosearch=D:\Quickbooks
echo Searching for %dirname% in %directorytosearch%
pause

for /d %%i in (%directorytosearch%\%dirname%) do (
        set found=true
        echo Deleting the folder "%%i"

        pause
        REM Delete a folder, even if not empty, and don't prompt for confirmation
        rmdir /s /q "%%i"
)
REM logic to do if no files were found
if NOT "%found%" == "true" (
    echo No directories were found with the name of %dirname%
)
pause

编辑删除多余的if,for /d只会返回现有的目录。

相关内容