【问题标题】:How can I delete all files/subdirs except for some files in DOS?如何删除除 DOS 中的某些文件之外的所有文件/子目录?
【发布时间】:2010-10-08 04:51:53
【问题描述】:

我正在寻找一个 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

【问题讨论】:

  • 这是否意味着:不要删除根目录中的任何 .bat 文件?或者如果它们在根目录中,不要删除这些 .bat?
  • 不要删除根目录下的任何个.bat文件

标签: dos


【解决方案1】:

根据@Ken 的评论修复:

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

【讨论】:

  • 您仍然在系统上保留 dos-attack 脚本 ;-)
  • 哎呀,不,你没有。但是/Y 是做什么的呢?
  • 对问题“你确定吗?”的答案是 Y
  • cd d: 和 cd c: 不起作用。只需 D: 或 C: 即可更改驱动器。此外,您可以使用 cd /d drive:\directory 语法对 CMD.EXE 进行这两种操作。
  • 我还需要删除所有子目录。我相信 del . 只会删除当前目录中的文件。
【解决方案2】:

您可以先将要保留的文件的属性设置为只读和隐藏,删除其余文件,然后将隐藏的只读文件的属性重置回来。

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        ╚═══════════════════════════════════════════════════════╝

只使用 hidden 属性可能还可以,但我知道 del 不会触及隐藏的系统文件,所以我都设置了。安全总比抱歉好,IMO。

基于 Marcus 的评论:如果您想扩展它以包含当前目录的子目录,只需将两个属性行都更改为

attrib <remainder of line>  /S

并将两条属性线之间的线更改为

@ %2 %3 /Q /S

这应该适用于您希望 except.bat 做的大多数事情。

【讨论】:

  • 这是个好主意。我还需要删除所有子目录(及其内容)。似乎此脚本仅删除根目录中的非 *.bat 文件。
  • Marcus,我已经更新了帖子以显示包含子目录。
【解决方案3】:

这是基于 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

【讨论】:

    猜你喜欢
    • 2011-05-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-20
    • 2013-07-31
    相关资源
    最近更新 更多