[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-11-27
12
Medium Priority
?
2,927 Views
Last Modified: 2008-03-17
please help
0
Comment
Question by:itbabe
  • 4
  • 2
  • 2
  • +3
11 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
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!

 
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

In real business world data are crucial and sometimes data are shared among different information systems. Hence, an agreeable file transfer protocol need to be established.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Six Sigma Control Plans

830 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