RoboDelete - Robust file delete for Windows

Download v1.29 2011-03-10
View source
About
Usage
Examples
Table of Parameters
Community comments and questions

RoboDelete started as a counterpart for robocopy. It provides conditional delete, move and list of files.

With MOVE options, it can be used as "RoboMove". Robocopy provides similar capabilities, but there are some extended features not available when using robocopy. For example, it is possible to leave a "file moved" notification to the original file location. Administrator of a file server can evacuate large video files from home directories and leave a small text file describing a policy violation or the new location of the files.

With combination of SIMULATE and LOG options, it can be used as "RoboList". You will get a listing of files, but nothing is really deleted.

License: Released 2012-02-08 to public domain - no restrictions.

Mandatory disclaimer:

Provided as-is. That said, the script has been in use for years in many environments and thus quite tested. If you do find a bug, please let me know using the form on the bottom of this page.

Use /SIMULATE before production run - you'll see what will happen before it actually happens.
Use /SAFEGUARD if files are going to be deleted based on their age.

For usage information on prompt run robodelete.wsf /?

Usage: robodelete.wsf /F:value /PATH:value [/Y] [/MINAGE:value] [/MAXAGE:value] [/MINDATE:value] [/MAXDATE:value] [/SAFEGUARD:value] [/COMP:value] [/INTERVAL:value] [/R] [/PATHFILTER:value] [/MINDEPTH:value] [/MAXDEPTH:value] [/Q] [/LOG:value] [/LOGSTAMP] [/MINLOG] [/REPLACELOG] [/FOLDERS] [/DISFLDAGE] [/SIMULATE] [/ATTREQ:value] [/ATTSKIP:value] [/MINSIZE:value] [/MAXSIZE:value] [/REGEXP] [/MOVE:value] [/MOVETARGET:value] [/NOTEFILE:value]

Some examples
1. Delete any file, age older than or equal to four days, from C:\TEMP:
robodelete.wsf /F:* /MINAGE:4 /PATH:"c:\temp"

2. Delete any atleast 4 months old file from current directory:
robodelete.wsf /F:* /MINAGE:4 /INTERVAL:m /PATH:.

3. Delete atleast 4 days old files of any name, recurse+delete subfolders.
robodelete.wsf /F:* /MINAGE:4 /PATH:"c:\temp" /R /FOLDERS

4. Like #3, but the /MINAGE is ignored for folders
robodelete.wsf /F:* /MINAGE:4 /PATH:"c:\temp" /R /FOLDERS /DISFLDAGE

5. Delete C:\TEMP\*.txt, with logging:
robodelete.wsf /F:*.txt /PATH:"c:\temp" /LOG:C:\DELETELOG.TXT

6. Delete *.txt files, using Regular Expression, ignore fileage:
robodelete.wsf /F:"^.*\.txt$" /REGEXP /PATH:"c:\temp"

7. Skip files if Archive is bit set or size is smaller than 100000 bytes
robodelete.wsf /F:* /PATH:"c:\temp" /ATTSKIP:A /MINSIZE:100000

8. If computer date is 2009-01-21 or later, Delete X:\*.tmp (366d+ old files)
robodelete.wsf /F:*.tmp /PATH:X:\ /MINAGE:366 /SAFEGUARD:2009-01-21

