Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Command line to recursively delete files but excluding a certain file

I need to delete files of a certain type (.zip files, say) from a folder, and all of its sub-folders, using the command line. Ideally I am looking for something that can be run as a .bat file in Windows.

I know there is a /S switch for the DEL command to look in sub-folders, but to add to the challenge I need to exclude files of a certain name ("Backup.zip" as an example).

Is there a way to delete files recursively but exclude files of a certain name. It will not be practical in my situation to explicitly list all the file names I want to delete, only the files of the matching type I don't want to delete.

like image 666
Tim C Avatar asked Feb 11 '09 09:02

Tim C


People also ask

How do I delete all files except a specific file extension?

Microsoft WindowsOpen File Explorer. Browse to the folder containing the files. Click the Type column heading to sort all files by the type of files. Highlight all the files you want to keep by clicking the first file type, hold down the Shift key, and click the last file.

How do you remove all files except some?

Using Extended Globbing and Pattern Matching Operators Also, with the ! operator, we can exclude all files we don't want glob to match during deletion. Let's look at the list of pattern matching operators: ?(pattern-list) matches at least zero and at most one occurrence.

How do I delete all files from a certain name?

To delete matching files: enter *_bad. jpg in the search box, select the results and press Delete or Del.


1 Answers

A nice trick: make the files you want to exclude read-only!

DEL /S will not delete read-only file.

The following script does not do exactly what you want (see my remarks below) but shows you how read-only files can be used to avoid deletion.

@ECHO OFF
:: This example supposes your first parameter is
:: the relative path to the folder to be deleted
:: and the second is the relative path (from the
:: the target folder) of the one to be excluded
:: Notice that this will only work if the folders
:: are in the working drive, if not, you ll
:: have to specify full paths

IF "%1"=="" GOTO ERROR
IF "%2"=="" GOTO ERROR
IF NOT EXIST %1\NUL GOTO ERROR
CD %1
IF NOT EXIST %2\NUL GOTO ERROR
ECHO Starting up the deletion process
ECHO. * Setting attributes
attrib %1\*.mp3 -r -s -h > NUL
attrib %2\*.mp3 +r > NUL
ECHO. * Deleting files
del /s %1\*.mp3
ECHO. * Reseting attributes
attrib %2\*.mp3 -r > NUL
ECHO.
ECHO Operation completed!
ECHO.
GOTO END

:ERROR
ECHO Parameters:
ECHO. Param1 -> target folder
ECHO. Param2 -> folder to be ignored
ECHO.
GOTO END

:END

Note: you can adapt this script in order to ignore not just a sub-folder but all files of given type:

attrib /S *.xxx +r > NUL

will in effect help you to exclude all 'xxx' files of the current directory and all sub-directories (hence the /S option).


Note: the "> NUL" part is a redirection often used to hide standard output, instead of displaying it on screen.
It can be dangerous if used too often (in a large loop with different paths involved, for instance) since it is a device, and like all devices (AUX, COMn, LPTn, NUL and PRN):

  • opening a device will claim one file handle. However, unlike files, devices will never be closed until reboot.
  • each device exists in every directory on every drive, so if you used redirection to NUL in, say, C:\ and after that you use it again in C:\TEMP, you'll lose another file handle.
like image 135
VonC Avatar answered Oct 31 '22 08:10

VonC