Arquivo em lote para remover pastas

0

Houve um post de Eric G há algum tempo sobre isso, que foi assim:

@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%
)

Alterei ligeiramente para isto:

@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

Infelizmente, isso não funciona e recebo a seguinte saída:

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...

O que há de errado com o código?

    
por Community 21.12.2016 / 20:27

1 resposta

0

O problema com o lote e o lote de origem também é que você não obtém nomes de pastas com espaços e não os cita.
Removemos alguns dos comentários.

@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 removeu o supérfluo se, o for / d retornará somente os diretórios existentes.

    
por 21.12.2016 / 21:14

Tags