2012-05-01 33 views
8

我有一個名爲x的文件夾,其中包含多個子文件夾和文件。我想刪除x中存在的名爲y的文件夾及其所有子文件夾。所述要被刪除的文件夾可能包含或不包含任何文件。我相信我可以使用cmd或某種批處理文件來做到這一點,但我是命令行新版本,可以真正使用一些幫助。如何使用cmd /批處理文件在目錄中刪除名稱x的所有文件夾

一個簡單的事情就是rd文件夾的名稱,它的工作原理,但我相信有比單獨刪除每個文件夾更好的方法..就像通過所有文件夾一些循環。

感謝

編輯:只是爲了澄清,我有X的內部Y(需要被刪除的文件夾),它可以在任何X的子文件夾,並在任意級別的深度。此外,我正在查看答案,可能需要一段時間才能接受任何答案。請多多包涵:)

回答

7

下面是評論描述腳本的每一部分該另一種解決方案:

@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= 

您還可以刪除在環部echopause在端部的一批默默運行。

它可能稍微複雜一點,但代碼可以應用於很多其他用途。

我測試了它在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\ 
4
FOR /D /R %%X IN (fileprefix*) DO RD /S /Q "%%X" 

採取護理使用的...

爲RD命令:

/S  Removes all directories and files in the specified directory 
     in addition to the directory itself. Used to remove a directory 
     tree. 
/Q  Quiet mode, do not ask if ok to remove a directory tree with /S 

FOR命令通過文件或變量列表用於循環,這些選項很容易記憶,目錄只能遞歸。

+0

你能解釋的究竟是什麼它做了一點?什麼是fileprefix?和那些標誌..現在嘗試它。 – Achshar

+0

希望這個鏈接可以幫助你。 http://www.robvanderwoude.com/batchcommands.php – StarPinkER

+2

@Achshar:基本上,把Y文件夾的名稱,而不是'fileprefix'('... IN(Y *)DO ...')。另外,把X文件夾的路徑放在'/ R'和'%% X'之間(Jermaine錯過了'/ R'開關需要一個參數,這是一個根文件夾來遍歷的事實)。還有一件事,你還需要添加一個條件來確保你正在刪除一個'Y'文件夾,而不是'Ysomething',它也會匹配'Y *'掩碼。像這樣的東西可能會做:'...如果「%%〜nxX」==「Y」RD ...'。 –

1

這種類型的題目的共同的問題是,如果在多個級別上存在目標文件夾的實例,則大多數方法都會導致錯誤,因爲當高級別文件夾被刪除時,它下面的所有文件夾都會消失。例如:

C:\X\Y\subfolder 
C:\X\Y\subfolder\one\Y 
C:\X\Y\subfolder\two\Y 
C:\X\Y\subfolder\three\Y 
C:\X\test 
C:\X\test\test 

先前的例子生成一個名爲Y 4個文件夾列表將被刪除,但在第一個之後被刪除剩下的三個名字不復存在,導致錯誤消息,當他們試圖刪除。我知道這是你的情況的可能性。

要解決此問題,必須從自底向上的訂單中刪除文件夾,即必須先刪除最內層的文件夾,並且必須先刪除最上層的文件夾。實現這一目標的方法是通過遞歸子程序

@echo off 
rem Enter into top level folder, process it and go back to original folder 
pushd x 
call :processFolder 
popd 
goto :EOF 

:processFolder 
rem For each folder in this level 
for /D %%a in (*) do (
    rem Enter into it, process it and go back to original 
    cd %%a 
    call :processFolder 
    cd .. 
    rem If is the target folder, delete it 
    if /I "%%a" == "y" (
     rd /S /Q "%%a" 
    ) 
) 
exit /B 

雖然在這種特殊情況下由其他方法的問題,只是多個錯誤信息,還有其他的情況下,當這種處理順序是根本。

0

製作蝙蝠有以下幾點:

del /q "C:\Users\XXX\AppData\Local\Temp\*" 

FOR /D %%p IN ("C:\Users\XXX\AppData\Local\Temp\*.*") DO rmdir "%%p" /s /q 
相關問題