CRM Hosting


Add to Technorati Favorites

Windows forfiles

Digg this story ?

In many situations you need to process a particular command against an entire set of files. With forfiles you can, for instance, delete old log files or temporary files, among other things. On Windows 2003 and Vista it is installed by default, if you are using Windows XP Pro you will find it in Windows Resource Kit Tools.

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. 0×09 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 0×09 was changed 30 days ago”
FORFILES /D 01/01/2001
/C “cmd /c echo @fname is new since Jan 1st 2001″
FORFILES /D +5/12/2008 /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”

The help from forfiles /? is pretty useful, but I’ll give you two more examples of how the command can be used.

If you want to remove the extension of zip files in the current folder, for instance, you can use:

forfiles /p . /m *.zip /c “cmd /c ren @file @fname”

The second example is a little more complex. Let’s say that every day you open some files from a folder and modify them. You want to save every night the files modified during the day in a different location, arranged like:

Drive:\backup_folder\month\day\*

This scenario can be useful if you want to have some kind of history for the changes in the files.

First of all, let’s set the root of the backup tree:

Set ROOT=H:\backup

Now we’ll play a little with the date and will extract the current day, month and year.

@For /F “tokens=2,3,4 delims=/ ” %%A In (‘Date /t’) do @(
Set Month=%%A
Set Day=%%B
Set Year=%%C
)

Rem @Echo DAY = %Day%
Rem @Echo Month = %Month%
Rem @Echo Year = %Year%

Set DATA=%Month%/%Day%/%Year%
Set P=%ROOT%\%Month%\%Day%

I also set the path where the modified files will be copied. Now I’ll check if the folders exist and if not I will create them.

Echo “Verify folders”

@If Not Exist %ROOT%\%Month% Goto makem
Goto testd

:makem
mkdir %ROOT%\%Month%

:testd
If Not Exist %ROOT%\%Month%\%Day% Goto maked
Goto copyfiles

:maked
mkdir %ROOT%\%Month%\%Day%
Goto copyfiles

This example assumes that the folder h:\backup already exists.

Now we have all the folders we need, so we can copy the files.

:copyfiles
Echo “Now I copy the files”

forfiles /s /m *.* /d +%DATA% /c “cmd /c copy @FILE %P%\”

I’ll explain the forfiles syntax used:

/s – search in subdirectories

/m *.* - search every file

/d +%DATA% - search files modified today

If you have other examples please add them.


You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

AddThis Social Bookmark Button
Comments are DoFollow, so you may consider writing a small note :)

Leave a Reply



BRDTracker