Solved

Folder Comparison

Posted on 2006-10-19
25
379 Views
Last Modified: 2010-04-30
Hi,

we were using a "folder comparison tool" along with a inhouse application. But of late, the business users have asked the "folder comparison" functionality to be part of the inhouse application itself.

so can someone please provide me the code as to how we can achieve this in VB6?

basically given two folders, we need to recursively list out all the "folders", "sub folders", "files" on the left hand side for the first selected folder. The corresponding details of the second folder must be shown on the right hand side. we plan to use "list view control" for this purpose.
0
Comment
Question by:expertfan
[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
  • 12
  • 11
  • 2
25 Comments
 

Author Comment

by:expertfan
ID: 17771731
can someone please post a code which will do the listing on "list view control".
-------------------------------------
0
 
LVL 15

Expert Comment

by:JackOfPH
ID: 17771952
here are a simple example
Option Explicit

Private Sub Form_Load()
Dim fileName As String


fileName = Dir("c:*.*")
Do Until fileName = ""
     ListView1.ListItems.Add().Text = fileName
   
   fileName = Dir
Loop
End Sub
0
Industry Leaders: 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!

 

Author Comment

by:expertfan
ID: 17772013
HI JackOfPH,

I am looking at code which will list the "folders", "subfolder" and "files" recursively for a given parent folder.
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 17772578
Private Sub Form_Load()
    ListView1.ColumnHeaders.Clear
    ListView1.ColumnHeaders.Add , , "Folder Name", 2000
    ListView1.ColumnHeaders.Add , , "Folder Path", 3000
    ListView1.View = lvwReport
End Sub

Private Sub Command1_Click()
    ListView1.ListItems.Clear
    ScanFolders "D:\your_folder_name" 'Give your folder path
End Sub

Private Sub ScanFolders(Path As String)
'Dim oFso As New FileSystemObject
Dim oFso
Dim oFld
Dim oTFld
Dim oLst As ListItem
    Set oFso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next 'To handle permission denied error
    If Not oFso.FolderExists(Path) Then
        Set oFso = Nothing
        Exit Sub
    End If
    Set oFld = oFso.GetFolder(Path)
    Set oLst = ListView1.ListItems.Add(, , oFld.Name)
        oLst.SubItems(1) = oFld.Path
   
    For Each oTFld In oFld.SubFolders
        ScanFolders oTFld.Path
        DoEvents
    Next
    Set oFso = Nothing
    Err.Clear
End Sub
0
 

Author Comment

by:expertfan
ID: 17772653
hi shijusn,

this is good. can some one now please provide the code to compare two folders[folder1 and folder2] and list out

1. the ones which exist on left only in say GREEN COLOR
2. the ones which exist on right side only in say BLUE COLOR
3. if foldernames match but the number of files/subfolders under them is different then it should be in RED COLOR
4. the remaining entries in BLACK COLOR
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 17772667
What do u mean by left and right side?
u mean to say, two list view controls on left and right?
0
 

Author Comment

by:expertfan
ID: 17772812
yes, two list view controls. so that we can compare two folders.
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 17786277
Private Sub Form_Load()
    ListView1.ColumnHeaders.Clear
    ListView1.ColumnHeaders.Add , , "Folder Name", 2000
    ListView1.ColumnHeaders.Add , , "Folder Path", 3000
    ListView1.View = lvwReport
   
    ListView2.ColumnHeaders.Clear
    ListView2.ColumnHeaders.Add , , "Folder Name", 2000
    ListView2.ColumnHeaders.Add , , "Folder Path", 3000
    ListView2.View = lvwReport
End Sub
Private Sub Command1_Click()
    ListView1.ListItems.Clear
    ListView2.ListItems.Clear
    ScanFolders "D:\Source_Folder", "D:\Source_Folder", "D:\Dest_Folder"
End Sub
Private Sub ScanFolders(Path As String, SourcePath, DestPath As String)
Dim oFso
Dim oFld
Dim oTFld
Dim oLst As ListItem
Dim sPath As String
    Set oFso = CreateObject("Scripting.FileSystemObject")
'On Error Resume Next 'Uncomment this line to handle permission denied error

    If Not oFso.FolderExists(Path) Then
        Set oFso = Nothing
        Exit Sub
    End If
    Set oFld = oFso.GetFolder(Path)
    For Each oTFld In oFld.SubFolders
        Set oLst = ListView1.ListItems.Add(, , oTFld.Name)
            oLst.SubItems(1) = oTFld.Path
           
        sPath = Replace(oTFld.Path, SourcePath, "")
        sPath = DestPath & sPath
        If Not oFso.FolderExists(sPath) Then
            Set oLst = ListView2.ListItems.Add(, , "** Missing **")
                oLst.SubItems(1) = sPath
            oLst.ForeColor = vbRed
            oLst.ListSubItems(1).ForeColor = vbRed
        Else
            Set oLst = ListView2.ListItems.Add(, , oTFld.Name)
                oLst.SubItems(1) = oTFld.Path
        End If
        ScanFolders oTFld.Path, SourcePath, DestPath
        DoEvents
    Next
    Set oFso = Nothing
    Err.Clear
End Sub
0
 

Author Comment

by:expertfan
ID: 17786508
hi shijusn,

this is showing the 'folders' that exist on leftside and mentions whats 'missing' on right side.

but does not 'list' the folders that exist on rightside but is 'missing' on left side.
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 17786908
Private Sub Command1_Click()
    ListView1.ListItems.Clear
    ListView2.ListItems.Clear
    ScanFolders "D:\Source_Folder", "D:\Source_Folder", "D:\Dest_Folder", ListView1, ListView2
    ScanFolders "D:\Dest_Folder", "D:\Dest_Folder", "D:\Source_Folder", ListView2, ListView1
End Sub

Private Sub ScanFolders(Path As String, SourcePath As String, DestPath As String, LvSource As ListView, LvDest As ListView)
Dim oFso
Dim oFld
Dim oTFld
Dim oLst As ListItem
Dim sPath As String
    Set oFso = CreateObject("Scripting.FileSystemObject")
'On Error Resume Next 'Uncomment this line to handle permission denied error

    If Not oFso.FolderExists(Path) Then
        Set oFso = Nothing
        Exit Sub
    End If
    Set oFld = oFso.GetFolder(Path)
    For Each oTFld In oFld.SubFolders
   
        Set oLst = LvSource.ListItems.Add(, , oTFld.Name)
            oLst.SubItems(1) = oTFld.Path
           
        sPath = Replace(oTFld.Path, SourcePath, "")
        sPath = DestPath & sPath
        If Not oFso.FolderExists(sPath) Then
            Set oLst = LvDest.ListItems.Add(, , "** Missing **")
                oLst.SubItems(1) = sPath
            oLst.ForeColor = vbRed
            oLst.ListSubItems(1).ForeColor = vbRed
        Else
            Set oLst = LvDest.ListItems.Add(, , oTFld.Name)
                oLst.SubItems(1) = oTFld.Path
        End If
        ScanFolders oTFld.Path, SourcePath, DestPath, LvSource, LvDest
        DoEvents
    Next
    Set oFso = Nothing
    Err.Clear
End Sub
0
 

Author Comment

by:expertfan
ID: 17787128
hi shijusn,

this is good improvement, but all the folders are getting repeated twice.
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 17787201
Well, you needed two folders to be compared.
Its not repeated twice, actually its showing the missing folders
If you look at the second column, then you can understand all the paths are different.
0
 

Author Comment

by:expertfan
ID: 17787224
See i am comparing folder-1 the folders under this are

one
two
three
four
five

the folder-2 contains the following subfolders.

one
two
three
six
seven.

so the result should be like this
---------------------
one                               one
two                               two
three                             three
four                               ** missing **
five                                ** missing **
**missing**                  six
**missing**                  seven

-----------------
but the code you gave gives 10 rows instead of 7 rows
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 17787313
;-) ok done

