How can I delete all files/subdirs except for some

2020-02-01 01:40发布

问题:

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?

Update

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

回答1:

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

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.

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

and change the line between the two attrib lines to

@ %2 %3 /Q /S

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



回答2:

Fixed based on @Ken's comment:

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


回答3:

This is based on Alex's method of backing up the .BAT files, but deletes all subfolders too, using the RD command.

@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


标签: dos