DeleteFilesOlderThan
Purpose
DeleteFilesOlderThan lets you delete files of a certain age. The number of files deleted is returned
via the ERRORLEVEL environment variable.
Example in a batch file, assuming four files in the target folder which are older than two days:
DeleteFilesOlderThan /time=2d /path=c:\data\ztemp
ECHO From batch: %ERRORLEVEL% file(s) deleted.
... results in ...
• DeleteFilesOlderThan v1.4.2 •
Copyright © 2013-2016 by BasicAware. All rights reserved.
Written by Knuth Konrad
Time : 1d
Folder : .\data\
File pattern : *.tmp
Recurse subfolders: False
Verbose : False
-- Scanning folder .\data\
- File pattern: *.tmp
- Deleting sa5a41.tmp
- Deleting sa5a42.tmp
- Deleting sa5a53.tmp
- Deleting sa5ab1.tmp
- Skipping test.cmd
Done. 4 file(s) deleted.
C:\DATA\ZTEMP>ECHO From batch: 4 file(s) deleted.
From batch: 4 file(s) deleted.
Download
DeleteFilesOlderThan
Installation
None. Download the ZIP file, extract the included executable and run it.
Please note: this is a console application. You should run it from a command prompt.
Use case(s)
-
-
No matter if it's log files or (older) backups, this tools helps you to keep only the necessary files around. Need
your log files for three month only? The oldest backup set is always that from last year?
DeleteFilesOlderThan helps you in automating the housekeeping.
Syntax
DeleteFilesOlderThan /time=<time specification> /path=<folder to delete files from>
[/filepattern=<files to delete>] [/subfolders=0|1] [/verbose=0|1] [/recyclebin=0|1] [/readonly=0|1] [/filessmallerthan=<size>|/filesgreaterthan=<size>]
[/processpriority=i|b]
... or ...
DeleteFilesOlderThan /t=<time specification> /p=<folder to delete files from>
[/f=<files to delete>[;<files to delete>]] [/s=0|1] [/v=0|1] [/rb=0|1] [/r=0|1] [/fst=<size>|/fgt=<size>] [/pp=i|b]
Parameters:
-
/t or /time = time specification, the following units are supported:
-
d = day, i.e. 1d
-
w = week, i.e. 2w
-
m = month, i.e. 3m
-
y = year, i.e. 4y
-
/p or /path = (start) folder. If the path includes spaces (or other special characters), enclose the path in double quotation marks, e.g. /p="C:\Program Files\MyApplication". Rule of thumb: if in doubt, use double quotation marks.
-
/f or /filepattern = file pattern. If omitted, all files are scanned (equals /f=*.*).
Multiple file patterns may be specified by using ";" as a separator, i.e. /f=*.doc;Backup*.rtf
-
/s or /subfolders = recurse subfolders: yes(1) or no (0). Default is 0, do not recurse subfolders.
-
/v or /verbose = (more) verbose out: yes(1) or no (0). Default is 0.
-
/rb or /recylcebin = delete to recyly bin (1) instead of permanent deletion (0). Default is 0 (=permanent deletion).
-
/r or /readonly = delete readonly files (1). Default is 0 (=don't delete readonly files).
-
/la or /lastaccess = Base file deletion on the file's LastAccessTime instead of LastWriteTime.
-
/hc or /hideconsole = hide the application's (console) window? No(0) or yes(1).
-
/pp or /processpriority = set (lower) this process' priority to Idle or Below normal.
-
/fst or /filessmallerthan = delete only files smaller than the given size.
-
/fgt or /filesgreaterthan = delete only files greater than the given size.
-
Please note that you may only use either /fst or /fgt. You can't use both parameters together. If you happen to pass both parameters, the last one
of the two in the parameters string "wins". File size format:
- <empty> = Byte, i.e. 100
- kb = Kilobyte, e.g. 100kb
- mb = Megabyte, e.g. 100mb
- gb = Gigabyte, e.g. 100gb
- tb = Terabyte, e.g. 100tb
- Please note: 1 KB = 1024 byte, 1 MB = 1024 KB etc.
Examples
DeleteFilesOlderThan /time=2d /path=D:\MyTarget
Delete all files from the folder D:\MyTarget which are older than two days.
DeleteFilesOlderThan /t=3w /p=C:\MyTarget\Data /f=*.txt /s=1
Delete all text files (*.txt) from the folder C:\MyTarget and all subfolders which are older than three weeks.
Source code
The PowerBASIC source code is available at Github
Known issues / limitations
Version
-
1.8.11 - November 2021
-
New parameter: /la.
-
Delete readonly files.
-
1.8.7 - February 2021
-
New parameter: /r.
-
Delete readonly files.
-
1.8.5 - November 2019
-
Echo error messages to STDOUT.
-
Error messages are now also written to STDOUT so that they get captured, if STDOUT is redirected
to a file.
-
1.8.1 - June 2018
-
New parameter: /pp.
-
Environment variables: Environment variables, e.g. %UserProfile% may be passed as (part of) the command line parameters.
-
1.6.6 - Juli 2017
-
Recompile because of a change in a static link lib.
-
1.6.5 - Juni 2017
-
More rigorous parameter evaluation for parameter /t.
-
1.6.4 - May 2017
-
Compiler glitch workaround.
-
1.6.0 - March 2017
-
New - The parameter /rb deletes the files to the recycle bin.
-
1.5.0 - January 2017
-
New - two new parameters for restricting the files to be deleted to certain file sizes.
-
1.4.3 - November 2016
-
Change - The amount of disk space recovered by the files deleted is shown.
-
1.4.2 - November 2016
-
Bugfix - folder/file names with space (see a pattern here?) passed as parameter weren't recognized correctly.
-
1.4 - April 2016
-
Multiple file patterns may now be passed.
-
1.3 - March 2016
-
Bugfix - file names with spaces were ignored by the tool.
-
1.2 - August 2015
-
Bugfix - folder names with spaces were ignored by the tool.
-
1.0 - April 2013