Private Sub ScanFolders(Path As String, SourcePath As String, DestPath As String, LvSource As ListView, LvDest As ListView)
Dim oFso
Dim oFld
Dim oTFld
Dim oLst As ListItem
Dim sPath As String
    Set oFso = CreateObject("Scripting.FileSystemObject")
'On Error Resume Next 'Uncomment this line to handle permission denied error

    If Not oFso.FolderExists(Path) Then
        Set oFso = Nothing
        Exit Sub
    End If
    Set oFld = oFso.GetFolder(Path)
    For Each oTFld In oFld.SubFolders
   
        Set oLst = LvSource.ListItems.Add(, , oTFld.Name)
            oLst.SubItems(1) = oTFld.Path
           
        sPath = Replace(oTFld.Path, SourcePath, "")
        sPath = DestPath & sPath
        If Not oFso.FolderExists(sPath) Then
            Set oLst = LvDest.ListItems.Add(, , "** Missing **")
                oLst.SubItems(1) = sPath
            oLst.ForeColor = vbRed
            oLst.ListSubItems(1).ForeColor = vbRed
        End If
        ScanFolders oTFld.Path, SourcePath, DestPath, LvSource, LvDest
        DoEvents
    Next
    Set oFso = Nothing
    Err.Clear
End Sub
0
 

