Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

compare to folders and find new or updated files using linq vb.net

Posted on 2014-08-22
12
Medium Priority
?
186 Views
Last Modified: 2016-02-16
The code I have below works great, I compare folders and can have a different file extension in the folders, I can even have different endings on the filenames. eg if in one folder they end p.jpg and the other end sc.png it works great.

THE PROBLEM if in one folder they end with "c.jpg" and the other they end with "sc.png" it returns the full list of all the files.

Dim dir1 As New System.IO.DirectoryInfo(startPath)
        Dim dir2 As New System.IO.DirectoryInfo(destinationPath)

        Dim list1 = dir1.GetFiles("*c.jpg", System.IO.SearchOption.TopDirectoryOnly)
        Dim list2 = dir2.GetFiles("*sc.png", System.IO.SearchOption.TopDirectoryOnly)

        Dim compare As New FileCompare
        compare.Find = "c.jpg"
        compare.Replacewith = "sc.png"

        Dim list3 = list1.Except(list2, compare)

Open in new window

0
Comment
Question by:taz8020
  • 5
  • 4
  • 3
12 Comments
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 40278553
OK.  Your code works perfectly the way you have written it.

Why don't you ask a question?
0
 
LVL 3

Author Comment

by:taz8020
ID: 40278554
it does not work if you use the example I gave?
0
 
LVL 3

Author Comment

by:taz8020
ID: 40278570
is there a way to rename files in the list before the compare?
0
Granular recovery for Microsoft Exchange

With Veeam Explorer for Microsoft Exchange you can choose the Exchange Servers and restore points you’re interested in, and Veeam Explorer will present the contents of those mailbox stores for browsing, searching and exporting.

 
LVL 45

Expert Comment

by:AndyAinscow
ID: 40278652
I think your FileCompare function needs to take the name of the second file (as a temporary string variable), check if the end is sc.png, if it is then replace that with c.jpg and then compare with the name of the first file, returning true if they match else returning false.
0
 
LVL 45

Accepted Solution

by:
AndyAinscow earned 2000 total points
ID: 40278659
There is an example of coding a comparer function here:
http://msdn.microsoft.com/en-us/library/vstudio/bb336390%28v=vs.90%29.aspx
0
 
LVL 3

Author Comment

by:taz8020
ID: 40278781
So how would I use this in the above example?
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40278823
Can you post your FileCompare class please.
0
 
LVL 3

Author Comment

by:taz8020
ID: 40278826
just using the standard FileCompare eg

Dim dir1 As New System.IO.DirectoryInfo(startPath)
        Dim dir2 As New System.IO.DirectoryInfo(destinationPath)

        Dim list1 = dir1.GetFiles("*c.jpg", System.IO.SearchOption.TopDirectoryOnly)
        Dim list2 = dir2.GetFiles("*sc.png", System.IO.SearchOption.TopDirectoryOnly)

        Dim compare As New FileCompare
        compare.Find = "c.jpg"
        compare.Replacewith = "sc.png"

        Dim list3 = list1.Except(list2, compare)
0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40278855
I have copy and paste your code into a VB .Net project and has a compile error with this line.

Dim compare As New FileCompare

So .Net on my system does not know about it. Is it in a dll that is not normally loaded at project creation? If so can you give the namespace it can be found in.
0
 
LVL 45

Expert Comment

by:AndyAinscow
ID: 40278968
As far as I know Microsoft .net doesn't have a built in FileCompare class.
Just modify that example so it compares the file names AFTER modifying the name from one of the sources in the way you require.
0
 
LVL 3

Author Comment

by:taz8020
ID: 40301647
sorry been using it so long thought it was built in here it is

    Public Class FileCompare
        Implements System.Collections.Generic.IEqualityComparer(Of System.IO.FileInfo)
        Public Find As String
        Public Replacewith As String
        Public Function Equals1(ByVal x As System.IO.FileInfo, ByVal y As System.IO.FileInfo) As Boolean Implements System.Collections.Generic.IEqualityComparer(Of System.IO.FileInfo).Equals
            'x = dest, y = start path
            If (x.Name.ToLower = y.Name.ToLower.Replace(Find.ToLower, Replacewith.ToLower)) AndAlso (x.LastWriteTime > y.LastWriteTime) Then
                Return True
            Else
                Return False
            End If
        End Function

        Public Function GetHashCode1(ByVal fi As System.IO.FileInfo) _
            As Integer Implements System.Collections.Generic.IEqualityComparer(Of System.IO.FileInfo).GetHashCode
            Dim s As String = fi.Name.Replace(Find, Replacewith).ToLower
            Return s.GetHashCode()
        End Function
    End Class

Open in new window

0
 
LVL 64

Expert Comment

by:Fernando Soto
ID: 40303898
Hi taz8020;

I have tried and the execution of the Except method did not return all the files. Can you verify this issue and if it is happening can you zip up the two directories with test data that will show the issue.
0

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

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

This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…

578 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