Command line to recursively delete files but excluding a certain file
Asked Answered
P

7

10

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.

Palmar answered 11/2, 2009 at 9:36 Comment(0)
E
8

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.
Elwina answered 11/2, 2009 at 10:4 Comment(2)
I don't yet understand the > NUL bit, but I like the idea of setting the attributes. Cheers!Palmar
interesting! notice that rd /s your_directory also erases system and hidden filesHeterocyclic
T
4

Just do this, easy

  1. windows button + r
  2. type cmd and hit enter
  3. Navigate to parent directory:
    type c: or d: (or letter of the drive you want to navigate to)
  4. type dir to see a list of that directory's contents ( dir /ah to see hidden files )
  5. then to change directory, type cd xxxx ( xxxx = directory name )
  6. Repeat 4&5 until you get to the directory where you want to run the batch delete
  7. then type your pattern. Something like: del /S /ah *.jpg and hit enter. It will run through all sub-directories, and remove all visible and hidden files that are .jpg files

*   is a wildcard
/S   deletes from all subfolders
/ah   a = select files based on attribute, h = hidden

Example: to delete those annoying .DS_Store files that appear when you copy from Mac to Windows, run:

del /S /ah .DS_Store

or

del /S /ah ._*

which will get all the 'duplicate' hidden files that are also created when copying from Mac to Windows.

Towroy answered 4/3, 2012 at 10:9 Comment(0)
G
3

You can easily loop a set of files and perform a command on each one, like this:

set match=D:\blah\M*.zip
for %%x in (%match%) do (
del %%x
)

Then I think you need to read this article on how to manipulate strings in DOS: http://www.dostips.com/DtTipsStringManipulation.php

Gallicize answered 11/2, 2009 at 9:50 Comment(0)
V
2

You can simply use below:

forfiles /p C:\temp-new /s /c "cmd /c if @isdir==FALSE del @file"

TechNet Referenceenter link description here

Viewpoint answered 11/11, 2011 at 16:40 Comment(0)
A
1

Perhaps the 'forfiles' command could be of use

http://technet.microsoft.com/en-us/library/cc753551.aspx

Hope that helps.

Antiphonary answered 11/2, 2009 at 9:40 Comment(0)
G
1

This script will delete all .zip files from a folder (and subfolders), BUT ONLY IF the file name does not contain the word "backup", or "Backup" or "BACKUP", etc.

# Script DeleteZip.txt
var str folder, filelist, file
cd $folder
lf -r -n "*.zip" > $filelist
while ($filelist <> "")
do
    lex "1" $filelist > $file
    # Does this $file contain the word "backup" ? Do case independent search (-c option).
    if ( { sen -c "^backup^" $file } <= 0 )
        system del ("\""+$file+"\"")
    endif
done

Script is in biterscripting ( http://www.biterscripting.com ). Save the script in file C:/Scripts/DeleteZip.txt. Run the script as

script "C:/Scripts/DeleteZip.txt" folder("C:/testfolder")

Will do the delete operation on folder C:/testfolder and all its subfolders, at all levels.

Garpike answered 21/2, 2010 at 18:39 Comment(0)
A
0

A simple way that you need:

FORFILES /P "DIRECTORY" /S /M SEARCHMASK /C "cmd /c if @file neq \"FILE_TO_EXCLUDE\" del /f @file"

You still can adapt the command to your need to exclude specific files and/or by date. More details in the 'forfiles' documentation below.

https://learn.microsoft.com/pt-br/windows-server/administration/windows-commands/forfiles

Apple answered 10/7, 2021 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.