Author Comment

by:expertfan
ID: 17787336
yes now its showing 7 entries only on each side......but the left side and right side do no map one to one like i had mentioned above in the requirement.

the output is coming as follows....which is pretty confusing.

five                      **misssing**
four                     **misssing**
one                      one
three                    seven
two                      six
**misssing**       three
**missing**         two

but the output should be

one                               one
two                               two
three                             three
four                               ** missing **
five                                ** missing **
**missing**                  six
**missing**                  seven
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 17787409
@expertfan
That depends on the folder names
Are you expecting result in sorted order?
Format you specified is not in sorted order, infact its in counting order one two three
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 17787412
Hope you can do rest of the formating and sorting things
0
 

Author Comment

by:expertfan
ID: 17787421
hi shijusn,

it need not be really sorted, but the leftside should have mapping to the right.....thats really what i am looking at.

because with the code you currently gave me, the left side does not map to the right side.
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 17787435
Is it so?
Please  verify the path specified in the listview

Have u noticed second column of each list view? they are mapped correctly
0
 
LVL 14

Accepted Solution

by:
Shiju Sasidharan earned 500 total points
ID: 17787448
Sorry try after this

Private Sub Command1_Click()
    ListView1.Sorted = True
    ListView1.SortKey = 1
    ListView2.SortKey = 1
    ListView1.ListItems.Clear
    ListView2.ListItems.Clear
    ScanFolders "D:\Source_Folder", "D:\Source_Folder", "D:\Dest_Folder", ListView1, ListView2
    ScanFolders "D:\Dest_Folder", "D:\Dest_Folder", "D:\Source_Folder", ListView2, ListView1
End Sub
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 17787455
Include this one too
>>ListView2.Sorted = True
0
 

Author Comment

by:expertfan
ID: 17787456
yes, i am comparing the "left side listview" with "right side listview" and following is the current output. hope this clarifies...

the output is coming as follows....which is pretty confusing.

five                      **misssing**
four                     **misssing**
one                      one
three                    seven
two                      six
**misssing**       three
**missing**         two

------------
if you observer row 1, 2, 3 are fine. but rows 4,5,6,7 are not showing correct mappings.
for example...
row-4 on leftside shows "three" but rightside shows "seven"
row-5 on leftside shows "two" and rightside shows "six"


0
 

Author Comment

by:expertfan
ID: 17787459
hi shijusn,

this works great !!
0
 
LVL 14

Expert Comment

by:Shiju Sasidharan
ID: 17787502
Cool , finally you are happy.

Thanks for the grade :-)
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
Suggested Courses
Course of the Month3 days, 15 hours left to enroll

630 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