?
Solved

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

Posted on 2006-11-27
12
Medium Priority
?
2,692 Views
Last Modified: 2008-03-17
please help
0
Comment
Question by:itbabe
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
  • 2
  • +3
12 Comments
 
LVL 4

Expert Comment

by:hclgroup
ID: 18020094
Could you be a little bit more specific!
0
 
LVL 9

Expert Comment

by:alkisg
ID: 18022804
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 8

Expert Comment

by:deadite
ID: 18025652
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!"
0
 
LVL 8

Expert Comment

by:deadite
ID: 18025658
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
0
 
LVL 3

Expert Comment

by:taycuong76
ID: 18026442
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.
0
 

Author Comment

by:itbabe
ID: 18026482
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
0
 
LVL 8

Expert Comment

by:deadite
ID: 18029477
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
0
 
LVL 8

Accepted Solution

by:
deadite earned 2000 total points
ID: 18032404
Here is the updated script.  It can handle having 0 items in each folder now:

'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:\Documents and Settings\flare\My Documents\scripts\test1"
strFolder2 = "C:\Documents and Settings\flare\My Documents\scripts\test2"

'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 each item1 in arrFile1
    For each item2 in arrFile2
        If item1 = item2 Then
            match = 1
        End If
    Next
    If match = 0 Then
        logFile.WriteLine item1
    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 each item2 in arrFile2
    For each item1 in arrFile1
        If item2 = item1 Then
            match = 1
        End If
    Next
    If match = 0 Then
        logFile.WriteLine item2
    End If
    match = 0
Next

'Finished Script
Wscript.Echo "Finished Script!"
0
 

Author Comment

by:itbabe
ID: 18035353
Hi deadite
It is working for files in the "highest-level folder" but not on files in subfolders.
How can I achieve that ?
0
 
LVL 9

Expert Comment

by:alkisg
ID: 18290335
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
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A short article about problems I had with the new location API and permissions in Marshmallow
If you’re thinking to yourself “That description sounds a lot like two people doing the work that one could accomplish,” you’re not alone.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Simple Linear Regression

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question