我正在寻找一个DOS脚本来删除根目录中的所有文件和子目录,但根目录中的一组批处理文件(* .bat)除外.那里的任何一个DOS玩家知道一个简单的方法呢?
更新
谢谢大家的帮助.这是我现在所在的地方(见下文).我使用Ken的建议删除文件.如果del或RD命令由于文件/目录上的锁定而失败,我想知道如何停止此脚本运行.有人知道吗现在,这个脚本在删除后会做一些事情,如果任何删除失败,我想停止脚本.
@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
最佳答案
YOu可以首先设置要保留的文件的属性,删除其余的文件,然后重新设置隐藏的,只读文件的属性.
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 ╚═══════════════════════════════════════════════════════╝
可能只是使用隐藏的属性,但我知道del不会隐藏系统文件,所以我设置两者.更安全比对不起,海事组织.
基于Marcus的评论:如果你想扩展它包括当前目录的子目录,只需将两个属性行更改为
attrib <remainder of line> /S
并改变两个属性行之间的线
@ %2 %3 /Q /S
这应该适用于你想要的大部分事情,除了.bat要做.
相关文章
转载注明原文:如何删除除DOS中的一些文件以外的所有文件/子目录? - 代码日志