11. The command FORFILES

A very useful command for batch processing is 'FORFILES'. It executes a command on each file that is selected. An interesting feature is that you are able to select files on a timestamp.

The complete syntax is:
FORFILES [/P Path] [/m SearchMask] [/S] [/C Command] [/D [+ | -] {date | dd}]
which is really puzzling. So let's extract what we need.
FORFILES [/P Path] [/M SearchMask] [/C Command] 
where '[/P Path]' refers to the Path to search. If we use the default, i.e. the current directory, the syntax will be still easier to understand:
FORFILES [/M SearchMask] [/C Command]
With '[/M SearchMask]', you specify a search mask for selecting files. The default is *.* and refers to all files.

The command parameter '[/C command]' is used to execute some command on each file. The default is "cmd /c echo @file" (with double quotes!) where '@file' is the name of the file. Several command variables can be used in the command string. The most important commands for now are:

'@file' > the name of the file
'@fname' > the filename without extension
'@ext' > only the extension of the file

Let's have a look at some examples.

11.1 Add a prefix to filenames

Type:
D:\test>FORFILES /M *.txt /C "cmd /c REN @file PREFIX_@file" [ENTER]
All text files will have now a filename that start with "PREFIX_".

11.2 Add a suffix to a filename

Type:
D:\test>FORFILES /M *.txt /C "cmd /c REN @file @fname_SUFFIX@ext" [ENTER]      
All text files will have now a filename that ends with "_SUFFIX".

11.3 Modifying filenames in the current directory and its subdirectories

Do you also want change the files in subdirectories? Just add the '/S' parameter to the command. So to extend filenames of text files in the current directory and its subdirectories with a suffix, type:
D:\test>FORFILES /S /M *.txt /C "cmd /c rename @file @fname_SUFFIX@ext" [ENTER]

11.4 Selections files on a date

Adding '/D -dd' to the 'FORFILES' makes it possible to select files older than 'dd' days (basis: the current date). A valid 'dd' number of days can be any number in the range of 0 to 32768(=89 years).

Make first a subdirectory 'backup':
D:\test>MD backup [ENTER]
To backup all text files in the current directory that were created/modified more than three days ago, type:
D:\test>FORFILES /M *.txt /C "cmd /c COPY @file D:	est\backup" /D -3 [ENTER]
Or before a specific date:
D:\test>FORFILES /M *.txt /C "cmd /c COPY @file D:	est\backup" /D -01/01/2016 [ENTER]
(Maybe you have to adapt the date format, e.g 01-01-2016) To backup all text files in the current directory created/modified after 1st January 2016, type
D:\test>FORFILES /M *.txt /C "cmd /c COPY @file D:	est\backup" /D +01/01/2016 [ENTER]
(with the plus sign before the date. The '+' could be omitted because it is default).

11.5 List files and export the list to a file

The switch '/D' can be put anywhere in the command, that can be fine for readability. Example: to select all files in the current directory that were created/modified more than a day ago and export the result to a file, type:
D:\test>FORFILES /M *.* /D -1 /C "cmd /C ECHO @file" > D:\test\result.txt [ENTER]