Blog

Five Steps to Create a Windows Script: File Counts by Extension

October 07, 2015 | Heroix Staff

Most sites have distinct features that require customized monitoring. In addition to basic PerfMon metrics, Event Logs and Pings, you may need to monitor responsiveness of a website, or the availability of an SMTP server, or the number of files in a directory. Customizable monitoring in Heroix Longitude is done with Transactions. Transactions run a specified test, compare the test result against a standard, and alert when the test fails.

The most flexible Transactions are done with Scripts – either a WinScript or UnixScript. Since we’re often working with multiple versions of Windows and PowerShell, we usually work with VBScripts to avoid compatibility issues.

In this post, we’re going to go over a simple VBscript we wrote for a customer who needed an alert if there were more than a given number of files in a directory with a specific file extension. Too many specific files resulted in processing being hung and corrective actions needed to be taken.

The outline for the script is:

1. The script will read in command line variables that are provided when the script is run. These variables will be used to specify the path to the directory to monitor and the file extension to be counted.

2. Create a FileSystem object and point it at the directory you want to monitor.

3. Loop through the files in the directory.

4. Increment a counter if the extension matches.

5. Output the number of files with the given extension.

 

If you’re familiar with VBScript please skip to the end. For those who haven’t worked with VBScript before, the details for each step listed above are:

 

1. The script will read in command line variables that are provided when the script is run. These variables will be used to specify the path to the directory to monitor and the file extension to be counted.

VBScript can use Wscript.Arguments to read in command line arguments. The indexes for Arguments start at 0:

LogDirectory = Wscript.Arguments(0)
FileExt = Wscript.Arguments(1)

We also modify the FileExt so that it starts with a “.” If it doesn’t already – more on that in step #4.
2. Create a FileSystem object and point it at the directory you want to monitor.
Windows Scripting can use a FileSystem Object to read in the properties of files and directories. In order to access the object we need to create an instance of the object:

set oFSO = createObject("Scripting.FileSystemObject")

Then use the GetFolder method to open a Folder object for the directory specified in the command line:

set oFolder = oFSO.GetFolder(LogDirectory)

3. Loop through the files in the directory

Looping through the files is done by first creating an object for the Files in the FolderObject that was just created:

Set LogFiles = oFolder.Files

and then Looping through each file with “For each” / “Next” loop:

For each FileName in LogFiles

Next

4. Increment a counter if the extension matches.
The check for this is more complex than just finding if the name for each file ends in a “.” followed by the given extension. We also need to account for the case where the given extension may exist as part of a longer extension – for example, we’re counting .doc files, but the directory contains a mix of .doc and .docx files.

The InStr function returns the start position of a substring within a string or the value 0 if the substring isn’t contained within the string. For example, InStr(“MyFile.Doc”,”Doc”) would return an 8. In our script we’re using the optional arguments for StartPosition (which defaults to 1), and Comparison (set to 1 for a case insensitive text comparison).

Before looping through the files, we use InStr to check if the given extension starts with a “.”, and to add “.” if it does not. This will avoid problems with files in which the extension as part of the file name (e.g. MyDoc.txt or MyDoc.Doc).

In order to avoid the situation where a “.doc” would match a “.docx” file, we need to add in some error checking. We do this by checking that the start position of the extension is equal to the total length of the filename minus the length of the extension (plus 1 because we’re counting the first character of the extension twice). That is:

InStr(1,”MyFile.Doc”,”Doc”,1) = Len(“MyFile.Doc”) – Len(“Doc”) + 1

InStr(1,”MyFile.Doc”,”.Doc”,1) = 7

Len(“MyFile.Doc”) = 10
Len(“.Doc”) = 4

Substituting in the variables used in the loop:

If InStr(1,FileName.Name,FileExt) = (Len(FileName.Name) – Len(FileExt) + 1)
FileCount = FileCount + 1
End If

5. Output the number of files with the given extension.

For this we could use either Wscript.Echo or Wscript.StdOut.Write, but Wscript.Echo returns a newline character in addition to the output and we want to suppress that in the script return:

Wscript.stdout.write FileCount

The full script is:

LogDirectory = Wscript.Arguments(0)
FileExt = Wscript.Arguments(1)

set oFSO = createObject("Scripting.FileSystemObject")
set oFolder = oFSO.GetFolder(LogDirectory)

FileCount = 0
If InStr(1,FileExt,".",1) <> 1 Then
FileExt = "." & FileExt
End If
ExtLen = Len(FileExt)

Set LogFiles = oFolder.Files

For each FileName in LogFiles
If InStr(1,FileName.Name,FileExt,1) = (Len(FileName.Name) - ExtLen + 1) Then
FileCount = FileCount + 1
End If
Next

Wscript.stdout.write FileCount

This script should be saved with a .vbs extension (e.g. FileExtCount.vbs), and executed as:

Cscript /nologo FileExtCount.vbs \myserverc$pathtofolder ext

for example:

Cscript /nologo FileExtCount.vbs \sqlserverc$processingfiles  doc

This would return a count of all files ending in “doc” in the named directory.

Please note that this is meant to be a basic script and does not cover error checking beyond verifying that the given extension is not part of a larger extension. Post a comment if you run into any problems or have any requests for modifications.

Want to learn more:

Download a FREE trial of Longitude  - Stand up Longitude in just minutes and immediately start seeing how your environment is performing, receive proactive alerts, reveal root causes, automate corrective actions, and more...

 

Start Your Free 30 Day Trial of  Longitude Today!