Retrieving File Properties

Microsoft® Windows® 2000 Scripting Guide

Files have a number of properties that are extremely useful for managing a file system. For example, the DateLastAccessed property tells you the date when someone last opened the file. This property can be used to identify files that are taking up disk space yet are never used. Similarly, the Size property tells you the size of a file in bytes. This helps you to better analyze disk usage; you can tell whether a single file might be using up more than its fair share of storage space.

Traditionally, system administrators have accessed file properties by using either Windows Explorer or command-line tools. Although these tools can return information about the files on a computer, they are not always designed to save this data or to act on it. In addition, many of these tools have only a limited ability to be automated, making it more difficult for system administrators to periodically sweep their hard drives and search for files that meet specific criteria.

Fortunately, detailed information about any file on a computer can also be retrieved by using the FileSystemObject; among other things, this allows you to automate the process of querying the file system for information about a file or group of files. A complete list of properties available through the File object is shown in Table 4.5.

Table 4.5 File Object Properties




Bitmap containing the attributes for the file.


Date that the file was created.


Date of the last time a user accessed the file.


Date of the last time a user modified the file.


Drive letter and trailing colon (for example, C:) representing the drive on which the file is stored.


File name, not including path information. For example, the Name of the file C:\Windows\System32\Scrrun.dll is Scrrun.dll.


Name of the folder in which the file is stored. For example, the ParentFolder of C:\Windows\System32\Scrrun.dll is Windows.


Full path of the file (for example, C:\Windows\System32\Scrrun.dll).


MS-DOS-style name of the file, using the 8.3 naming convention. For example, the file C:\MySpreadsheet.xls might have the ShortName MySpre~1.xls.


MS-DOS-style path to the file, using the 8.3 naming convention. For example, the file C:\Windows\Program Files\MyScript.vbs might have the ShortName C:\Windows\Progra~1\MyScript.vbs.


Total size, in bytes, of the contents of the file.


String describing the file type, as recorded in the registry (for example, "Microsoft Word Document").

To access file properties, a script must:

  1. Create an instance of the FileSystemObject.

  2. Use the GetFile method to create an object reference to a particular file. The script must pass the path of the file as the GetFile parameter.

  3. Echo (or otherwise manipulate) the appropriate file properties.

For example, the script in Listing 4.27 uses the GetFile method to bind to the file C:\Windows\System32\Scrrun.dll and then echoes a number of the file properties.

Listing 4.27 Enumerating File Properties

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.GetFile("c:\windows\system32\scrrun.dll")
Wscript.Echo "Date created: " & objFile.DateCreated
Wscript.Echo "Date last accessed: " & objFile.DateLastAccessed
Wscript.Echo "Date last modified: " & objFile.DateLastModified
Wscript.Echo "Drive: " & objFile.Drive
Wscript.Echo "Name: " & objFile.Name
Wscript.Echo "Parent folder: " & objFile.ParentFolder
Wscript.Echo "Path: " & objFile.Path
Wscript.Echo "Short name: " & objFile.ShortName
Wscript.Echo "Short path: " & objFile.ShortPath
Wscript.Echo "Size: " & objFile.Size
Wscript.Echo "Type: " & objFile.Type

When this script runs under CScript, output similar to the following appears in the command window:

Date created: 10/29/2001 10:35:36 AM
Date last accessed: 2/14/2002 1:55:44 PM
Date last modified: 8/23/2001 4:00:00 AM
Drive: c:
Name: scrrun.dll
Parent folder: C:\Windows\system32
Path: C:\Windows\system32\scrrun.dll
Short name: scrrun.dll
Short path: C:\Windows\system32\scrrun.dll
Size: 147483
Type: Application Extension