Link to home
Start Free TrialLog in
Avatar of itbabe
itbabe

asked on

vbscript : is their a way to compare the contents of 2 folders ?

please help
Avatar of hclgroup
hclgroup

Could you be a little bit more specific!
Here is a little script I just wrote up for you.  It will collect all the file names in 2 user defined folders.  It will then compare the file names to the other folder and list the unique file names.  All you need to do is create a .vbs file and copy and paste the code into it.  Then modify the strFolder1 and strFolder2 variables to the location of the folders you want to compare files.

'Set Objects
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Get Path to Script
scriptPath = Left(WScript.ScriptFullName,InstrRev(WScript.ScriptFullName,".") -1)

'Create Log File
logName = scriptPath & ".txt"
Set logFile = objFSO.CreateTextFile(logName, True)
logFile.WriteLine WScript.ScriptName & vbCrLf & "--------------------"

'Set Folders to Compare
strFolder1 = "C:\Scripts"
strFolder2 = "C:\Scripts2"

'Check if Folders Exist
If Not objFSO.FolderExists(strFolder1) Then
    Wscript.Echo "Error - '" & strFolder1 & "' Does Not Exist" & vbCrLf & "Quitting Script!"
    Wscript.quit
Elseif Not objFSO.FolderExists(strFolder2) Then
    Wscript.Echo "Error - '" & strFolder2 & "' Does Not Exist" & vbCrLf & "Quitting Script!"
    Wscript.quit
End If

'Build Folder1 File List
Set objFolder1 = objFSO.GetFolder(strFolder1)
Set colFiles1 = objFolder1.Files
Dim arrFile1()
i = 0
For Each File1 in colFiles1
    ReDim Preserve arrFile1(i)
      arrFile1(i) = File1.Name
      i = i + 1
Next

'Build Folder2 File List
Set objFolder2 = objFSO.GetFolder(strFolder2)
Set colFiles2 = objFolder2.Files
Dim arrFile2()
i = 0
For Each File2 in colFiles2
    ReDim Preserve arrFile2(i)
      arrFile2(i) = File2.Name
      i = i + 1
Next

'Output Unique Files in Folder 1
logFile.WriteLine vbCrLf & strFolder1 & "- Files Not Matched in Folder 2" & vbCrLf & "--------------------"
match = 0
For i = 0 to UBound(arrFile1)
    For j = 0 to UBound(arrFile2)
          If arrFile1(i) = arrFile2(j) Then
                match = 1
          End If
      Next
    If match = 0 Then
          logFile.WriteLine arrFile1(i)
      End If
      match = 0
Next

'Output Unique Files in Folder 2
logFile.WriteLine vbCrLf & strFolder2 & "- Files Not Matched in Folder 1" & vbCrLf & "--------------------"
match = 0
For i = 0 to UBound(arrFile2)
    For j = 0 to UBound(arrFile1)
          If arrFile2(i) = arrFile1(j) Then
                match = 1
          End If
      Next
    If match = 0 Then
          logFile.WriteLine arrFile2(i)
      End If
      match = 0
Next

'Finished Script
Wscript.Echo "Finished Script!"
I forgot to mention.  It will output the unique results to a file with the same name and location of the .vbs file but using a .txt extension.

Enjoy
Assuming that you have two dir to compare named C:\TEST1 and C:\TEST2, you could create a .bat file to do this.
Open NotePad.exe and type the following line
Dir c:\TEST1 /a > C:\test1.txt
Dir c:\TEST2 /a > C:\test2.txt
Rename the file as "c:\check.bat" for example.
This will create 2 file test1.txt and test2.txt in c:\

Go to see and check the content of 2 files above manually or use some code to do the task.
Avatar of itbabe

ASKER

Hi deadite,

Your script was what I was looking for.
When executing it,  I however get the following error.
Line : 50
Char : 1
Error : Subscript out of range: 'Ubound'
Code : 800A0009
Source: Vbscript Runtime Error
This is caused by there being no files in the folders defined in these lines:

strFolder1 = "C:\Scripts"
strFolder2 = "C:\Scripts2"


After you make sure you modified these to your locations... check to see if there are any files in the folders.  If there are none, you will receive that error.  As a test, drop a file in the folder..and it should work..  I don't have time to correct the script right now.  If you want it modified to include this fix, let me know and I'll fix it up tonight
ASKER CERTIFIED SOLUTION
Avatar of deadite
deadite
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of itbabe

ASKER

Hi deadite
It is working for files in the "highest-level folder" but not on files in subfolders.
How can I achieve that ?
Why refund? The links I gave answer his question perfectly.

How to scan through a directory tree
http://www.winscripter.com/WSH/FileIO/75.aspx
How to read the properties of a file
http://www.winscripter.com/WSH/FileIO/68.aspx
Hi,

I tried to open the links that alkisg provided but when I open them, I have an error.

Can someone give a vbs code that compares between the contents of two folders while the two folders have subfolders?