Вы находитесь на странице: 1из 6

Deleting Files by Age

The FORFILES command


Forfiles can be found in the Windows 2000 Resource Kit and built-in with Windows 2003/Vista.

Here is the output from forfiles /?

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 +8/19/2005 /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"

Batch files and the command prompt

A batch file is simply a list of commands that you would otherwise type out at the command prompt (CMD). To
execute commands such as "del" and "copy", you just place each command on a separate line in a text file
ending in .BAT or .CMD.

In the world of DOS/CMD, echoing is repeating text or commands to the user as they are executed. If we wrote
a batch file that simply said "copy C:\file.txt D:\file.txt", you would see that text appear and then the response
from the copy command, such as "1 file(s) copied." To hide our command from being shown to the user, you
can prefix it with @. So "@del D:\file.txt" will not show the user anything. Instead of having to put a @ in front
of everything, just put "@echo off" at the top of our batch file. The user will see the results of commands
being executed (prompts, success or failure messages, etc) but won't have to see those commands as they are
executed. To purposely write a message to the user, simply write "echo Hello World" and the words "Hello
World" will be shown to the user.

To read more about the commands available from the command prompt, type "help" in the command prompt.
To know more about a command (such as ECHO), type "help echo" or "echo /?" Be aware that several
commands act differently when used interactively (CMD) or batched.
Understaning our forfiles utility

Let's analyze the forfiles utility and the command parameters that I'll be using.

forfiles /p C:\Temp /s /m *.tmp /d -120 /c "cmd /c echo @path"

The parameters /p, /s, /m, /d, and /c were explained in Part 1. In the above example, I'm going to look at the
C:\Temp folder (/p C:\Temp) and all the subfolders (/s) for any files ending in tmp (/m *.tmp) that are older than
120 days (/d -120) and I'll echo the filename back (/c).

The /c parameter is: "cmd /c echo @path". So I'll break that down real quick.

"cmd /c" will start a new command shell, execute the given command and then exit. You can learn more about
cmd itself by typing "help cmd" or "cmd /?" at a command prompt. The command we want "cmd /c" to run
follows directly after the parameter switches (/c) and is listed as "echo @path". Echo, as explained above, will
display some text back to the user. The text here is @path. In turn, @path is replaced with an absolute
filename.

To give you an example. Let's say you had three files in C:\Temp that were more than 120 days old. Those files
are A.TMP, B.TMP, and C.TMP. If you ran the above command at the command prompt, you will see:

C:\>forfiles /p C:\Temp /s /m *.tmp /d -120 /c "cmd /c echo @path"

"C:\Temp\A.TMP"

"C:\Temp\B.TMP"

"C:\Temp\C.TMP"

Basically we're going to use all the same parameters but instead of echoing the filename back, we're going to
delete it with the del command.

Creating our batch file

This stops our commands from being shown back to the user during execution and gives us a blank line.

@echo off
echo.

We will now remove the trace files (trace*.*) from the documentService\bin folder. Note the first echo
command informs the user to what we're about to do.
echo Removing old documentService log files
forfiles /p C:\documentService\bin /s /m trace*.* /d -120 /c "cmd /c
del @path"

Let's print a blank line between each set of commands. We'll display a message to the user and then remove
the trace files (trace*.*) from the emrRun\bin folder.

echo.
echo Removing old emrRun log files
forfiles /p C:\documentService\emrRun\bin /s /m trace*.* /d -120 /c
"cmd /c del @path"

Now we move from trace logs to completed jobs. Let's first remove old backup images.

echo.
echo Removing old image files
forfiles /p D:\DocumentStore\imagingShare /s /m *.tif* /d -120 /c "cmd
/c del @path"

And then completed jobs that have succeeded.

echo.
echo Removing old completed jobs
forfiles /p D:\DocumentStore\storeToPurge /s /m iMedDoc*.* /d -120 /c
"cmd /c del @path"

Putting it all together

Here are the full contents of the file.

Batch File - this file ends in .BAT and may be (and should be) rejected by your Anti-Virus or Anti-Spyware
software.

Text File - this file ends in .TXT and needs to be renamed to .BAT locally in order to be executed. It should save
to your local hard drive without any problems. Make sure you are not "hiding know file extensions" in Windows
Explorer when adding on .BAT to the filename.
Scheduling your task

Scheduling the batch file for execution using Windows Task Scheduler is fairly easy.

In Windows, click Start -> Control Panel -> Scheduled Tasks -> Add Scheduled Task

Follow the simple wizard by selecting the batch file, detailing the frequency at which to run, and specifying user
credentials. The user is important because the account executing the batch file will need authorization to delete
files in both C:\documentService\ and D:\DocumentStore\.

If the user is part of a password change policy, be sure you update the password here as well or the batch file
will not run. You'll get an entry in the Event Viewer if authentication fails.

Вам также может понравиться