An Enhanced Directory Utility

Run this script to empower your directory searches

Stein Borge

November 4, 2002

5 Min Read
ITPro Today logo


File housekeeping is a seemingly never-ending task for network administrators. Removing temporary files, duplicate data, old service packs, blue-screen-of-death dumps, and unauthorized media files such as MP3s is repetitive and time-consuming. The Dir command, which you issue from a command prompt, is limited in its ability to query for anything more than filename information. You can use wildcards to list files (e.g., dir *.doc), but you can't specify date ranges or file sizes. Windows Explorer provides more flexible search capabilities but doesn't let you output the results to a text file or use scripts to automate searches (Windows Explorer requires human interaction, so you can't automate its features).

To help you perform regular administrative cleanup tasks, you need a command-line utility that can use a variety of criteria to perform directory search operations. I've written a script, edir.vbs, which Listing 1, page 2, shows, that lets you evaluate conditional expressions against files. Instead of wildcards or command-line switches, the Edir utility uses conditional expressions to filter files, so you can use complex criteria when searching. I developed the script under Windows 2000 Service Pack 2 (SP2) with Windows Script Host (WSH) 5.6. The script requires WSH 2.0 or later and VBScript 5.0 or later to execute. I've tested the script under Windows NT 4.0, and it will also run as is under Windows Me and Windows 9x.

Using Edir
The Edir command's syntax is

edir path criteria [output]

where path is the path to search and criteria specifies what files to search for. The path can be local or in Universal Naming Convention (UNC) format. The script uses File Scripting Object (FSO) properties, which Web Table 1 (http://www.winscriptingsolutions.com, InstantDoc ID 26966) shows, to build the criteria expression. You must surround the FSO properties within square brackets ([]).

The criteria expression follows the same rules as VBScript expressions do. The expression can use the logical operators AND, OR, and NOT to include any number of criteria. You must surround any text criteria with ASCII character 96 ('). VBScript requires quotation marks (") to represent text expressions, but the command-line processor doesn't take kindly to these, so use ASCII 96 instead. The following command lists media files under the network share \odinusers:

edir "\odinusers" "[ext]='mpg' or[ext]='mp3' or [ext]='mpeg' or[ext]='wav' or [ext]='mov' or[ext]='avi' or [ext]='jpg' or[ext]='wmv' or [ext]='asf'"

You handle dates as you would with VBScript or Visual Basic for Applications (VBA). The script treats as dates any expression that you surround with pound symbols (#). For example, the command

edir c:data "[Size]>1000000And [Ext]='doc' And[DateCreated]<#1/1/2001#"

lists all .doc files larger than 1,000,000 bytes created before January 1, 2001.

You can use any VBScript function in an expression. The command

edir e:data "[size]>10000000 And[DateLastAccessed] lists any file larger than 10,000,000 bytes and older than 365 days. Table 1 lists several constants that you can use when dealing with file attributes. A file's attributes can be a combination of any of the values in Table 1. For example, a read-only, hidden file with the archive bit set would have the value 35 (1 + 2 + 32). To test for a given attribute, use the AND Boolean operator. The commandedir c:data "[Ext]='doc' And[Attributes] And Archive"lists all files with the .doc extension and the Archive attribute set.The third script argument is optional. By default, the utility returns the path of any file that meets the specified criteria. When you pass the third argument, the utility uses it to control output. Edir treats the output argument as a VBScript string statement. You can use this argument to build complex output sequences. For example, the commandcscript edir.vbs e:data "[DateCreated] <#1/1/2001#" "'Move '''& [Path] & ''' ''e:archive' & [Name] & ''''" > archive.bat builds a batch file—archive.bat—that, when run, moves all files created before January 1, 2001, to an archive directory.You use standard VBScript string syntax to build the string. Edir uses the FSO property names within square brackets to identify file properties. For example, the commandedir e:data "[size]>10000000And [DateLastModified]<#1/1/2001#" "[size] & ',' &[path] & ',' &[DateLastModified]" > bigfiles.csvlists all files larger than 10,000,000 bytes and created before January 1, 2001, and redirects the output to the comma separated value (CSV) file bigfiles.csv.You can sort output by piping results to the Sort command. For example, the commandedir e:data "[size]>10000000And [DateLastModified]<#1/1/2001#" "Right(Space(15)& [size],15) & ' ' & [name]"| sort /rpads the output with spaces. The results pipe to the Sort command, which uses the /r switch to sort the results in descending order.How It Works 
Edir uses FSO to recurse directory structures to search for files. The search argument builds an expression that the script can evaluate. During execution, Edir passes the search criteria argument to the ReplaceCriteria function at callout B in Listing 1. The ReplaceCriteria function first replaces all occurrences of the ASCII 96 character with quotation marks. The utility then replaces any occurrences of the [ext] keyword in the search criteria with GetExt(objFile.Path) because the FSO File object doesn't expose an extension property. The GetExt function at callout A returns any extension from the file being evaluated. The utility uses a regular expression to update any remaining keywords surrounded by square brackets. Edir removes the square brackets and prefixes the keyword with objFile. The VBScript Eval function evaluates the resulting expression. If the result of the Eval is True, Edir outputs the file path. For example, the search criteria[Size]>100000 And [Ext]='doc'And [DateCreated]<#1/1/2001#"becomesobjFile.Size>100000 AndGetExt(objFile.Path)= "doc"And objFile.DateCreated <#1/1/2001#You can use Windows Explorer or the Dir command to search your directories, but these tools are limited. Use Edir when you want to expand your search capabilities.
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