Klasörleri kaldırmak için toplu iş dosyası


0

Eric G tarafından bir süre önce bununla ilgili bir yazı vardı, bu da şöyle oldu:

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

Bunu biraz değiştirdim:

@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

Ne yazık ki, çalışmıyor ve aşağıdaki çıktıyı alıyorum:

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

Kodda yanlış olan ne?


İlk bakışta komut satırı argümanı her iki partide de yanlış olarak belirtilmiş. Bu olmak zorunda "%~1"değil "%1%"tilde cmd hat args tek yüzde işareti lider olan, normal vars gelen mevcut dış çift tırnak mümkün ve farklı kaldırır.
LotPings,

Partinizle ve kaynak partinizle ilgili sorun, dir adlarını boşluklu almanız ve alıntı yapmamanızdır. Cevabımı gör.
LotPings

Yanıtlar:


0

Partinizle ve kaynak partinizle ilgili sorun, dir adlarını boşluklu almanız ve alıntı yapmamanızdır.
Yorumların bazıları kaldırıldı.

@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

For / d yalnızca varolan dizinleri döndürürse, EDIT gereksiz olanı kaldırır.

Sitemizi kullandığınızda şunları okuyup anladığınızı kabul etmiş olursunuz: Çerez Politikası ve Gizlilik Politikası.
Licensed under cc by-sa 3.0 with attribution required.