Need some help creating a script that will look for 0kb files
I need some help writing a script to sift through a particular directory and find any 0kb files that are more than 20 minutes old. We have an app that writes data to txt files with random file names, and sometimes the process fails and restarts, leaving the 0kb file until it's restarted. So I want to focus on ones that are more than 20 minutes old to prevent the script from finding legitimate 0kb files because they're still in the process of being written. Problem is I have little scripting knowledge, so does anyone have an existing script I can use/modify? Thanks!
October 20th, 2010 11:59am

Use ForFiles command from Windows 2003. Copy to XP if needed on the desktop. Run this command from any command line, change c:\windows to the desired path. You can add some logic to check the date/time if needed as well. Although 20mins is a pretty small time to test against, a day is much easier using the /D switch. forfiles /S /P c:\windows /M *.* /C "cmd /c If @fsize==0 Echo @path @fsize" 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 +10/20/2010 /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"
Free Windows Admin Tool Kit Click here and download it now
October 20th, 2010 3:40pm

Hi, Please try the following PowerShell script: Dir| where{ $_.length –eq 0 –and $_.LastWriteTime –lt [DateTime]::Now.AddHours(-1/3) } Thanks.This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
October 21st, 2010 3:49am

Hi, Do you need any other assistance? If there is anything we can do for you, please let us know. Thanks.This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" on the post that helps you, and to click "Unmark as Answer" if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Free Windows Admin Tool Kit Click here and download it now
October 26th, 2010 4:56pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics