2009-11-20 277 views
3

我發現了一些代碼來刪除文件夾,在這種情況下,刪除所有'''文件夾。 我創建了10個測試文件夾,加上1個已經存在的文件夾。我想刪除除了4之外的所有文件。 代碼起作用,它會保留我的測試文件夾中的4個,除了它也會保留其他文件夾。是否有批處理文件中的其他文件夾的某些屬性會阻止它被刪除?它是在幾個星期前通過工作創建的。批處理文件刪除文件夾

這裏是我偷的代碼(但並不真正瞭解細節):對於

rem DOS - Delete Folders if # folders > n 
@Echo Off 
:: User Variables 
:: Set this to the number of folders you want to keep 
Set _NumtoKeep=4 
:: Set this to the folder that contains the folders to check and delete 
Set _Path=C:\MyFolder_Temp\FolderTest 
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{" 
PushD %_Path% 
Set _s=%_NumtoKeep% 
If %_NumtoKeep%==1 set _s=single 
    For /F "tokens=* skip=%_NumtoKeep%" %%I In ('dir "%_Path%" /AD /B /O-D /TW') Do (
    If Exist "%temp%\tf}1{" (
     Echo %%I:%%~fI >>"%temp%\tf}1{" 
    ) Else (
     Echo.>"%temp%\tf}1{" 
     Echo Do you wish to delete the following folders?>>"%temp%\tf}1{" 
     Echo Date  Name>>"%temp%\tf}1{" 
     Echo %%I:%%~fI >>"%temp%\tf}1{" 
)) 
PopD 
If Not Exist "%temp%\tf}1{" Echo No Folders Found to delete & Goto _Done 
Type "%temp%\tf}1{" | More 
Set _rdflag= /q 
Goto _Removeold 
Set _rdflag= 
:_Removeold 
For /F "tokens=1* skip=3 Delims=:" %%I In ('type "%temp%\tf}1{"') Do (
If "%_rdflag%"=="" Echo Deleting 
rd /s%_rdflag% "%%J") 
:_Done 
If Exist "%temp%\tf}1{" Del "%temp%\tf}1{" 
+0

你的格式很混亂。嘗試編輯問題來修復它。 – 2009-11-20 21:50:54

+1

@Echo ON真正看到它在做什麼。 – 2009-11-20 21:57:53

回答

0

使用RD C:\BLAH /S /Q命令的Windows

0

有兩個工具,我能想到的檢查目錄權限。在目錄中使用

attrib -r /s <directory_name> 

要在目錄

cacls /? 
1

For /F線是關鍵的授予權限查看幫助

爲只讀刪除。它將行的部分結果放在單引號中,並對其進行迭代。

所以它需要的文件的目錄列表中的目標目錄中,排除基礎上,_NumtoKeep變量第一ñ線,並給出結果。

然後它在做這項工作之前,詢問Set _rdflag= /q這一行,確認行動。

相關問題