Compare Folder Script

Today I created a script to compare the contents of two folder including its subfolders. It’s a simple solution if you don’t want to buy folder comparator software, but the script will not compare the content of the file, it only compares the file size and last modified date.

compare-script

Click read more to see the script

The Script

' -------------------------------
' Name    : Compare Folder Script
' Version : 1.0
' Author  : Lucky
' -------------------------------

WScript.Echo "Compare Folder Script v1.0 by Lucky"
WScript.Echo "==================================="
WScript.Echo

Dim args
Set args = WScript.Arguments

Dim folder1
Dim folder2
Dim hideok

If args.Count<>3 Then
    WScript.Echo "Usage: cscript compare.vbs [folder1] [folder2] [hideok]"
    WScript.Quit()
Else
    folder1=args(0)
    folder2=args(1)
    If LCase(args(2)) = "yes" Then
        hideok = True
    Else
    	  hideok = False
    End If
    WScript.Echo "Compare """ & args(0) & """ and """ & args(1) & """:"
    WScript.Echo
End If

'Start compare
Function comparefolder(folder1,folder2,hideok)
	Dim fso,f1

	Set fso = CreateObject("Scripting.FileSystemObject")
	Set f1 = fso.GetFolder(folder1)
	Set f2 = fso.GetFolder(folder2)

	'Check on first folder
	Set colFiles = f1.Files
	For Each objFile in colFiles
	    cmpFileName = fso.BuildPath(f2.Path, objFile.Name)

	    If fso.FileExists(cmpFileName) Then
	        Set objFile2 = fso.GetFile(cmpFileName)
                IsOk= True
	        If objFile.Size <> objFile2.Size Then
	            WScript.Echo objFile.Path + ": Different file size (" + CStr(objFile.Size) + " and " + CStr(objFile2.Size) + ")"
                    IsOk = False
	        End If
	        If objFile.DateLastModified <> objFile2.DateLastModified Then
	            WScript.Echo objFile.Path + ": Different modified date (" + CStr(objFile.DateLastModified) + " and " + CStr(objFile2.DateLastModified) + ")"
                    IsOk = False
                End If
	        If Not hideok And IsOk Then
	            WScript.Echo objFile.Path + ": OK"
	        End If
	    Else
	        WScript.Echo cmpFileName + ": file didn't exist"
	    End If
	Next

	'Check on second folder
	Set colFiles = f2.Files
	For Each objFile in colFiles
	    cmpFileName = fso.BuildPath(f1.Path, objFile.Name)

	    If Not fso.FileExists(cmpFileName) Then
	        WScript.Echo cmpFileName + ": file didn't exist"
	    End If
	Next

	'Check on first folder sub
	Set colFolders = f1.SubFolders
	For Each objFolder in colFolders
	    cmpFolderName = fso.BuildPath(f2.Path, objFolder.Name)

	    If fso.FolderExists(cmpFolderName) Then
          Call comparefolder(objFolder.Path,cmpFolderName,hideok)
	    Else
	        WScript.Echo cmpFolderName + ": folder didn't exist"
	    End If

	Next

	'Check on second folder sub
	Set colFolders = f2.SubFolders
	For Each objFolder in colFolders
	    cmpFolderName = fso.BuildPath(f1.Path, objFolder.Name)

	    If Not fso.FolderExists(cmpFolderName) Then
	        WScript.Echo cmpFolderName + ": folder didn't exist"
	    End If

	Next
End Function

Call comparefolder(folder1,folder2,hideok)

WScript.Quit()

Usage
Copy the script above and save it to file named “compare.vbs”.

Syntax:
cscript compare.vbs [folder1] [folder2] [hideok]

Examples:
Compare folder named "folder1" to "folder2" and hide indentical file.
cscript compare.vbs "folder1" "folder2" yes

Compare folders and save the report to file
cscript compare.vbs "folder1" "folder2" no > differences.txt


No related posts.

Tags: ,

2 Responses to “Compare Folder Script”

  1. Thankz a Lot! Really Helpful… ^^

  2. You’re welcome! :)

Leave a Reply