如何删除DOS中除某些文件以外的所有文件/子目录? [英] How can I delete all files/subdirs except for some files in DOS?

查看:482
本文介绍了如何删除DOS中除某些文件以外的所有文件/子目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个DOS脚本来删除根目录中的所有文件和子目录,但根目录中的一组批处理文件(* .bat)除外。任何DOS都知道有什么简单的方法可以做到这一点?

I'm looking for a DOS script to delete all files and subdirectories in a root directory except for a set of batch files (*.bat) that are in the root directory. Any DOS jocks out there that know an easy way to do this?

更新

谢谢大家的帮助。这就是我现在的位置(见下文)。我使用Ken的建议删除文件。我想知道如果 del RD 命令由于锁定而失败,如何停止此脚本的运行文件/目录。有人知道吗?现在,此脚本将在删除后执行很多操作,如果任何删除失败,我想停止脚本。

Thanks for your help everyone. This is where I'm at now (see below). I'm using Ken's suggestion for the delete of the files. I would like to know how I can stop this script running if the del or RD commands fail due to a lock on a file/dir. Anyone know how? Right now, this script will do a bunch of things after the deletes and I'd like to stop the script if any of the deletes fail.

@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 can set the attributes of the files you want to keep to readonly and hidden first, delete the rest, and then reset the attributes of the hidden, readonly files back.

attrib +r +s *.bat
del *.*
attrib -r -s *.bat

我过去经常这样做,并且编写了一个批处理文件来自动执行此操作:

I used to do that quite often, and wrote a batch file that automated this:

@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更安全。

It's probably OK just to use the hidden attribute, but I know that del doesn't touch hidden system files, so I set both. Better safe than sorry, IMO.

基于Marcus的评论:如果要扩展此范围以包括当前目录的子目录,只需将两个attrib行都更改为

Based on a comment from Marcus: If you want to extend this to include subdirectories of the current directory, simply change both attrib lines to

attrib <remainder of line>  /S

并将两个attrib行之间的线更改为

and change the line between the two attrib lines to

@ %2 %3 /Q /S

这应该适用于您除了bat想要执行的大多数操作。

That should work for most things you'd want except.bat to do.

这篇关于如何删除DOS中除某些文件以外的所有文件/子目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