批处理文件,用于删除目录中除系统文件以外的所有文件

时间:2017-10-13 17:01:46

标签: batch-file cmd rmdir

我试图编写批处理文件,删除DELETE目录中的所有文件和目录,除了少数" important"那些。我曾尝试将重要文件/目录中的Hidden和System属性添加到其中。虽然它似乎适用于" del"命令," rmdir"无论属性如何,仍会删除所有内容。

@echo on

attrib  important.txt +s +h
attrib  folder +s +h
attrib  picture.bmp +s +h
attrib  delete.bat +s +h


del *.* 
rmdir /s /q C:\Users\Lenovo\Desktop\DELETE

attrib  important.txt -s -h
attrib  folder -s -h
attrib  picture.bmp -s -h
attrib  delete.bat -s -h

pause

有什么办法吗?

1 个答案:

答案 0 :(得分:0)

以下是在PowerShell中执行此操作的方法。这可以放在.ps1脚本文件中,并从cmd.exe命令shell运行。

将文件的只读属性设置为不删除。

(Get-Item .\important.txt).Attributes+="ReadOnly"

使用以下命令删除文件。如果您确信将删除正确的文件,请从命令末尾删除-WhatIf

Remove-Item -Path * -Recurse -ErrorAction SilentlyContinue -WhatIf