Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Storing the value of multiple variables

1 view
Skip to first unread message

Highlander

unread,
Sep 20, 2006, 10:36:56 AM9/20/06
to
Hello all. I've got a script that checks a folder for any changes and
notifies me. It will repeat this check in a timed loop (set to 60
seconds for now). It looks for the folder attribute "Last Modified",
and stores that timestamp in a variable called strRef (reference). On
the next check, it stores that subsequent timestamp in a variable
called strNew (new). Then it compares the two variables strRef and
strNew and looks for a difference.

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

axtens

unread,
Sep 20, 2006, 11:39:54 PM9/20/06
to
G'day Highlander

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

Highlander

unread,
Sep 21, 2006, 2:36:44 PM9/21/06
to

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

0 new messages