|
-
Jan 31st, 2007, 04:17 AM
#1
Thread Starter
Lively Member
[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.
-
Jan 31st, 2007, 11:16 AM
#2
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:
Private Sub CompareDir()
Dim strFirstDir As String = "c:\TestDir\"
Dim strSecondDir As String = "c:\TestDir2\"
Dim colDirList As New Hashtable
For Each strDirSec As String In IO.Directory.GetDirectories(strSecondDir)
colDirList.Add(strDirSec.Replace(strSecondDir, "").ToUpper, strDirSec.Replace(strSecondDir, "").ToUpper)
Next
For Each strDir As String In IO.Directory.GetDirectories(strFirstDir)
If colDirList.ContainsKey(strDir.Replace(strFirstDir, "").ToUpper) = False Then
IO.Directory.CreateDirectory(strSecondDir & strDir.Replace(strFirstDir, ""))
End If
Next
colDirList.Clear()
End Sub
-
Feb 1st, 2007, 06:31 AM
#3
Thread Starter
Lively Member
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.
-
Feb 1st, 2007, 09:30 AM
#4
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:
Private Sub CompareDir(ByVal strFirstDir As String, ByVal strSecondDir As String)
Dim colDirList As New Hashtable, colFileList As New Hashtable
If strSecondDir.Chars(strSecondDir.Length - 1) <> "\" Then
strSecondDir = strSecondDir & "\"
End If
If strFirstDir.Chars(strFirstDir.Length - 1) <> "\" Then
strFirstDir = strFirstDir & "\"
End If
For Each strDir As String In IO.Directory.GetDirectories(strSecondDir)
colDirList.Add(strDir.Replace(strSecondDir, "").ToUpper, strDir.Replace(strSecondDir, "").ToUpper)
For Each strFile As String In IO.Directory.GetFiles(strDir)
colFileList.Add(strFile.Replace(strDir, "").ToUpper, strDir)
Next
Next
For Each strDir As String In IO.Directory.GetDirectories(strFirstDir)
If colDirList.ContainsKey(strDir.Replace(strFirstDir, "").ToUpper) = False Then
IO.Directory.CreateDirectory(strSecondDir & strDir.Replace(strFirstDir, ""))
End If
For Each strFile As String In IO.Directory.GetFiles(strDir)
If colFileList.ContainsKey(strfile.Replace(strDir, "").ToUpper) = False Then
IO.File.Copy(strfile, strSecondDir & strDir.Replace(strFirstDir, "") & strfile.Replace(strDir, ""))
End If
Next
If IO.Directory.GetDirectories(strDir).Length > 0 Then
CompareDir(strDir, strSecondDir & strDir.Replace(strFirstDir, "")) 'Recursive call
End If
Next
colDirList.Clear()
colFileList.Clear()
End Sub
-
Feb 2nd, 2007, 07:09 AM
#5
Thread Starter
Lively Member
Re: About Compare Two Directories
i think this is what i want ...thanks dude!
-
Feb 2nd, 2007, 07:12 AM
#6
Fanatic Member
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
-
Feb 2nd, 2007, 08:59 AM
#7
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:
System.IO.File.GetCreationTime(strFileName)
-
Feb 2nd, 2007, 09:20 AM
#8
Fanatic Member
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!!!
-
Feb 2nd, 2007, 09:28 AM
#9
Thread Starter
Lively Member
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:
Dim FileDateInfo As New FileInfo(SourcePath_and_Name)
dim fileinfo as string = filedateinfo.lastwritetime.tostring
-
Feb 2nd, 2007, 09:36 AM
#10
Fanatic Member
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
-
Feb 2nd, 2007, 09:36 AM
#11
Fanatic Member
Re: [ReSolved]About Compare Two Directories [ReSolved]
-
Feb 2nd, 2007, 10:00 AM
#12
Thread Starter
Lively Member
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?
-
Feb 2nd, 2007, 10:43 AM
#13
Fanatic Member
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??
-
Feb 2nd, 2007, 11:23 AM
#14
Thread Starter
Lively Member
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.
-
May 19th, 2008, 02:16 PM
#15
Member
Re: [ReSolved]About Compare Two Directories [ReSolved]
 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:
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!
-
May 19th, 2008, 02:32 PM
#16
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:
For Each strDir As String In IO.Directory.GetDirectories(strSecondDir)
colDirList.Add(strDir.Replace(strSecondDir, "").ToUpper, strDir.Replace(strSecondDir, "").ToUpper)
For Each strFile As String In IO.Directory.GetFiles(strDir)
colFileList.Add(strFile.Replace(strDir, "").ToUpper & System.IO.File.GetCreationTime(strDir).ToString, strDir) 'Add the date here.
Next
Next
Use the date in the comparison of the hashtable key.
VB.NET Code:
For Each strFile As String In IO.Directory.GetFiles(strDir)
If colFileList.ContainsKey(strfile.Replace(strDir, "").ToUpper & System.IO.File.GetCreationTime(strDir).ToString) = False Then'Added the date info.
IO.File.Copy(strfile, strSecondDir & strDir.Replace(strFirstDir, "") & strfile.Replace(strDir, ""))
End If
Next
-
May 19th, 2008, 02:59 PM
#17
Member
Re: [ReSolved]About Compare Two Directories [ReSolved]
 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:
For Each strDir As String In IO.Directory.GetDirectories(strSecondDir)
colDirList.Add(strDir.Replace(strSecondDir, "").ToUpper, strDir.Replace(strSecondDir, "").ToUpper)
For Each strFile As String In IO.Directory.GetFiles(strDir)
colFileList.Add(strFile.Replace(strDir, "").ToUpper & System.IO.File.GetCreationTime(strDir).ToString, strDir) 'Add the date here.
Next
Next
Use the date in the comparison of the hashtable key.
VB.NET Code:
For Each strFile As String In IO.Directory.GetFiles(strDir)
If colFileList.ContainsKey(strfile.Replace(strDir, "").ToUpper & System.IO.File.GetCreationTime(strDir).ToString) = False Then'Added the date info.
IO.File.Copy(strfile, strSecondDir & strDir.Replace(strFirstDir, "") & strfile.Replace(strDir, ""))
End If
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!
-
May 19th, 2008, 03:17 PM
#18
Re: [ReSolved]About Compare Two Directories [ReSolved]
I see the problem in line #2 of the second example above change
VB.NET Code:
System.IO.File.GetCreationTime(strDir).ToString)
to
VB.NET Code:
System.IO.File.GetCreationTime(strFile).ToString)
-
May 19th, 2008, 05:53 PM
#19
Member
Re: [ReSolved]About Compare Two Directories [ReSolved]
 Originally Posted by ProphetBeal
I see the problem in line #2 of the second example above change
VB.NET Code:
System.IO.File.GetCreationTime(strDir).ToString)
to
VB.NET Code:
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.
-
May 20th, 2008, 08:46 PM
#20
Member
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!
-
May 21st, 2008, 08:03 AM
#21
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:
strFile.Replace(strDir, "").ToUpper
you would want to remove that replace statement so it would look more like this...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|