9. Move MP3 files and copy a notification file as replacement
robodelete.wsf /F:*.mp3 /PATH:X:\ /MOVE:PATH /MOVETARGET:Y:\ /NOTEFILE:a.txt
F File(s) to delete (see below for more)
PATH Specifies the rootpath to files to delete. (env vars accepted)
Y Suppress delete confirmation prompts (Yes to all)
MINAGE Chooses files based on their age. SEE WARNING BELOW
MAXAGE Chooses files based on their age. SEE WARNING BELOW
MINDATE Chooses files based on their date and time. See below.
MAXDATE Chooses files based on their date and time. See below.
SAFEGUARD Safeguard against incorrect computer time. See below.
COMP Fileage compare method. (See details below)
INTERVAL Interval of /MINAGE and /MAXAGE params. Default=days. See below.
R Recurse subfolders. Default: No recursion.
PATHFILTER Elaborate which folders to examine. Combine with /R (see below)
MINDEPTH Recurse depth. Default: No limit. Combined with /R (see below)
MAXDEPTH Recurse depth. Default: No limit. Combined with /R (see below)
Q Quiet: Suppresses console output.
LOG Logfile name. (environment variables accepted)
LOGSTAMP Timestamps the logfilename. If /LOG:a.txt -> a_YYYYMMDD_HHMMSS.txt
MINLOG Don't log successfull deletes (log errors and other messages only)
REPLACELOG Overwrite existing log (default: append to existing log)
FOLDERS Delete empty subfolders (folders of *ANY* name) also. Requires /R.
DISFLDAGE /FOLDERS switch will ignore /MINAGE and /MAXAGE definitions.
SIMULATE Nothing is really deleted. (and acts like /Y is defined).
ATTREQ Attributes required: Delete files with these attrs set (see below)
ATTSKIP Attributes skipped: No NOT delete files with attrs set (see below)
MINSIZE Minimum filesize in BYTES. Files smaller than this not deleted.
MAXSIZE Maximum filesize in BYTES. Files larger than this not deleted.
REGEXP /F is interpreted as regular expression (see below)
MOVE Move files instead of delete. (see below for more)
MOVETARGET Combined with /MOVE (see below for more)
NOTEFILE Create notefile for deleted/moved files. (see below for more)
F Parameter

Specifies filename(s) to delete. Multiple match patterns can be specified using pipe (|) as separator. Note, that you need to use double quotes around the value when using pipe-character, like this: /F:"*.txt|*.log"

Wildcards

*Match zero or more characters (any characters)
?Match zero or any one character
>Match exactly one (any) character (use double quotes!)
< Match any one or more characters, except \ (backslash)
This is mainly implemented for the /pathfilter switch to match a single directory in the path spesification. Not so usable in the /F parameter.

Examples

/F:*match any file
/F:"*.txt|*.log"match files ending ".txt" or ".log"
/F:log.txtexact match
/F:"FILE????_??_??.txt"matches, for example, "FILE__.txt" and "FILE1_2_3.txt"
/F:"FILE>>>>_>>_>>.txt"matches, for example, "FILE2009_01_28.txt"
REGEXP Parameter

Defines, that /F switch is a regular expression instead of traditional DOS-file name pattern. Note, that CMD.EXE handles characters like " | and > in a special way on the command line. Regexps are handled case insensitively.

Detailed documentation of regular expressions is way beyond the scope of this documentation. Therefore I'll assume that those who might find this parameter useful are allready familiar with them.

/REGEXP /F:"^.*\.txt$"   = Matches *.txt files.
MINAGE and MAXAGE Parameters

WARNING 1: See also: /INTERVAL
WARNING 2: See /SAFEGUARD to prevent accidental deletions

Filters files/folders based on their age. /COMP and /INTERVAL parameters modifies how the values of /MINAGE and /MAXAGE are interpreted.

Deleting a file changes the parent folders "Date modified"-timestamp: If you use /FOLDERS, notice that the "age" of the parent folder is possibly (depends from the value of /COMP) 0 seconds after deleting a file from it. Use /DISFLDAGE to disable the age test for folder deletions.

Examples

/MINAGE:4delete objects that are atleast 4 days old.
/MAXAGE:4PROTECT objects that are atleast 5 days old.
/MINAGE:4 /MAXAGE:4delete 4d (or over) old. Protect 5d (or over) old.
/MINAGE:2 /MAXAGE:4delete 2d (or over) old. Protect 5d (or over) old.

Note:
/MINAGE and /MINDATE are mutually exclusive.
/MAXAGE and /MAXDATE are mutually exclusive.
MINDATE and MAXDATE Parameters

Filters files/folders based on their datestamps. /COMP parameter modifies what dates are used for comparisons.

