VBScript help required - How do I check a second path

I need a little help with this script.  The logic is working as I want it to, but I need to be able to have it check two different SubFolder paths on the same server.  I'm guessing this can be done in a single script, it's just that I am not sure how. The script is attached.

BTW - the script simply checks the subfolders modified date against the current date.

I hope I'm explaining myself clearly. If not please ask questions.

Thanks in advance.

SK
'Dim Path1, Path2  - Not sure this is correct way to do what I want

Const ForWriting = 2

Outputfile = "C:\TEST\Result.csv" 'Output results to csv

strComputer = "SERVERN002"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set Output = objfso.CreateTextFile(Outputfile, ForWriting, True)

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

strFolderName = "C:\FOLDER\INFO-1"  'Currently works to check the subfolders, but it is only one folder I need to check
'Path1 = "C:\FOLDER\INFO-1"    
'Path2 = "C:\FOLDER\INFO-2"   'I also want to check the subfolders of this folder

Output.WriteLine

Set colSubfolders = objWMIService.ExecQuery _
    ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
        & "Where AssocClass = Win32_Subdirectory " _
            & "ResultRole = PartComponent")

For Each objFolder in colSubfolders
    GetSubFolders strFolderName
Next

Sub GetSubFolders(strFolderName)
    Set colSubfolders = objWMIService.ExecQuery _
        ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
            & "Where AssocClass = Win32_Subdirectory " _
                & "ResultRole = PartComponent")

    For Each objFolder in colSubfolders
        strFolderName = objFolder.Name
        dtLastModified = WMIDateStringToDate(objFolder.LastModified)
    If Day(Now)&Month(Now)&Year(Now) = Day(dtLastModified)&Month(dtLastModified)&Year(dtLastModified) Then
        Output.WriteLine objFolder.Name
        Output.WriteLine "Last modified: " & dtLastModified & " **** Successful ****"
        Output.WriteLine ""
    Else
      Output.WriteLine objFolder.Name
      Output.WriteLine " FAILED - Check the Scripts on the FAILED Server"
      Output.WriteLine ""
    End If
      GetSubFolders strFolderName
    Next
End Sub

Output.Close

'**** The Code above is accurate **********

Function WMIDateStringToDate(sLastModified)
    WMIDateStringToDate = CDate(Mid(sLastModified, 7, 2) & "/" & _
    Mid(sLastModified, 5, 2) & "/" & Left(sLastModified, 4) _
    & " " & Mid (sLastModified, 9, 2) & ":" & _
    Mid(sLastModified, 11, 2) & ":" & Mid(sLastModified, 13, 2))
End Function

Open in new window

CompareFolderDate.txt
lasthopeAsked:
Who is Participating?
 
RobSampsonConnect With a Mentor Commented:
You can also use an array and just loop through the folder paths.

Regards,

Rob.

'Dim Path1, Path2  - Not sure this is correct way to do what I want

Const ForWriting = 2

Outputfile = "C:\TEST\Result.csv" 'Output results to csv

strComputer = "SERVERN002"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set Output = objfso.CreateTextFile(Outputfile, ForWriting, True)

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

arrFolders = Array("C:\FOLDER\INFO-1", "C:\FOLDER\INFO-2")

For Each strFolderName In arrFolders
	Output.WriteLine
	
	Set colSubfolders = objWMIService.ExecQuery _
	    ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
	        & "Where AssocClass = Win32_Subdirectory " _
	            & "ResultRole = PartComponent")
	
	For Each objFolder in colSubfolders
	    GetSubFolders strFolderName
	Next
Next

Output.Close

