• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 545
  • Last Modified:

Looking vbs algorithm to compare two windows directories for equality

Hi,

I would like a snippet of vbs code that will compare two windows directories and tell me if they are equal or not. It should compare both directories and files in the directories.

Thanks,

Chris Schene

0
Christopher Schene
Asked:
Christopher Schene
  • 8
  • 5
  • 5
  • +1
1 Solution
 
Robberbaron (robr)Commented:
the compare should be by name only or by file size / date as well ?


0
 
leakim971PluritechnicianCommented:
Hello cschene,

Googled and found that : http://myvbs.info/scripts.aspx?scriptname=SizeDiff.xml&XSL=ScriptDetail.xsl

Regards
0
 
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
Ok, I looked at this one : ": http://myvbs.info/scripts.aspx?scriptname=SizeDiff.xml&XSL=ScriptDetail.xsl
"

It does not seem that it will compare the folder tree....just one level. It has to handle the case where there is a folder hierarchy
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
comparing file size and date probably is not enough to test for equality of a file: It is possible that two files have the same size, time and date but still have diffrent content:
123456789 and 987654321 and the same size, but are different, for example.
0
 
Bill PrewCommented:
Do you really need to do this from a script, there are 3rd party utilities for exactly this purpose?  My favorite is Beyond Compare, but there are a few others I could pass along if interested.

If the directories are not equal, do you want to know why, or just a binary equal/not equal?

~bp
0
 
Robberbaron (robr)Commented:
if the content is to be check as well (or even move than just name and size) then i agree with billprew that vbs is not up to the task.

to compare content, you really have to open each file and check , or use some binary compare.
0
 
Bill PrewCommented:
Here are a few to look at, there's one other good one I'm forgetting right now.

http://www.scootersoftware.com/

http://winmerge.org/

http://www.componentsoftware.com/Products/CSDiff/highlights.htm

~bp
0
 
Robberbaron (robr)Commented:
to expand the script linked to include folder, refer http://www.tek-tips.com/viewthread.cfm?qid=1581161&page=2

adding checks for size, date and time is not a big deal either.

0
 
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
billprew: All I really need to know is if there is some difference between the directories/trees...I do not need to know what those differences are.

However, I do need to execute this programmatically from a vbs script.....I could even have the output of the compare function passed to a file and then parse out and read the file content.
0
 
Bill PrewCommented:
Are these all text files, or will their be binary files as well?

~bp
0
 
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
the files are binary and text
0
 
Bill PrewCommented:
If you truly want a VB native solution then it's a fair amount of work.  From your perspective, if you had a function called FolderCompare(dir1, dir2) that returned a true or false indicating if dir1 was congruent to dir2 then that's all you need?

Inside FolderCompare() we need to worry about:
- all files and subfolders must exist in both parent folders
- no extra files or folders can exist in either parent directory
- date stamps on files must match (which stamps, created, changed, accessed?)
- date stamps on subfolders must match (which stamps, created, changed, accessed?)
- file sizes must match
- file content must match in a binary mode
- anything else?

This could probably be done in less code in a BAT script, but that wouldn't work for you?  What if we called

~bp
0
 
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
billprew,

Yes, it is possible to use a bat file if the bat file can somehow return a result code to my .vbs script. Also, the  .bat file could output a result to a file which I could then read from my vbs script.

So, yes, I think it is possible to use a .bat file assuming the .bat file could place a code "NOMATCH" in a known file in the event that there is a difference and "MATCH" if they are the same.  I think you are on the right track. I know .vbs better than I do .bat files. Do you have a  batch file that would accomplish this comparison?

I actually use this same approach creating subversion branches from a DOS command line and I read the "SUCCESS" string and reversion number from a text file.
0
 
leakim971PluritechnicianCommented:
You can run the command line FC from VBScript :
http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/fc.mspx?mfr=true

No need to download it on Windows 7 (I think Vista, 2008/R2 have it too)

fc /b file1.bin file2.bin

Open in new window

0
 
Robberbaron (robr)Commented:
I was working on this and came up with something very much along the lines of billprew & leakim971 when I logged in to post.

