Alex Rivera | Logout

Command line to recursively delete files but excluding a certain file

Asked 2009-02-11T09:36:36.327
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.

Edit
Report

1 Answer

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.

answered 2010-02-21T18:39:22.720

Your Answer