Deleting a file can change the parent folders "Date modified"-timestamp. If you use /FOLDERS, notice that the date of the parent folder is possibly (depends from the value of /COMP) 0 seconds after deleting a file from it. Use /DISFLDAGE to disable the age test for folder deletions.

It's strongly recommended to use an unambiguous format for dates: YYYY-MM-DD HH:MM:SS. There are quite a few formats for numeric dates out there. For example, people in the US and Europe might interpret them differently. If we write 06/07/2011 or 06.07.2011, did we mean 2011-07-06 or 2011-06-07? June or july? Same applies to computers. What are the regional settings? Are they the same in all the remote offices on all continents across the globe? Using the ISO8601 format above none of this has to be considered - it is the same everywhere.

Examples

/MINDATE:"2009-10-29 00:00:00"delete files on or after this time
/MAXDATE:"2009-10-29 23:59:59"delete files on or before this time
/MINDATE:"2009-10-29 00:00:00" /MAXDATE:"2009-10-29 23:59:59"range

Note:
/MINAGE and /MINDATE are mutually exclusive.
/MAXAGE and /MAXDATE are mutually exclusive.
INTERVAL Parameter

Defines what interval is specified on the /MINAGE and /MAXAGE parameters. If /INTERVAL is not defined, /MINAGE and /MAXAGE defaults to days.

/INTERVAL:sSeconds
/INTERVAL:nMinutes
/INTERVAL:hHours
/INTERVAL:dDays (default)
/INTERVAL:mMonths
/INTERVAL:yYears
COMP Parameter

Defines which file properties are used to compare the file age.

/COMP:CREFile age is determined from Create DateTime
/COMP:MODFile age is determined from Last Modified DateTime
/COMP:BOTH(Default) Most recent time is selected from CRE and MOD-dates.
MINDEPTH and MAXDEPTH Parameters

Defines which levels of subfolders are processed. Ignored if /R is not defined. Default: No limitation (all subfolders are processed). Negative values are treated as an error.

In the examples below path is defined as /PATH:C:\RootFolder and this is the example directory three:

      C:\RootFolder\Sub1\Sub2\Sub3\Sub4\Sub5\Sub6\Sub7
         ↑          ↑    ↑    ↑    ↑    ↑    ↑    ↑
         0          1    2    3    4    5    6    7   ← depth
/MAXDEPTH:0Only "RootFolder" examined (like /R not specified)
/MAXDEPTH:1"RootFolder" and "Sub1" processed.
/MAXDEPTH:2"RootFolder", "Sub1" and "Sub2" processed.
/MINDEPTH:3 /MAXDEPTH:3Only "Sub3" processed.
/MINDEPTH:2 /MAXDEPTH:4"Sub2", "Sub3", "Sub4" processed.
/MINDEPTH:5"Sub5" and all it's subfolders are processed.
/MINDEPTH:1Protect files on the "RootFolder", process the rest.
PATHFILTER Parameter

Allows you to define more pricisely which folders to examine for files to delete. Multiple match patterns can be specified using pipe (|) as separator. Enviromenment variables are expanded.

Wildcards

*Match zero or more characters (any characters)
?Match zero or any one character
>Match exactly one (any) character (use double quotes!)
< Match one or more characters, except \ (backslash). Handy for matching a single directory.

Examples

/R /F:*.dbx /PATH:"B:\Home" /PATHFILTER:"B:\Home\<\Application Data\Identities\<\Microsoft\Outlook Express" Delete Outlook Express datafiles on a "imagined" directory used as homedirectory share.
/PATHFILTER:"B:\Home\<\Temp"Matches "B:\Home\AnyUseUser\Temp", but not "B:\Home\AnyUser\SubDir\Temp"
/PATHFILTER:"B:\Home\<\Temp\*"Matches "B:\Home\AnyUser\Temp" and all Temp's subfolters.
/PATHFILTER:"B:\Home\<\Temp|R:\Home2\<\Temp"Uses pipe to match 2 different paths.
/PATHFILTER:*\TempMatches any path where it ends to "Temp" directory.
ATTREQ and ATTSKIP Parameters

