Deleting Files by Age
Deleting Files by Age
Deleting Files by Age
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 (.).
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.
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:\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.
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"
echo.
echo Removing old completed jobs
forfiles /p D:\DocumentStore\storeToPurge /s /m iMedDoc*.* /d -120 /c
"cmd /c del @path"
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.