Results 1 to 21 of 21

Thread: [ReSolved]About Compare Two Directories [ReSolved]

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    125

    Resolved [ReSolved]About Compare Two Directories [ReSolved]

    Hi All,
    I am currently stuck in how to compare two directories
    Assume i have two directories need to compare.(Named Dir1 and Dir2)
    Dir1 have 4 folders in it lets say Folder1, Folder2, Folder3, Folder4.
    Dir2 have 3 folders in it lets say Folder1, Folder2, Folder3

    obviously Dir1 have Folder4 which the Dir2 don't have. i want a code that can compare these two directories and be able to find out the differences. if yes, then create the same name folder in Dir2
    many thanks
    Last edited by scotish_bagpipe; Feb 2nd, 2007 at 07:10 AM.

  2. #2
    Hyperactive Member ProphetBeal's Avatar
    Join Date
    Aug 2006
    Location
    New York
    Posts
    424

    Re: About Compare Two Directories

    Below is a quick function that will compare 2 dirs and create any missing directories. FYI i didnt include any error catching in my code...so you may want to add that. Hopefully this helps.
    VB Code:
    1. Private Sub CompareDir()
    2.         Dim strFirstDir As String = "c:\TestDir\"
    3.         Dim strSecondDir As String = "c:\TestDir2\"
    4.         Dim colDirList As New Hashtable
    5.         For Each strDirSec As String In IO.Directory.GetDirectories(strSecondDir)
    6.             colDirList.Add(strDirSec.Replace(strSecondDir, "").ToUpper, strDirSec.Replace(strSecondDir, "").ToUpper)
    7.         Next
    8.         For Each strDir As String In IO.Directory.GetDirectories(strFirstDir)
    9.             If colDirList.ContainsKey(strDir.Replace(strFirstDir, "").ToUpper) = False Then
    10.                 IO.Directory.CreateDirectory(strSecondDir & strDir.Replace(strFirstDir, ""))
    11.             End If
    12.         Next
    13.         colDirList.Clear()
    14.     End Sub

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    125

    Re: About Compare Two Directories

    thanks for your code, its helps me a lot
    but what if i want to do the whole tree of directory?
    e.g i got two directories Dir1 Dir2
    Dir1 and Dir2 structure is
    Code:
    Dir1                                    Dir2
      Folder1                                 Folder1
          Folder11                               Folder11
                                                              XMlFile1.xml
          Folder12                               Folder12
          Folder13                               Folder13
                                                             XMLFile3.xml
                                                    Folder14
      Folder2                                 Folder2
                                                 XMLFile2.xml
      Folder3                                 Folder3
      Folder4                                 Folder4
                                                Folder5

    each folder maybe contains many xml files
    what i gonna do is go throgh all the directory and its sub directories. if any of files or folders from Dir2 that Dir1 don't have, create the folder from Dir1 and add files if it haven't appear in Dir1.
    in above example we can see from Dir2 there are two folders (Folder14 and Folder5)which Dir1 don't have.
    prophetbeal, from above your example i only can add folder5 in dir1 but missing folder14, how can I go through all the directory and its sub or even sub-sub -sub directories?
    Last edited by scotish_bagpipe; Feb 1st, 2007 at 06:39 AM.

  4. #4
    Hyperactive Member ProphetBeal's Avatar
    Join Date
    Aug 2006
    Location
    New York
    Posts
    424

    Re: About Compare Two Directories

    I updated by previous function to include comparing sub folders and files. Just a reminder, you may want to add in some error catching.

    VB Code:
    1. Private Sub CompareDir(ByVal strFirstDir As String, ByVal strSecondDir As String)
    2.         Dim colDirList As New Hashtable, colFileList As New Hashtable
    3.         If strSecondDir.Chars(strSecondDir.Length - 1) <> "\" Then
    4.             strSecondDir = strSecondDir & "\"
    5.         End If
    6.         If strFirstDir.Chars(strFirstDir.Length - 1) <> "\" Then
    7.             strFirstDir = strFirstDir & "\"
    8.         End If
    9.  
    10.         For Each strDir As String In IO.Directory.GetDirectories(strSecondDir)
    11.             colDirList.Add(strDir.Replace(strSecondDir, "").ToUpper, strDir.Replace(strSecondDir, "").ToUpper)
    12.             For Each strFile As String In IO.Directory.GetFiles(strDir)
    13.                 colFileList.Add(strFile.Replace(strDir, "").ToUpper, strDir)
    14.             Next
    15.         Next
    16.  
    17.         For Each strDir As String In IO.Directory.GetDirectories(strFirstDir)
    18.             If colDirList.ContainsKey(strDir.Replace(strFirstDir, "").ToUpper) = False Then
    19.                 IO.Directory.CreateDirectory(strSecondDir & strDir.Replace(strFirstDir, ""))
    20.             End If
    21.             For Each strFile As String In IO.Directory.GetFiles(strDir)
    22.                 If colFileList.ContainsKey(strfile.Replace(strDir, "").ToUpper) = False Then
    23.                     IO.File.Copy(strfile, strSecondDir & strDir.Replace(strFirstDir, "") & strfile.Replace(strDir, ""))
    24.                 End If
    25.             Next
    26.             If IO.Directory.GetDirectories(strDir).Length > 0 Then
    27.                 CompareDir(strDir, strSecondDir & strDir.Replace(strFirstDir, "")) 'Recursive call
    28.             End If
    29.         Next
    30.         colDirList.Clear()
    31.         colFileList.Clear()
    32.     End Sub

    Helpful Links:
    VB 6 - How to get the "Key" Value in a collection
    VB.NET - File Search Utility || VB.NET - How to compare 2 directories || VB.NET - How to trust a network share
    VB.NET - Create Excel Spreadsheet From Array || VB.NET - Example Code & Hints you may not know
    VB.NET - Save JPEG with a certain quality (image compression) || VB.NET - DragDrop Files, Emails, and Email Attachments

    Please post some of the code you need help with (it makes it easier to help you)
    If your problem has been solved then please mark the thread [RESOLVED].
    Don't forget to Rate this post

    "Pinky, you give a whole new meaning to the phrase, 'counter-intelligence'."-The Brain-

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    125

    Re: About Compare Two Directories

    i think this is what i want ...thanks dude!

  6. #6
    Fanatic Member Ggalla1779's Avatar
    Join Date
    Feb 2006
    Location
    Glasgow
    Posts
    532

    Re: [ReSolved]About Compare Two Directories [ReSolved]

    so how would you change that routine to update based on the creation date being later?? ie I have pen drive plug it in _work folder checks -Work folder on c drive and updates any new creatons or nwer created files??

    cheers

    George

  7. #7
    Hyperactive Member ProphetBeal's Avatar
    Join Date
    Aug 2006
    Location
    New York
    Posts
    424

    Re: [ReSolved]About Compare Two Directories [ReSolved]

    Instead of comparing just based on the file name. You would also check the Creation date of the file.
    VB Code:
    1. System.IO.File.GetCreationTime(strFileName)

    Helpful Links:
    VB 6 - How to get the "Key" Value in a collection
    VB.NET - File Search Utility || VB.NET - How to compare 2 directories || VB.NET - How to trust a network share
    VB.NET - Create Excel Spreadsheet From Array || VB.NET - Example Code & Hints you may not know
    VB.NET - Save JPEG with a certain quality (image compression) || VB.NET - DragDrop Files, Emails, and Email Attachments

    Please post some of the code you need help with (it makes it easier to help you)
    If your problem has been solved then please mark the thread [RESOLVED].
    Don't forget to Rate this post

    "Pinky, you give a whole new meaning to the phrase, 'counter-intelligence'."-The Brain-

  8. #8
    Fanatic Member Ggalla1779's Avatar
    Join Date
    Feb 2006
    Location
    Glasgow
    Posts
    532

    Re: [ReSolved]About Compare Two Directories [ReSolved]

    yup but how would I detect a pen drive is available with folder _work

    I have a question on for this....any help would be great!!!

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    125

    Re: [ReSolved]About Compare Two Directories [ReSolved]

    system file watcher? maybe we can accord the file creation time compare with current determine to decide which file we need?
    VB Code:
    1. Dim FileDateInfo As New FileInfo(SourcePath_and_Name)
    2. dim fileinfo as string = filedateinfo.lastwritetime.tostring

  10. #10
    Fanatic Member Ggalla1779's Avatar
    Join Date
    Feb 2006
    Location
    Glasgow
    Posts
    532

    Re: [ReSolved]About Compare Two Directories [ReSolved]

    well plan was when I plug in USB pen drive...it looks at the folder on c drive and copies latest files to Pen....then when I got home plugged it in and it copies latest files to main dev box.... that way I have a backup of dev work

  11. #11
    Fanatic Member Ggalla1779's Avatar
    Join Date
    Feb 2006
    Location
    Glasgow
    Posts
    532

    Re: [ReSolved]About Compare Two Directories [ReSolved]


  12. #12

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    125

    Re: [ReSolved]About Compare Two Directories [ReSolved]

    I think the logical driver always got name like (E:\ or G:\)
    Dim strRoots() as string= Directory.GetLogicalDrivers
    Dim s as string
    dim m as string =""
    For each s in strRoots
    m=m &s
    Next
    msgbox(m)

    this code will diplay all the name of driver in your computer
    i guess is it possiable to use before name of driver length to compare with the pen driver you inserted length? i also found DirectorySearch componet in vb.net, i am not sure its can be use as a trigger to help you detect when the pen driver inserted?

  13. #13
    Fanatic Member Ggalla1779's Avatar
    Join Date
    Feb 2006
    Location
    Glasgow
    Posts
    532

    Re: [ReSolved]About Compare Two Directories [ReSolved]

    Well it certainly gets drive letters...wonder if we could do a normal drives and now and see if there is diff...thats would give pen drive?? Check for _Work folder then check for changes??

  14. #14

    Thread Starter
    Lively Member
    Join Date
    Jun 2006
    Posts
    125

    Re: [ReSolved]About Compare Two Directories [ReSolved]

    in my opinion, you can try to use the FileSystemWatcher to Check whether your pen drive has been plugged in or not. (as a trigger)
    First You can set File watch path as you Pen Drive name (E:\ or G:\)
    if your pen drive plugged in, then You can write directory compare code within the FileSystemWathcher_Created Sub routine to compare your pen drive and your local drive, If file has been modified, copy files to pen drive.
    if watcher can't found pen drive then do nothing.(which also means pen drive is not plug in yet)


    about How to compare files... every file havs the latest write time. you can based on your pen drivers folder's file to find out the latest write time which is greater than this time from the work folder.
    once you select out these files, you are able to copy them from work folder to your pen drive

    this is only my personal idea. because i don't have and USB Drive at moment so i unable to write code write now,, but it still worry a try

    build a service to implement this will be a better choise
    Last edited by scotish_bagpipe; Feb 2nd, 2007 at 11:35 AM.

  15. #15
    Member
    Join Date
    Mar 2008
    Posts
    32

    Re: [ReSolved]About Compare Two Directories [ReSolved]

    Quote Originally Posted by ProphetBeal
    Instead of comparing just based on the file name. You would also check the Creation date of the file.
    VB Code:
    1. System.IO.File.GetCreationTime(strFileName)

    Prophet, Can you give an example of this? I am trying to see if any of the files in strSecondDir don't match the strFirstDir. I have if they aren't there via your example and I can't figure out how to do the checking of the file time. I need to check for a different date/time of every file. Can you help me out. I think I need more coffee.

    Thanks!

  16. #16
    Hyperactive Member ProphetBeal's Avatar
    Join Date
    Aug 2006
    Location
    New York
    Posts
    424

    Re: [ReSolved]About Compare Two Directories [ReSolved]

    I haven't tested this cause I don't have the VB.NET IDE on this machine yet, but these changes should work.

    First add the date to the statement as the key to the hashtable
    VB.NET Code:
    1. For Each strDir As String In IO.Directory.GetDirectories(strSecondDir)
    2.     colDirList.Add(strDir.Replace(strSecondDir, "").ToUpper, strDir.Replace(strSecondDir, "").ToUpper)
    3.     For Each strFile As String In IO.Directory.GetFiles(strDir)
    4.         colFileList.Add(strFile.Replace(strDir, "").ToUpper & System.IO.File.GetCreationTime(strDir).ToString, strDir) 'Add the date here.
    5.     Next
    6. Next

    Use the date in the comparison of the hashtable key.
    VB.NET Code:
    1. For Each strFile As String In IO.Directory.GetFiles(strDir)
    2.     If colFileList.ContainsKey(strfile.Replace(strDir, "").ToUpper & System.IO.File.GetCreationTime(strDir).ToString) = False Then'Added the date info.
    3.         IO.File.Copy(strfile, strSecondDir & strDir.Replace(strFirstDir, "") & strfile.Replace(strDir, ""))
    4.     End If
    5. Next

    Helpful Links:
    VB 6 - How to get the "Key" Value in a collection
    VB.NET - File Search Utility || VB.NET - How to compare 2 directories || VB.NET - How to trust a network share
    VB.NET - Create Excel Spreadsheet From Array || VB.NET - Example Code & Hints you may not know
    VB.NET - Save JPEG with a certain quality (image compression) || VB.NET - DragDrop Files, Emails, and Email Attachments

    Please post some of the code you need help with (it makes it easier to help you)
    If your problem has been solved then please mark the thread [RESOLVED].
    Don't forget to Rate this post

    "Pinky, you give a whole new meaning to the phrase, 'counter-intelligence'."-The Brain-

  17. #17
    Member
    Join Date
    Mar 2008
    Posts
    32

    Re: [ReSolved]About Compare Two Directories [ReSolved]

    Quote Originally Posted by ProphetBeal
    I haven't tested this cause I don't have the VB.NET IDE on this machine yet, but these changes should work.

    First add the date to the statement as the key to the hashtable
    VB.NET Code:
    1. For Each strDir As String In IO.Directory.GetDirectories(strSecondDir)
    2.     colDirList.Add(strDir.Replace(strSecondDir, "").ToUpper, strDir.Replace(strSecondDir, "").ToUpper)
    3.     For Each strFile As String In IO.Directory.GetFiles(strDir)
    4.         colFileList.Add(strFile.Replace(strDir, "").ToUpper & System.IO.File.GetCreationTime(strDir).ToString, strDir) 'Add the date here.
    5.     Next
    6. Next

    Use the date in the comparison of the hashtable key.
    VB.NET Code:
    1. For Each strFile As String In IO.Directory.GetFiles(strDir)
    2.     If colFileList.ContainsKey(strfile.Replace(strDir, "").ToUpper & System.IO.File.GetCreationTime(strDir).ToString) = False Then'Added the date info.
    3.         IO.File.Copy(strfile, strSecondDir & strDir.Replace(strFirstDir, "") & strfile.Replace(strDir, ""))
    4.     End If
    5. Next
    Unsure what exactly is happening but looks like it is telling me every file is missmatched now. Let me know once you get to a place you can test. I have to head out for a bit thanks for your help!

  18. #18
    Hyperactive Member ProphetBeal's Avatar
    Join Date
    Aug 2006
    Location
    New York
    Posts
    424

    Re: [ReSolved]About Compare Two Directories [ReSolved]

    I see the problem in line #2 of the second example above change

    VB.NET Code:
    1. System.IO.File.GetCreationTime(strDir).ToString)
    to
    VB.NET Code:
    1. System.IO.File.GetCreationTime(strFile).ToString)

    Helpful Links:
    VB 6 - How to get the "Key" Value in a collection
    VB.NET - File Search Utility || VB.NET - How to compare 2 directories || VB.NET - How to trust a network share
    VB.NET - Create Excel Spreadsheet From Array || VB.NET - Example Code & Hints you may not know
    VB.NET - Save JPEG with a certain quality (image compression) || VB.NET - DragDrop Files, Emails, and Email Attachments

    Please post some of the code you need help with (it makes it easier to help you)
    If your problem has been solved then please mark the thread [RESOLVED].
    Don't forget to Rate this post

    "Pinky, you give a whole new meaning to the phrase, 'counter-intelligence'."-The Brain-

  19. #19
    Member
    Join Date
    Mar 2008
    Posts
    32

    Re: [ReSolved]About Compare Two Directories [ReSolved]

    Quote Originally Posted by ProphetBeal
    I see the problem in line #2 of the second example above change

    VB.NET Code:
    1. System.IO.File.GetCreationTime(strDir).ToString)
    to
    VB.NET Code:
    1. System.IO.File.GetCreationTime(strFile).ToString)
    I changed your code from GetCreationTime to GetLastWriteTime and that fixed it!

    Thanks!
    Last edited by jtrout; May 19th, 2008 at 06:12 PM.

  20. #20
    Member
    Join Date
    Mar 2008
    Posts
    32

    Re: [ReSolved]About Compare Two Directories [ReSolved]

    OK, it works great, but (yes I hate that word!) if I have a file with the same name in two different directories
    C:\folder1\temp1\mylog.txt #5/19/2008 6:00:00 PM#
    C:\folder1\temp2\mylog.txt #5/20/2008 7:00:00 PM#

    Yes this is correct as far as the same file is under two different directories. It is a install log for two different parts of the same software. Is there a way to instead of adding it to the hashtable with \mylog.txt #5/19/2008 6:00:00 PM# to be like \temp1\mylog.txt #5/19/2008 6:00:00 PM#

    Thanks for the help. You all have been great with help on a closed thread!

  21. #21
    Hyperactive Member ProphetBeal's Avatar
    Join Date
    Aug 2006
    Location
    New York
    Posts
    424

    Re: [ReSolved]About Compare Two Directories [ReSolved]

    If you look at the code where it populates the hashtable and checks if the key is in the hashtable, you will see a replace statement like this
    VB.NET Code:
    1. strFile.Replace(strDir, "").ToUpper
    you would want to remove that replace statement so it would look more like this...
    VB.NET Code:
    1. strFile.ToUpper

    Helpful Links:
    VB 6 - How to get the "Key" Value in a collection
    VB.NET - File Search Utility || VB.NET - How to compare 2 directories || VB.NET - How to trust a network share
    VB.NET - Create Excel Spreadsheet From Array || VB.NET - Example Code & Hints you may not know
    VB.NET - Save JPEG with a certain quality (image compression) || VB.NET - DragDrop Files, Emails, and Email Attachments

    Please post some of the code you need help with (it makes it easier to help you)
    If your problem has been solved then please mark the thread [RESOLVED].
    Don't forget to Rate this post

    "Pinky, you give a whole new meaning to the phrase, 'counter-intelligence'."-The Brain-

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width