attached anyway. seems to work generally.

Option explicit
'EE Robberbaron
'modified from '
'...http://myvbs.info/scripts.aspx?scriptname=SizeDiff.xml&XSL=ScriptDetail.xsl'
'... http://ss64.com/nt/fc.html'

Dim Folder1, Folder2, output, FSO, oFolder1, oFolder2, bOk

Folder1="C:\EE\MyT\123453"
Folder2="C:\EE\MyT\123456"


set fso=createobject("Scripting.fileSystemObject")
set ofolder1=fso.getfolder(folder1)
set ofolder2=fso.getfolder(folder2)

Dim WshShell
set WshShell = CreateObject("wscript.Shell")



bOk = CompareFolderContents(oFolder1, oFolder2)
If bOk = True Then
	WScript.Echo "---- Folders match  ----"
 Else
 	WScript.Echo "***** Folders dont match *****"
End if

WScript.Quit
'-------------------------'
Function CompareFolderContents(oFldA, oFldB)
	Dim oFilesA,  iFiles
	Dim oFileA, oFileB, bOk, cmdz
	Dim oSubFldA, oSubFldB
	
	'easy compares'
	'folder file count'
	If oFldA.Files.Count<> ofldB.Files.Count Then
		CompareFolderContents=False
		WScript.Echo "folder file count		"
		Exit function
	End If
	'folder subfolder count'
	If oFldA.SubFolders.Count<>ofldb.SubFolders.Count Then
		CompareFolderContents=False
		WScript.Echo "folder subfolder count"
		Exit function
	End If	

	'folder size'
	If oFldA.size <> oFldB.Size Then
		CompareFolderContents=False
		WScript.Echo "folder size"
		Exit function
	End If	
	
	'check each file'
	set oFilesA=ofldA.Files
	for each ofileA in ofilesA
	    iFiles=iFiles+1
	    if FSO.FileExists(oFldB.Path & "\" & ofileA.Name) then
	        'compare size'
	        set ofileB=FSO.GetFile(oFldB.Path & "\" & ofileA.Name)
	        
	        if ofileB.Size <> ofileA.Size then
				CompareFolderContents=False
				WScript.Echo "file compare size"
				Exit function
	        end If
	        'compare date time stamp'
	        If oFileB.DateLastModified <> oFileA.DateLastModified Then
				CompareFolderContents=False
				WScript.Echo "file compare date"
				Exit function	        
	        End if
	    	
	    	'now do binary compare'
	    	cmdz = "FC " & oFldB.Path & "\" & ofileA.Name & " " & oFldA.Path & "\" & ofileA.Name & " | FIND ""FC: no dif"" > nul"
	    	WScript.Echo cmdz
			bOk = WshShell.Run (cmdz,0,True)
			If bOk = 1 Then
				CompareFolderContents=False
				WScript.Echo "file compare binary"
				Exit function	        			
			End if
	    end if
	Next
	Set oFileA=Nothing: Set oFileB=Nothing
		
	'files are ok so recurse the subfolders'
	For Each oSubFldA In oFldA.SubFolders
		If FSO.FolderExists(ofldB.Name & "\" & osubfldA.Name) Then
			'subfolder name match
			oSubFldB = FSO.GetFolder(ofldB.Name & "\" & osubfldA.Name)
		
			bOk = CompareFolderContents( oSubFldA, oSubFldB)
			If Not bOk Then
				CompareFolderContents=False
				Exit function	        			
			End if
				
		End if
	Next

	
	CompareFolderContents=true
End Function

Open in new window

0
 
Robberbaron (robr)Commented:
a couple of corrections and maybe quicker.

if the Folder size test gives incorrect results, delete that section.  

Option explicit
'EE Robberbaron
'v2  3/Jan/2010
'modified from '
'...http://myvbs.info/scripts.aspx?scriptname=SizeDiff.xml&XSL=ScriptDetail.xsl'
'... http://ss64.com/nt/fc.html'

Dim Folder1, Folder2, output, FSO, oFolder1, oFolder2, bOk

Folder1="C:\EE\MyT\123453"
Folder2="C:\EE\MyT\123456"


set fso=createobject("Scripting.fileSystemObject")
set ofolder1=fso.getfolder(folder1)
set ofolder2=fso.getfolder(folder2)

Dim WshShell
set WshShell = CreateObject("wscript.Shell")



bOk = CompareFolderContents(oFolder1, oFolder2)
If bOk = True Then
	WScript.Echo "---- Folders match  ----"
 Else
 	WScript.Echo "***** Folders dont match *****"
End if

WScript.Quit
'-------------------------'
Function CompareFolderContents(oFldA, oFldB)
	Dim oFilesA,  iFiles
	Dim oFileA, oFileB, bOk, cmdz , sTmp
	Dim oSubFldA, oSubFldB
	
	'easy compares'
	'folder file count'
	If oFldA.Files.Count<> ofldB.Files.Count Then
		CompareFolderContents=False
		WScript.Echo "folder file count		"
		Exit function
	End If
	WScript.Echo "folder file count	OK	"
	
	'folder subfolder count'
	If oFldA.SubFolders.Count<>ofldb.SubFolders.Count Then
		CompareFolderContents=False
		WScript.Echo "folder subfolder count"
		Exit function
	End If	
	WScript.Echo "folder subfolder count OK"

	'folder size  --- seems to give spurious results'
	If oFldA.size <> oFldB.Size Then   
		CompareFolderContents=False   
		WScript.Echo "folder size"    
		Exit function     
	End If	    
	WScript.Echo "folder size OK"
	
	'check each file'
	set oFilesA=ofldA.Files
	for each oFileA in ofilesA
	    iFiles=iFiles+1
	    sTmp = oFldB.Path & "\" & ofileA.Name
	    if FSO.FileExists(sTmp) then
	        'compare size'
	        set ofileB=FSO.GetFile(sTmp)
	        
	        if ofileB.Size <> ofileA.Size then
				CompareFolderContents=False
				WScript.Echo "file compare size"
				Exit function
	        end If
	        WScript.Echo "file compare size OK"
	        
	        'compare date time stamp'
	        If oFileB.DateLastModified <> oFileA.DateLastModified Then
				CompareFolderContents=False
				WScript.Echo "file compare date"
				Exit function	        
	        End if
	    	WScript.Echo "file compare date OK"
	    	
	    	'now do binary compare'
	    	cmdz = "FC " & sTmp & " " & oFldA.Path & "\" & ofileA.Name & " | FIND ""FC: no dif"" > nul"
	    	'WScript.Echo cmdz'
			bOk = WshShell.Run (cmdz,0,True)
			If bOk = 1 Then
				CompareFolderContents=False
				WScript.Echo "file compare binary"
				Exit function	        			
			End If
			WScript.Echo "file compare binary OK"
	    end if
	Next
	Set oFileA=Nothing: Set oFileB=Nothing
		
	'files are ok so recurse the subfolders'
	For Each oSubFldA In oFldA.SubFolders
		sTmp = ofldB.Path & "\" & oSubFldA.Name
		If FSO.FolderExists(sTmp) Then
			'subfolder name match
			Set oSubFldB = FSO.GetFolder(sTmp)
		    		    
			bOk = CompareFolderContents( oSubFldA, oSubFldB)
			If Not bOk Then
				WScript.Echo "Subfolder FAIL"
				CompareFolderContents=False
				Exit function	        			
			End If
			WScript.Echo "Subfolder OK"
		 Else
	 		WScript.Echo "Folder names mismatch"
	 		CompareFolderContents=False
			Exit function		
		End if
	Next

	
	CompareFolderContents=true
End Function

Open in new window

0
 
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
Robber barron: I am testing your solution. Thanks.
0
 
Bill PrewCommented:
>> cschene

Let me know how you make out, I won't fashion a BAT approach unless you run into problems with the VBS approach.

~bp
0
 
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
works well!
0
 
Christopher ScheneSystem Engineer/Software EngineerAuthor Commented:
Robber Barron: Thanks for your help. Your .vbs solution works well. Great job.

To the rest of you, thanks for your input!

Happy new year!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 8
  • 5
  • 5
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now