Некоторое время назад Эрик Джи написал об этом сообщение, которое звучало так:

@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 ответ1

0

Проблема с вашим пакетом, а также с исходным пакетом в том, что вы получаете имена dir с пробелами, а вы их не заключаете в кавычки.
Удалены некоторые комментарии.

@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

EDIT убрал лишнее, если for /d вернет только существующие dir.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками .