下面是評論描述腳本的每一部分該另一種解決方案:
@Echo OFF
REM Important that Delayed Expansion is Enabled
setlocal enabledelayedexpansion
REM This sets what folder the batch is looking for and the root in which it starts the search:
set /p foldername=Please enter the foldername you want to delete:
set /p root=Please enter the root directory (ex: C:\TestFolder)
REM Checks each directory in the given root
FOR /R %root% %%A IN (.) DO (
if '%%A'=='' goto end
REM Correctly parses info for executing the loop and RM functions
set dir="%%A"
set dir=!dir:.=!
set directory=%%A
set directory=!directory::=!
set directory=!directory:\=;!
REM Checks each directory
for /f "tokens=* delims=;" %%P in ("!directory!") do call :loop %%P
)
REM After each directory is checked the batch will allow you to see folders deleted.
:end
pause
endlocal
exit
REM This loop checks each folder inside the directory for the specified folder name. This allows you to check multiple nested directories.
:loop
if '%1'=='' goto endloop
if '%1'=='%foldername%' (
rd /S /Q !dir!
echo !dir! was deleted.
)
SHIFT
goto :loop
:endloop
您可以從初始變量的前採取/p
出和=
後,只需輸入自己的價值觀,如果你不「不想被提示:
set foldername=
set root=
您還可以刪除在環部echo
和pause
在端部的一批默默運行。
它可能稍微複雜一點,但代碼可以應用於很多其他用途。
我測試了它在C:\Test
尋找同樣的文件夾名稱qwerty
的多個實例:
C:\Test\qwerty
C:\Test\qwerty\subfolder
C:\Test\test\qwerty
C:\Test\test\test\qwerty
和所有被留下是:
C:\Test\
C:\Test\test\
C:\Test\test\test\
你能解釋的究竟是什麼它做了一點?什麼是fileprefix?和那些標誌..現在嘗試它。 – Achshar
希望這個鏈接可以幫助你。 http://www.robvanderwoude.com/batchcommands.php – StarPinkER
@Achshar:基本上,把Y文件夾的名稱,而不是'fileprefix'('... IN(Y *)DO ...')。另外,把X文件夾的路徑放在'/ R'和'%% X'之間(Jermaine錯過了'/ R'開關需要一個參數,這是一個根文件夾來遍歷的事實)。還有一件事,你還需要添加一個條件來確保你正在刪除一個'Y'文件夾,而不是'Ysomething',它也會匹配'Y *'掩碼。像這樣的東西可能會做:'...如果「%%〜nxX」==「Y」RD ...'。 –