Solved

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

Posted on 2006-11-27
12
2,177 Views
Last Modified: 2008-03-17
please help
0
Comment
Question by:itbabe
  • 4
  • 2
  • 2
  • +3
12 Comments
 
LVL 4

Expert Comment

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

Expert Comment

by:killbrad
ID: 18020237
0
 
LVL 9

Expert Comment

by:alkisg
ID: 18022804
0
 
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
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 500 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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
MaxSpan challenge 9 67
bigHeights  challenge 13 56
base64 decode encode 12 98
How can i compile this github project?? 2 44
Here we come across an interesting topic of coding guidelines while designing automation test scripts. The scope of this article will not be limited to QTP but to an overall extent of using VB Scripting for automation projects. Introduction Now…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now