Sub GetSubFolders(strFolderName)
    Set colSubfolders = objWMIService.ExecQuery _
        ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
            & "Where AssocClass = Win32_Subdirectory " _
                & "ResultRole = PartComponent")

    For Each objFolder in colSubfolders
        strFolderName = objFolder.Name
        dtLastModified = WMIDateStringToDate(objFolder.LastModified)
    If Day(Now)&Month(Now)&Year(Now) = Day(dtLastModified)&Month(dtLastModified)&Year(dtLastModified) Then
        Output.WriteLine objFolder.Name
        Output.WriteLine "Last modified: " & dtLastModified & " **** Successful ****"
        Output.WriteLine ""
    Else
      Output.WriteLine objFolder.Name
      Output.WriteLine " FAILED - Check the Scripts on the FAILED Server"
      Output.WriteLine ""
    End If
      GetSubFolders strFolderName
    Next
End Sub

'**** The Code above is accurate **********

Function WMIDateStringToDate(sLastModified)
    WMIDateStringToDate = CDate(Mid(sLastModified, 7, 2) & "/" & _
    Mid(sLastModified, 5, 2) & "/" & Left(sLastModified, 4) _
    & " " & Mid (sLastModified, 9, 2) & ":" & _
    Mid(sLastModified, 11, 2) & ":" & Mid(sLastModified, 13, 2))
End Function

Open in new window

0
 
KimputerConnect With a Mentor Commented:
Assuming I interpret your request correctly and you want exactly the same operation, except on another folder, here's the code. Path1 and Path2 vars not used. Only useful if you need even more iterations.
Const ForWriting = 2

Outputfile = "C:\TEST\Result.csv" 'Output results to csv

strComputer = "SERVERN002"

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set Output = objfso.CreateTextFile(Outputfile, ForWriting, True)

Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")

strFolderName = "C:\FOLDER\INFO-1"  'Currently works to check the subfolders, but it is only one folder I need to check

Output.WriteLine

Set colSubfolders = objWMIService.ExecQuery _
    ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
        & "Where AssocClass = Win32_Subdirectory " _
            & "ResultRole = PartComponent")

For Each objFolder in colSubfolders
    GetSubFolders strFolderName
Next

'extra code starts here

strFolderName = "C:\FOLDER\INFO-2" 

Set colSubfolders = objWMIService.ExecQuery _
    ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
        & "Where AssocClass = Win32_Subdirectory " _
            & "ResultRole = PartComponent")

For Each objFolder in colSubfolders
    GetSubFolders strFolderName
Next

'extra code ends here


Sub GetSubFolders(strFolderName)
    Set colSubfolders = objWMIService.ExecQuery _
        ("Associators of {Win32_Directory.Name='" & strFolderName & "'} " _
            & "Where AssocClass = Win32_Subdirectory " _
                & "ResultRole = PartComponent")

    For Each objFolder in colSubfolders
        strFolderName = objFolder.Name
        dtLastModified = WMIDateStringToDate(objFolder.LastModified)
    If Day(Now)&Month(Now)&Year(Now) = Day(dtLastModified)&Month(dtLastModified)&Year(dtLastModified) Then
        Output.WriteLine objFolder.Name
        Output.WriteLine "Last modified: " & dtLastModified & " **** Successful ****"
        Output.WriteLine ""
    Else
    	Output.WriteLine objFolder.Name
    	Output.WriteLine " FAILED - Check the Scripts on the FAILED Server"
    	Output.WriteLine ""
    End If
    	GetSubFolders strFolderName
    Next
End Sub

Output.Close

'**** The Code above is accurate **********

Function WMIDateStringToDate(sLastModified)
    WMIDateStringToDate = CDate(Mid(sLastModified, 7, 2) & "/" & _
    Mid(sLastModified, 5, 2) & "/" & Left(sLastModified, 4) _
    & " " & Mid (sLastModified, 9, 2) & ":" & _
    Mid(sLastModified, 11, 2) & ":" & Mid(sLastModified, 13, 2))
End Function

Open in new window

0
 
lasthopeAuthor Commented:
Both solutions work just as I wanted them to so rewarding points to both. Chose to use the array code because it is what I was attempting to do on my own.

Thanks to Kimputer and RobSampson for the awsome assistance!
0
 
RobSampsonCommented:
No problem. Thanks for the grade.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.