SOLVED: Command Line Batch To Delete Files Based on Their Age

I needed to be able to delete files left on an FTP server I run that are older than two months and to accomplish that I used the FORFILES command that came out with Windows 7 and Server 2008.

I have been doing this type of scripting for a long time but was unfamiliar with FORFILES, so if you are like me you will find that bringing up a CMD and typing FORFILES /? to be very helpful.

To get to the point directly I ended chickening out on an automatic delete and decided just to move the files to a _TO BE DELETED folder:

FORFILES /P “F:\ftp” /S /D -60 /C “cmd /c if @isdir==FALSE move @file F:\_ToBeDeleted”

The English translation of this command is:

FORFILES in /Path “F:\ftp” including /Subdirectories /Dated more than -60 days ago, run the following /Command “cmd /c if this is NOT a DIRECTORY move the@file to F:\_ToBeDeleted”

If I wanted to just automatically delete the files and the folder, the command would be a much simpler:

FORFILES /P “F:\ftp” /S /D -60 /C “cmd /c del @file”

You could also accomplish this using the more common ROBOCOPY command which we have documented: urtech.ca/2015/08/solved-how-to-move-files-based-on-their-age-using-robocopy/

Below if the full explanation of the FORFILES command line:

FORFILES [/P pathname] [/M searchmask] [/S]
[/C command] [/D [+ | -] {MM/dd/yyyy | dd}]

Description:
Selects a file (or set of files) and executes a command on that file. This is helpful for batch jobs.

Parameter List:
/P    pathname      Indicates the path to start searching.   The default folder is the current working directory(.).

/M    searchmask    Searches files according to a searchmask.  The default searchmask is ‘*’ .

/S                  Instructs forfiles to recurse into subdirectories. Like “DIR /S”.

/C    command       Indicates the command to execute for each file. Command strings should be wrapped in double quotes.

The default command is “cmd /c echo @file”.

The following variables can be used in the command string:
@file    – returns the name of the file.
@fname   – returns the file name without extension.
@ext     – returns only the extension of the file.
@path    – returns the full path of the file.
@relpath – returns the relative path of the file.
@isdir   – returns “TRUE” if a file type is a directory, and “FALSE” for files.
@fsize   – returns the size of the file in bytes.
@fdate   – returns the last modified date of the file.
@ftime   – returns the last modified time of the file.

To include special characters in the command line, use the hexadecimal code for the character in 0xHH format (ex. 0x09 for tab). Internal CMD.exe commands should be preceded with “cmd /c”.

/D    date          Selects files with a last modified date greater than or equal to (+), or less than or equal to
(-), the specified date using the “MM/dd/yyyy” format; or selects files with a last modified date greater than or equal to (+) the current date plus “dd” days, or less than or equal to (-) the current date minus “dd” days. A valid “dd” number of days can be any number in the range of 0 – 32768. “+” is taken as default sign if not specified.

/?                  Displays this help message.

Examples:
FORFILES /?
FORFILES
FORFILES /P C:\WINDOWS /S /M DNS*.*
FORFILES /S /M *.txt /C “cmd /c type @file | more”
FORFILES /P C:\ /S /M *.bat
FORFILES /D -30 /M *.exe
/C “cmd /c echo @path 0x09 was changed 30 days ago”
FORFILES /D 01/01/2001
/C “cmd /c echo @fname is new since Jan 1st 2001”
FORFILES /D +7/7/2015 /C “cmd /c echo @fname is new today”
FORFILES /M *.exe /D +1
FORFILES /S /M *.doc /C “cmd /c echo @fsize”
FORFILES /M *.txt /C “cmd /c if @isdir==FALSE notepad.exe @file”

Please note that I found the THIS blog useful in working out my solution for automatically removing old files.

View Comments

  • I set the path to delete a txt file but i get the bellow error,
    kindly if it possible please reply to me.
    FORFILES /P D:\MP\Referance\*.txt /S /D -10 /C “cmd /c del @file”

    ERROR: Invalid syntax. '/c' option is not allowed more than '1' time(s).
    Type "FORFILES /?" for usage.
    Press any key to continue . . .

  • Tried the command and got the error:

    C:\>FORFILES /P "J:\Public\Temp" /S /D -365 /C "cmd /c del @file"
    ERROR: Invalid syntax. '/c' option is not allowed more than '1' time(s).
    Type "FORFILES /?" for usage.

    Not sure what I'm doing wrong as I copied and pasted the command above and simply changed the path and age of files.

    • Hi Cairan;

      Sometimes the web text does not copy/paste correctly. To fix that I have now adjusted the commands to be in a different format that does not change with copy / paste. I just tried it again and it seems happy :)

      FORFILES /P "F:\ftp" /S /D -60 /C "cmd /c if @isdir==FALSE move @file F:\_ToBeDeleted"

Published by
Ian Matthews
Tags: forfiles

This website uses cookies.