Controls what files to/not to delete based on file attributes. Case of /ATTREQ and /ATTSKIP having same attributes defined is treated as an error. You can define either one, or both.

Attribute abbreviations

RRead only
HHidden
SSystem
AArchive
CCompressed

Abbreviations can be combined. Following example defines, that delete only not-readonly files, which are compressed and Archive-bit cleared. Hidden and System attributes are ignored:

/ATTREQ:C /ATTSKIP:RA

ATTREQ deletes the file only, if ALL defined bits are set.
ATTSKIP skips deletion, if ANY defined bit is set.
LOG and LOGSTAMP Parameters

Sets Logfile name. Environment variables are expanded. If combined with /LOGSTAMP, the resulting logfile name will be modified with timestamp: If LOG is "/LOG:a.txt", then the resulting filename is "a_YYYYMMDD_HHMMSS.txt".

Logfile has the following TAB-separated columns: ("OBJECT" = file or folder)
ROW_TIMESTAMP, OBJECT_AGE, FOLDERPATH, FILENAME, FILESIZE, ERROR_MESSAGE

SAFEGUARD Parameter

It is recommended to use /SAFEGUARD when using batch files to ensure, that computer clock is atleast on the specified date. If test fails, the script terminates immediately. Date format is "YYYY-MM-DD".

Incorrect time on computer clock can result in unintentional deletes when using /MINAGE and/or /MAXAGE. Example scenario:
Current date is 2011-01-15. During BIOS reset the computer clock is cleared to 1970-01-01. "Example file" is created 2011-01-14 (real age 1 day). A scheduled task is run after bootup, where MINAGE is set to 12 months. Because of incorrect computer time, the "Example file" passes the MINAGE test and gets deleted.

/SAFEGUARD:2009-01-21    = Terminate, if computer clock's current date is 2009-01-20 or earlier.
MOVE Parameter

Move files instead of deleting.

Move modes other than OFF has to be combined with /MOVETARGET.

Examples

/MOVE:OFFFiles are deleted (Default)
/MOVE:FLATAll files are moved directly to MOVETARGET. If file with same name exists on the MOVETARGET, the file is not moved.
/MOVE:FLATREPLACELike FLAT, but existing files on the MOVETARGET will be overwritten.
/MOVE:PATHFile's folder structure is replicated to MOVETARGET.
/MOVE:PATHREPLACELike PATH, but existing files on the MOVETARGET will be overwritten.
MOVETARGET Parameter

/MOVETARGET specifies the path where files are moved if /MOVE switch is used. Environment variables are expanded. The movetarget directory must exist prior to executing robodelete.

/MOVE:FLAT /MOVETARGET:\\SERVER\MISC
NOTEFILE Parameter

When file is deleted or moved, it is replaced with specified notefile on the original location. The notefile must exists and will be copied as is. Use whatever file format needed; html, wav or anything.

Examples

/NOTEFILE:RemoveNoteFile.txt
If "\Folder\MediaFile.mpg" is deleted/moved, then RemoveNoteFile.txt is copied to original location as "\Folder\MediaFile.mpg.txt".


/NOTEFILE:RemoveNoteFile.html

If "\Folder\HugeMediaFile.mpg" is deleted/moved, then RemoveNoteFile.html is copied to original location as "\Folder\HugeMediaFile.mpg.html".
 Community
2011-12-11 22:13 :: Author of robodelete
   
Staff
@ Casper: Thank you, much appreciated. It's nice to hear that you found
robodelete useful just like we did. Remember to use /SAFEGUARD option when
deleting files by their age - just to be on the safe side :) (at least if
you're using scheduled jobs). It saved my day once.
2011-12-05 17:35 :: Casper
   
It's great! I use robocopy and it's problem to delete file by age. Only MOVE. Thanks/
Your say:


Name/Alias (voluntary - not required):