除了DOS中的某些文件外,如何删除所有文件/子目录?

时间:2009-02-17 20:48:07

标签: dos

我正在寻找一个DOS脚本来删除根目录中的所有文件和子目录,除了根目录中的一组批处理文件(* .bat)。有没有DOS的人知道一个简单的方法呢?

更新

感谢大家的帮助。这就是我现在所处的位置(见下文)。我正在使用Ken的建议来删除文件。我想知道如果delRD命令因文件/目录锁定而失败,我怎么能停止运行此脚本。谁知道怎么样?现在,这个脚本将在删除后执行一些操作,如果任何删除操作失败,我想停止脚本。

@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

3 个答案:

答案 0 :(得分:21)

你可以先将你想要保存的文件的属性设置为只读和隐藏,删除其余文件,然后重新设置隐藏的只读文件的属性。

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不会触及隐藏的系统文件,所以我设置了两个。比起对不起,IMO更安全。

根据Marcus的评论:如果你想扩展它以包含当前目录的子目录,只需将两个attrib行改为

attrib <remainder of line>  /S

并将两个属性行之间的行更改为

@ %2 %3 /Q /S

这应该适用于除了.bat之外你想要的大多数事情。

答案 1 :(得分:2)

根据@ Ken的评论修正:

>d:
>mkdir bats
>c:
>copy *.bat d:\bats
>del *.* / Y
>copy d:\bats\*.bat c:\

答案 2 :(得分:0)

这是基于Alex的备份.BAT文件的方法,但也删除了所有子文件夹,使用RD命令。

@echo off

rem !WARNING!
rem THE_DELETE_DRIVE is the drive to delete
rem THE_BACKUP_DRIVE is the drive to use for backup
set THE_DELETE_DRIVE=T:
set THE_BACKUP_DRIVE=C:
rem !WARNING!

echo This will recursively delete everything from %THE_DELETE_DRIVE%\ (except batch files).
echo Are you sure? Press Ctrl+C to cancel, or any other key to continue...
echo.
pause

rem Make the backup folder
md %THE_BACKUP_DRIVE%\bak12345

rem Copy all batch files from delete-drive root to backup folder
copy %THE_DELETE_DRIVE%\*.bat %THE_BACKUP_DRIVE%\bak12345

rem Delete everything in the delete-drive root
rd /s/q %THE_DELETE_DRIVE%\

rem Copy all backed-up files back to delete-drive root
copy %THE_BACKUP_DRIVE%\bak12345\*.bat %THE_DELETE_DRIVE%\

rem Remove the backup folder
rd /s/q %THE_BACKUP_DRIVE%\bak12345

echo ************************************
echo All Done!
echo ************************************
echo.

pause