I had a request for a script to monitor the number of files located in the root of a file share. Since the name of the share is unknown, they wanted a list of shares defined in a configuration file.
To make it more exciting, they wanted an event written to the application log of the host running the script. But wait, there's more! I can't just use the VBScript command (WSHShell.LogEvent) because they want something other than event ID 0.
So what is the solution?
Well first, I need to define some variables.
sEventID is the event ID between 0 and 1000 that they want to use
sEventType can be one of Information, Warning or Error.
Next, we open the config file and we loop through each line assigning the line to a variable so we can then enumerate the share.
Set oConfig = CreateObject("Scripting.FileSystemObject")
Set oShareList = oConfig.GetFile("ShareList.ini")
'Ensure the INI file has content
if oShareList.Size > 0 then
Set oReadConf = oConfig.OpenTextFile("ShareList.ini",1) Do until oReadConf.AtEndOfStream
sShare = oReadConf.ReadLine
ok, now we have that done, we can do the real work.
Here we want to loop through each file share getting a count of the number of physical files. If we want to go deaper, we need to also go through each subfolder.
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFolder = objFSO.GetFolder(sShare)
Set colFiles = objFolder.Files sText = sText & "Share name is " & sShare & vbCrLf sText = sText & "Script started at " & Time() & vbCrLf
iCount = colFiles.Count
sStartTime = Now()
ShowSubfolders objFSO.GetFolder(sShare)
sEndTime = Now() sText = sText & "Total file count is " & iCount LogIt sText
sText = ""
loop
That's basically it for the main portion of the script. There are now two subroutines to discuss. First is ShowSubFolders and the other is LogIt.
ShowSubFolders will count the number of files located in each subfolder.
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files iCount = iCount + colFiles.Count
ShowSubFolders Subfolder
Next
End Sub
This is pretty self explanitory. We create a file system object and point to the path of the sub folder. We then count the number of files and call the sub routine with the name of the current sub folder and we keep going until all sub folders have been gone through. How easy is that?
The LogIt routine is where we log our count to the event log.
Again though, we could have done this in two lines of code but it is not as feature rich as this solution.
Sub LogIt(sText)
sCmd = "EventCreate /T " & sEventType & " /ID " & sEventID & _
" /L APPLICATION /SO ShareFileCount /D """ & sText & """" Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
Set objProcess = objWMIService.Get("win32_process")
Set objProcessSU = objWMIService.Get("win32_ProcessStartUP")
Set objConfig = objProcessSU.SpawnInstance_
errRTN = objProcess.Create(sCmd, null, objConfig, procID)
End Sub
sCmd is our command. We are calling the EventCreate command with switches. /L is the logfile. In our case, the Application log. /ID is the event ID which is defined in our variable, /T is the event type, /SO is the event source which I have defined as the script name and /D is the event text I want posted to the log. To make it work, the text following /D has to be inside quotes.
We then create an object to a WMI call and execute it. This creates a process on the defined server (. being local) and writes the data to the event log.
It is just that simple