2009-02-17 79 views
11

我正在查找一個DOS腳本來刪除根目錄中除根目錄中的一組批處理文件(* .bat)以外的所有文件和子目錄。任何DOS的運動員都知道一個簡單的方法來做到這一點?如何刪除除DOS中某些文件以外的所有文件/子目錄?

更新

感謝您的幫助大家。這就是我現在的位置(見下文)。我正在使用Ken的建議刪除這些文件。我想知道如果由於鎖定文件/目錄導致delRD命令失敗,我該如何停止運行此腳本。任何人都知道嗎?現在,這個腳本會在刪除後執行一些操作,如果任何刪除操作失敗,我想停止腳本。

@echo off 

REM ********************************************************************* 
REM * Delete all files and subdirs except for batch files in the root * 
REM ********************************************************************* 

REM Delete all files in current dir except bat files. Does this by a) setting the attributes of *.bat files to 
REM readonly and hidden, b) deleting the rest, c) reseting the attributes 

attrib +r +s *.bat 
del *.* /S /Q 
attrib -r -s *.bat 

REM Deletes ALL subdirectories 

FOR /D %%G in (*) DO RD /s /q %%G 
+0

這是否意味着:不要刪除根目錄中的任何.bat文件?或者不要刪除這些.bat,如果它們在根目錄中? – colithium 2009-02-17 20:51:49

+0

不要刪除根目錄中的_any_ .bat文件 – 2009-02-17 20:54:11

回答

20

你可以設置你想保留爲只讀和隱藏第一,刪除其餘的,然後重置的屬性文件的屬性隱藏,只讀文件。

attrib +r +s *.bat 
del *.* 
attrib -r -s *.bat 

我以前做的是經常,並且寫道,自動化這個批處理文件:

@echo off 
@ if "%1" == "%9" goto help 
@ if /i %1 EQU ? goto help 
@ if /i %1 EQU help goto help 
@ attrib +h +s %1 
@ %2 %3 /Q 
@ attrib -h -s %1 
@ goto :EOF 
:help 
@echo  ╔═══════════════════════════════════════════════════════╗ 
@echo  ║ except filespec1 doscommand filespec2     ║ 
@echo  ║              ║ 
@echo  ║ filespec1 The files to exclude from doscommand  ║ 
@echo  ║ doscommmand The DOS command to execute on filespec2 ║ 
@echo  ║ filespec2 The files to execute doscommand against ║ 
@echo  ║              ║ 
@echo  ║ Example:            ║ 
@echo  ║              ║ 
@echo  ║ except *.txt del *.*         ║ 
@echo  ║              ║ 
@echo  ║Deletes all files except text files in the directory ║ 
@echo  ╚═══════════════════════════════════════════════════════╝ 

這可能確定只使用隱藏屬性,但我知道,德爾不接觸隱藏的系統文件,所以我設置了兩個。國際海事組織,比對不起更安全

基於從馬庫斯評論:如果你想擴展,以包括當前目錄的子目錄,只需兩個ATTRIB線改爲

attrib <remainder of line> /S 

和改變兩個ATTRIB線之間的線

@ %2 %3 /Q /S 

這應該適用於大多數你想要的東西except.bat要做。

2

基於固定的@ Ken的評論:

>d: 
>mkdir bats 
>c: 
>copy *.bat d:\bats 
>del *.*/Y 
>copy d:\bats\*.bat c:\ 
+0

您仍然將DOS攻擊腳本保留在您的系統上;-) – dirkgently 2009-02-17 20:52:54

0

這是基於Alex的備份.BAT文件的方法,但也使用RD命令刪除所有子文件夾。

@echo off 

rem !WARNING! 
rem THE_DELETE_DRIVE is the drive to delete 
rem THE_BACKUP_DRIVE is the drive to use for backup 
set THE_DELETE_DRIVE=T: 
set THE_BACKUP_DRIVE=C: 
rem !WARNING! 

echo This will recursively delete everything from %THE_DELETE_DRIVE%\ (except batch files). 
echo Are you sure? Press Ctrl+C to cancel, or any other key to continue... 
echo. 
pause 

rem Make the backup folder 
md %THE_BACKUP_DRIVE%\bak12345 

rem Copy all batch files from delete-drive root to backup folder 
copy %THE_DELETE_DRIVE%\*.bat %THE_BACKUP_DRIVE%\bak12345 

rem Delete everything in the delete-drive root 
rd /s/q %THE_DELETE_DRIVE%\ 

rem Copy all backed-up files back to delete-drive root 
copy %THE_BACKUP_DRIVE%\bak12345\*.bat %THE_DELETE_DRIVE%\ 

rem Remove the backup folder 
rd /s/q %THE_BACKUP_DRIVE%\bak12345 

echo ************************************ 
echo All Done! 
echo ************************************ 
echo. 

pause 
相關問題