标签:
杂谈 |
分类: 工作 |
So many files and folders contends reports with old date, how to deal with them to prevent the space gets full. We use to use fdate.exe to isolate the old ones and delete them if they are older than 30 days. Until June 1st 2010, it is not working for W2K8 servers and wipe out everything. It time to change. After google a bit, forfiles.exe seems to be the new tool we need for the task. Here is the details about this 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"
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.
Issues and Troubleshooting
C:\>forfiles /p “C:\Temp” /s /m *.tmp /d -120 /c "cmd /c echo @path"
It was give us error and not running the command.
ERROR: Invalid argument/option - '@PATH'
Type "FORFILES /?" for usage.
It was solved by removed the quotation marks around the directory path like the following
C:\>forfiles /p C:\Temp /s /m *.tmp /d -120 /c "cmd /c echo @path"
Real examples:
Delete file older than 30 days in the path c:\local\data
Forfiles /p C:\local\data\ /s /m *.* /d -30 /c "cmd /c del @path"
Delete folders older than 31 days in the path c:\local\data
ForFiles /P
C:\local\data\