The folder names to be checked are listed in the text file
"folders.txt". The script works fine, when I'm checking only one
folder. When I want to check multiple folders, and I add more than one
name to "folders.txt", I can't get it to work. I've tried modifying the
script several ways but the problem I always run into is that I can't
seem to "store" the variables strRef and strNew for specific folders.
The variables get mixed up between two different folders.
Any help would be greatly appreciated. Thanks!
- Dave
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim strComputer, sPath, List, sFolder, sFullPath, LastMod, strRef,
strNew
Do While ScriptFinish <> True
CheckFolders
Wscript.Sleep 60000
Loop
Sub CheckFolders
strComputer = "."
sPath = "D:\Tasks\Services\"
List = "Folders.txt"
Set ListFile = objFSO.OpenTextFile(List, 1)
Do Until ListFile.AtEndOfStream
sFolder = ListFile.ReadLine
sFullPath = sPath & sFolder
sFullPath = replace(sFullPath, "\", "\\")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer &
"\root\cimv2")
Set colFolders = objWMIService. _
ExecQuery("Select * from Win32_Directory where name = " _
& "'" & sFullPath & "'")
For Each objFolder in colFolders
Wscript.Echo Now
Wscript.Echo "Name: " & objFolder.Name
LastMod = objFolder.LastModified
Wscript.Echo "Last modified: " & LastMod
'~~~ If the "strRef" variable is empty, that means
'~~~ the script is starting for the first time.
'~~~ The variable then needs to be assigned a value.
IF IsEmpty(strRef) Then
strRef = sFolder & LastMod
Else
'~~~ If the "strRef" variable is NOT empty, that means
'~~~ the script has already run once within the timed loop.
'~~~ The variable then needs to be compared against
'~~~ the variable "strNew".
strNew = sFolder & LastMod
IF strRef = strNew Then
Wscript.Echo "Folder has not changed." & vbCrlf
Else
Wscript.Echo "FOLDER HAS CHANGED!" & vbCrlf
strRef = strNew
End IF ' strRef = strNew
End IF ' IsEmpty(strRef)
Wscript.Echo vbCrlf
Next
Loop ' Do Until ListFile.AtEndOfStream
ListFile.close
End Sub
Set objFSO = nothing
Set ListFile = nothing
Set objWMIService = nothing
Set colFolders = nothing
You might want to try using arrays. I fiddled with your code, as below.
I didn't take a long time over it, so may have arrayed what didn't need
it. I also changed the sleep so that things would move faster. You'll
probably want to change it back.
Kind regards,
Bruce M. Axtens
Strapper Technologies
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim strComputer, sPath, List, sFolder, sFullPath, LastMod(10),
strRef(10), strNew(10)
Dim nInstance
Do While ScriptFinish <> True
CheckFolders
Wscript.Sleep 10000
Loop
Sub CheckFolders
strComputer = "."
sPath = "C:\Documents and Settings\bruce\My Documents\My
Downloads\"
List = "Folders.txt"
Set ListFile = objFSO.OpenTextFile(List, 1)
nInstance = 0
Do Until ListFile.AtEndOfStream
sFolder = ListFile.ReadLine
sFullPath = sPath & sFolder
sFullPath = replace(sFullPath, "\", "\\")
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer &
"\root\cimv2")
Set colFolders = objWMIService. _
ExecQuery("Select * from Win32_Directory where name = " _
& "'" & sFullPath & "'")
For Each objFolder in colFolders
Wscript.Echo Now
Wscript.Echo "Name: " & objFolder.Name
LastMod( nInstance ) = objFolder.LastModified
Wscript.Echo "Last modified: " & LastMod( nInstance )
'~~~ If the "strRef" variable is empty, that means
'~~~ the script is starting for the first time.
'~~~ The variable then needs to be assigned a value.
IF IsEmpty(strRef( nInstance ) ) Then
strRef( nInstance ) = sFolder & LastMod( nInstance )
Else
'~~~ If the "strRef" variable is NOT empty, that means
'~~~ the script has already run once within the timed loop.
'~~~ The variable then needs to be compared against
'~~~ the variable "strNew".
strNew( nInstance ) = sFolder & LastMod( nInstance )
IF strRef( nInstance ) = strNew( nInstance ) Then
Wscript.Echo "Folder has not changed." & vbCrlf
Else
Wscript.Echo "FOLDER HAS CHANGED!" & vbCrlf
strRef( nInstance ) = strNew( nInstance )
End IF ' strRef = strNew
End IF ' IsEmpty(strRef)
Wscript.Echo vbCrlf
Next
nInstance = nInstance + 1
That's the ticket Bruce! Creating the array. And every variable that
you arrayed needed to be. I did have to increase the size of the array
from (10) to (15), since I have more than 11 folders I need to check.
Thanks again. Cheers mate!
- Dave