我發現了一些代碼來刪除文件夾,在這種情況下,刪除所有'''文件夾。 我創建了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{"
你的格式很混亂。嘗試編輯問題來修復它。 – 2009-11-20 21:50:54
@Echo ON真正看到它在做什麼。 – 2009-11-20 21:57:53