Photo Gallery
Member List
Search
Calendars
FAQ
Ticket List
Log Out
Forums
Register
Login
My Profile
Inbox
Address Book
My Subscription
My Forums
file comparison script
Logged in as: Guest
arrSession:exec spGetSession 2,16,26907
Active Users: There are
0
members and
0
guests.
Users viewing this topic: none
Printable Version
All Forums
>>
[Scripting]
>>
Post a VBScript
>> file comparison script
Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page:
[1]
Login
Message
<< Older Topic
Newer Topic >>
file comparison script -
10/13/2005 8:13:31 AM
kirrilian
Posts: 628
Score: 3
Joined: 3/15/2005
From:
Status:
offline
this script will take a list of servers and paths and check the files with wmi to see if they are the same
'========================================================================== ' ' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 3.1 ' ' NAME: Remote File Comparison Script ' ' AUTHOR: Kirrilian ' Date : 2/28/2005 ' ' COMMENT: ' usage: cscript filecheck.vbs [option] [input file] ' Eg. cscript filecheck.vbs -all input.txt ' This script will compare files from a source server/path With remote server/paths ' It will compare the source With multiple targets if you wish. This ' Is useful for checking file deployments on multiple servers. ' The input file has the following syntax and order: Ex. ' server1,d:\temp\temp1 <<<< source server and path ' server2,c:\temp\temp2 <<<< target servers and paths ' server3,d:\temp " " " ' server4,e:\temp\temp3\temp4 " " " ' You can specify as many targets as you wish with any path. ' Output: A summary text file with the date in the file name ' A detailed output file in csv format. These will be in the ' same directory you run the script. ' Ex. filecheck_Summary-3-11-2005@1200.txt ' filecheck_Detail-3-11-2005@1200.csv '========================================================================== Set objArgs = WScript.Arguments Set fso = CreateObject("Scripting.FileSystemObject") Set objDateDictionary = CreateObject("Scripting.Dictionary") Set objLostDictionary = CreateObject("Scripting.Dictionary") 'for summary 'missing Set objSumDictionaryM = CreateObject("Scripting.Dictionary") 'different Set objSumDictionaryD = CreateObject("Scripting.Dictionary") 'extra Set objSumDictionaryE = CreateObject("Scripting.Dictionary") 'create our dynamic 2d Array 'columns: machine,src file and date,missing,extra,different(Date) ReDim Preserve outArray(5, 0) ReDim Preserve targetServers(0) Dim arrcount, outputType, Mcount, Dcount, Ecount, currDate, modDate, InputFile, txtStreamIn Dim targetPath,sourcePath,dictPath Mcount = 0 Dcount = 0 Ecount = 0 'Call input section argNum objArgs.Count ' ********* input section *********** Sub argNum(num) 'testing for command line arguments Select Case num Case 1 usage() Case 2 'read servers,paths from input file argTest objArgs.Item(0) InputFile = objArgs.Item(1) If fso.FileExists(InputFile) Then Set txtStreamIn = fso.OpenTextFile(InputFile) WScript.Echo "Using " & InputFile & " as the input." Else WScript.Echo "Input file doesn't exist. Try again." usage() End If Case Else usage() End Select End Sub 'argNum Sub argTest(arg) ' Select Case arg Case "-m" outputType = "missing" Case "-d" outputType = "different" Case "-e" outputType = "extra" Case "-all" outputType = "all" Case Else usage() End Select End Sub 'argTest Sub usage() WScript.Echo "usage: cscript esd_filecheck.vbs [option] [input file]" & vbNewLine _ & "Options: -m Is For files missing on the target machines," & vbNewLine _ & vbTab & " -e is for files extra on the target machines," & vbNewLine _ & vbTab & " -d is for files with a different modified Date on the target machines," & vbNewLine _ & vbTab & " -all is to output all of the tests." & vbNewLine _ & vbTab & " Read the script header for how To format the input file." WScript.Quit End Sub 'usage '**************** end input section ***************** '**************** output section *************** MyDate = Replace(Date, "/", "-") & "@" & Left(Replace(Time, ":", ""), 3) & Right(Time, 2) 'MyDate = Replace(Date, "/", "-") DOutputFile = "./filecheck_Detail-" & mydate & ".csv" SOutputFile = "./filecheck_Summary-" & mydate & ".txt" Set DtxtStreamOut = fso.OpenTextFile(DOutputFile, 2, True) Set StxtStreamOut = fso.OpenTextFile(SOutputFile, 2, True) lineNum = 0 Do while Not (txtStreamIn.AtEndOfStream) input = Split(txtStreamIn.ReadLine, ",") If lineNum = 0 Then sourceServer = input(0) sourcePath = input(1) runSource sourceServer,sourcePath Else targetServer = input(0) targetPath = input(1) runTarget targetServer,targetPath targetServers(lineNum - 1) = targetServer ReDim Preserve targetServers(lineNum) End If lineNum = lineNum + 1 Loop Sub runSource(sServer,sPath) 'load up the source WScript.Echo "starting source query On " & sServer & " using path " & sPath loadSource sServer,sPath loadRecursiveSource sServer,sPath WScript.Echo "finished source query" End Sub Sub runTarget(tServer,tPath) 'test remote machines, one at a Time WScript.Echo vbNewLine WScript.Echo "starting remote query For " & tserver & " using path " & tPath 'checks files in the path inFolder tserver,tpath 'checks files recursively in the path recurseFolders tserver,tPath 'checks For files missing on the target findLost tserver 'output missing files outputLost tserver WScript.Echo "finished remote query for " & tserver 'tally for summary addexecSum tserver,"m",Mcount addexecSum tserver,"e",Ecount addexecSum tserver,"d",Dcount 'clean up resetCounters End Sub 'loadTarget WScript.Echo vbNewLine 'output files that dont match or werent found outputArray() WScript.Echo vbNewLine 'put the summary at the bottom so its easier to see outputexecSum 'clear hash tables cleanup() '**********begin functions**************** ' check target servers Sub inFolder(fnComputer, fnPath) 'Fix path For wmi folderRename = Replace(fnPath,"\","\\") folderRename = Mid(folderRename,3) drive = Left(fnPath, 2) Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & fnComputer & "\root\cimv2") Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where drive = '"& drive &"' and Path = '" & folderRename & "\\'") For Each objFile In colFiles path = lcase(objFile.Path & objFile.FileName & "." & objFile.Extension) wscript.echo path 'path = Replace(path, lcase(mid(fnPath,3)), "") pathArr = Split(path, "\") size = UBound(pathArr) path = "\" & pathArr(size) wscript.echo path If objDateDictionary.exists(path) Then 'check To see If target file exists In the source And add entry In temp storage if so If Not objLostDictionary.exists(path) Then objLostDictionary.add path,"True" End If 'wscript.echo Left(objFile.LastModified,(Len(objFile.LastModified)) - 13) & vbtab & objDateDictionary.Item(path) If objDateDictionary.Item(path) = Left(objFile.LastModified,(Len(objFile.LastModified)) - 13) Then 'WScript.Echo "its a infolder match, jim." Else If outputType = "different" Or outputType = "all" Then WScript.Echo path & " modified Date doesnt match!!" Dcount = Dcount + 1 'make the dates human readable, darn humans! prettyCDate objDateDictionary.Item(path) prettyMDate Left(objFile.LastModified,(Len(objFile.LastModified)) - 13) add2Array fnComputer,sourcePath & Path & " - " & currDate,"","",modDate End If End If Else If outputType = "extra" Or outputType = "all" Then WScript.Echo "doesnt exist Infolder on target, adding To output: " & fnComputer & path Ecount = Ecount + 1 'add2Array fnComputer,path,"","X","" add2Array fnComputer,"","",targetPath & path,"" End If End If Next End Sub 'inFolder ' inFolder and recurseFolders gives a recursive total of a directory Function recurseFolders(fnComputer,fnPath) Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & fnComputer & "\root\cimv2") 'use the functionfolder object in wmi to get all the functionfolders and recurse each individually Set colfunctionfolders = objWMIService.ExecQuery _ ("Associators of {Win32_Directory.Name='" & fnPath & "'} " _ & "Where AssocClass = Win32_Subdirectory " _ & "ResultRole = PartComponent") For Each objFolder in colfunctionfolders 'Fix path For wmi folderRename = Replace(objFolder.Name,"\","\\") folderRename = Mid(folderRename,3) drive = Left(fnPath, 2) Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where drive = '"& drive &"' and Path = '" & folderRename & "\\'") For Each objFile In colFiles path = LCase(objFile.Path & objFile.FileName & "." & objFile.Extension) path = Replace(path, lcase(Mid(fnPath,3)), "") 'pathArr = Split(path, "\") 'size = UBound(pathArr) 'path = "\" & pathArr(size - 1) wscript.echo path If objDateDictionary.exists(path) Then 'check To see If target file exists In the source And add entry In temp storage if so If Not objLostDictionary.exists(path) Then objLostDictionary.add path,"True" End If If objDateDictionary.Item(path) = Left(objFile.LastModified,(Len(objFile.LastModified)) - 13) Then 'WScript.Echo "its a recursive match, jim." Else If outputType = "different" Or outputType = "all" Then WScript.Echo path & " modified Date doesnt match!!" Dcount = Dcount + 1 'make the dates human readable, darn humans! prettyCDate objDateDictionary.Item(path) prettyMDate Left(objFile.LastModified,(Len(objFile.LastModified)) - 13) add2Array fnComputer,sourcePath & path & " - " & currDate,"","",modDate End If End If Else If outputType = "extra" Or outputType = "all" Then WScript.Echo "doesnt exist recursive In target, adding To output: " & fnComputer & path Ecount = Ecount + 1 'add2Array fnComputer,path,"","X","" add2Array fnComputer,"","",targetPath & path,"" End If end If Next 'call Function over And over until all folders are recursed recurseFolders fnComputer,objFolder.Name Next End Function 'recurseFolders Sub add2Array(machine,file,missing,extra,diff) ReDim Preserve outArray(5, arrcount) outArray(0,arrcount) = machine outArray(1,arrcount) = file outArray(2,arrcount) = missing outArray(3,arrcount) = extra outArray(4,arrcount) = diff arrcount = arrcount + 1 End Sub 'add2Array Sub outputArray() WScript.Echo "************ detailed output **************" printOut "machine,src file and date,missing,extra,different(Date)" For j = 0 To arrcount - 1 printOut outArray(0,j) & "," & outArray(1,j)& "," & outArray(2,j) _ & "," & outArray(3,j)& "," & outArray(4,j) Next End Sub 'outputArray Sub outputLost(oserver) If outputType = "missing" Or outputType = "all" Then For Each obj In objLostDictionary If objLostDictionary(obj) = "False" Then add2Array oserver,sourcePath & obj,"X","","" Mcount = Mcount + 1 End if Next End If 'clean up lost dictionary for next run b = objLostDictionary.removeall End Sub 'outputLost Sub cleanup() a = objDateDictionary.removeall b = objLostDictionary.removeall c = objSumDictionaryM.removeall d = objSumDictionaryE.removeall e = objSumDictionaryD.removeall Set objDateDictionary = Nothing Set objLostDictionary = Nothing Set DtxtStreamOut = Nothing Set StxtStreamOut = Nothing erase targetServers erase outArray End Sub 'cleanup Sub loadSource(fnComputer, fnPath) WScript.Echo fnPath folderRename = Replace(fnPath,"\","\\") folderRename = Mid(folderRename,3) drive = Left(fnPath, 2) Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & fnComputer & "\root\cimv2") Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where drive = '"& drive &"' and Path = '" & folderRename & "\\'") For Each objFile In colFiles path = lcase(objFile.Path & objFile.FileName & "." & objFile.Extension) path1 = Replace(path, lcase(mid(fnPath,3)), "") wscript.echo path1 objDateDictionary.add path1,Left(objFile.LastModified,(Len(objFile.LastModified)) - 13) Next End sub 'loadSource Sub loadRecursiveSource(fnComputer, fPath) Set objWMIService = GetObject("winmgmts:" _ & "{impersonationLevel=impersonate}!\\" & fnComputer & "\root\cimv2") 'use the functionfolder object in wmi to get all the functionfolders and recurse each individually Set colfunctionfolders = objWMIService.ExecQuery _ ("Associators of {Win32_Directory.Name='" & fPath & "'} " _ & "Where AssocClass = Win32_Subdirectory " _ & "ResultRole = PartComponent") For Each objFolder In colfunctionfolders 'Fix path For wmi folderRename = Replace(objFolder.Name,"\","\\") folderRename = Mid(folderRename,3) drive = Left(fPath, 2) Set colFiles = objWMIService.ExecQuery("Select * from CIM_DataFile where drive = '"& drive &"' and Path = '" & folderRename & "\\'") For Each objFile In colFiles path = lcase(objFile.Path & objFile.FileName & "." & objFile.Extension) path1 = Replace(path, lcase(Mid(fPath,3)), "") wscript.echo path1 If Not objDateDictionary.exists(path1) Then objDateDictionary.add path1,Left(objFile.LastModified,(Len(objFile.LastModified)) - 13) End if Next loadRecursiveSource fnComputer,objFolder.Name Next End Sub 'loadRecursiveSource Sub findLost(fserver) For Each obj In objDateDictionary If Not objLostDictionary.exists(obj) Then objLostDictionary.add obj,"False" WScript.Echo obj & " is missing from " & fserver End If Next End Sub 'findLost Sub printOut (data) WScript.Echo data DtxtStreamOut.writeline data End Sub 'printOut sub addexecSum(aserver,dType,data) 'dType is the type of output 'data is the totalled amount for that type Select Case dType Case "m" If Not objSumDictionaryM.exists(aserver) Then objSumDictionaryM.add aserver,data End If Case "e" If Not objSumDictionaryE.exists(aserver) Then objSumDictionaryE.add aserver,data End If Case "d" If Not objSumDictionaryD.exists(aserver) Then objSumDictionaryD.add aserver,data End If Case Else WScript.Echo "Huh?" End Select End Sub 'execSum Sub resetCounters() Mcount = 0 Dcount = 0 Ecount = 0 End Sub 'resetCount Sub outputexecSum() endNum = UBound(targetServers) printOutES "*********************** Summary *************************" printOutES "This is the summary for query type *" & outputType & "*," & vbnewline _ & "using server " & sourceServer & " with path " & sourcePath & " as the source." & vbNewLine printOutES "Server" & vbTab & vbTab & vbTab & "Missing" & vbTab & vbTab & "Different" & vbTab & "Extra" For i = 0 To (endNum - 1) 'server In targetServers server = targetServers(i) printOutES "--------------------------------------------------------------" printOutES server & vbTab & vbTab & objSumDictionaryM.Item(server) & vbTab _ & vbTab & objSumDictionaryD.Item(server)& vbTab & vbTab & objSumDictionaryE.Item(server) Next printOutES "--------------------------------------------------------------" End Sub 'outputexecSum Sub printOutES (data) WScript.Echo data StxtStreamOut.writeline data End Sub 'printOut Sub prettyCDate(inDate) theYear = Left(inDate, 4) theMo = Mid(inDate, 5, 2) theDay = Mid (inDate, 7, 2) theHour = Mid(inDate, 9, 2) theMinute= Mid(inDate, 11, 2) 'theSecond= Mid(inDate, 13, 2) currDate = theMo & "/" & theDay& "/" &theYear & " @ " & theHour & ":" & theMinute '& ":" & theSecond End Sub 'prettyCDate Sub prettyMDate(inDate) theYear = Left(inDate, 4) theMo = Mid(inDate, 5, 2) theDay = Mid (inDate, 7, 2) theHour = Mid(inDate, 9, 2) theMinute= Mid(inDate, 11, 2) 'theSecond= Mid(inDate, 13, 2) modDate = theMo & "/" & theDay& "/" &theYear & " @ " & theHour & ":" & theMinute '& ":" & theSecond End Sub 'prettyMDate
_____________________________
Have you searched
here
?
VBScript Fundamentals
My Site
Post #: 1
RE: file comparison script -
11/6/2005 12:08:07 AM
Cybertwister
Posts: 20
Score: 0
Joined: 11/6/2005
Status:
offline
great
(in reply to
kirrilian
)
Post #: 2
If you found our site useful please link to us
<a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>
.
All Forums
>>
[Scripting]
>>
Post a VBScript
>> file comparison script
Page:
[1]
Jump to:
Select a Forum
All Forums
----------------------
[Welcome]
- - Forum Rules
- - Test Posting Messages
- - New Member Area/Introduction
[Scripting]
- - WSH & Client Side VBScript
- - WSH & Client Side VBScript Tutorial
- - Post a VBScript
- - Windows PowerShell
- - ASP
- - ASP.NET
- - Windows Script Components
[General Forum]
- - Other Programming/Scripting Languages
- - Suggestions & Feedback
- - Off-Topic Lounge
New Messages
No New Messages
Hot Topic w/ New Messages
Hot Topic w/o New Messages
Locked w/ New Messages
Locked w/o New Messages
Post New Thread
Reply to Message
Post New Poll
Submit Vote
Delete My Own Post
Delete My Own Thread
Rate Posts
Forum Software ©
ASPPlayground.NET
Advanced Edition
2.5.5 ANSI