批处理文件删除文件夹

问题描述:

我发现了一些代码来删除文件夹,在这种情况下,删除所有'''文件夹。 我创建了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

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

有两个工具,我能想到的检查目录权限。在目录中使用

attrib -r /s <directory_name> 

要在目录

cacls /? 

For /F线是关键的授予权限查看帮助

为只读删除。它将行的部分结果放在单引号中,并对其进行迭代。

所以它需要的文件的目录列表中的目标目录中,排除基础上,_NumtoKeep变量第一ñ线,并给出结果。

然后它在做这项工作之前,询问Set _rdflag= /q这一行,确认行动。