Determine a Files Age

Use WSH to determine the age of a file.

Readers

February 10, 2003

1 Min Read
ITPro Today logo in a gray background | ITPro Today


Knowing the age of a file is useful for archiving and housekeeping purposes. In "Real-World Scripting: Deleting Files by Their Age" (June 2000, http://www.winscriptingsolutions.com, InstantDoc ID 8799), Dick Lewis presents a Perl script that checks files' ages and deletes files that are older than the age you specify. Dick's script requires that you download and install Perl for Win32. I wrote an alternative script, which Listing 3 shows, that uses Windows Script Host's (WSH's) standard features to calculate files' ages. My script uses the File System Object's (FSO's) File.DateCreated property and the Now function that returns the current date and time. The difference between these two values tells you a file's age. To use the script, go to a command prompt and enter

Cscript fileage.vbs   Path_and_Name_of_File

For example, entering

Cscript fileage.vbs c:windowsotepad.exe

on my system results in the output NOTEPAD.EXE is 263 days old.

—Wilfred Wright
[email protected]

Sign up for the ITPro Today newsletter
Stay on top of the IT universe with commentary, news analysis, how-to's, and tips delivered to your inbox daily.

You May Also Like