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

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

LVL 3
taz8020Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AndyAinscowFreelance programmer / ConsultantCommented:
OK.  Your code works perfectly the way you have written it.

Why don't you ask a question?
0
taz8020Author Commented:
it does not work if you use the example I gave?
0
taz8020Author Commented:
is there a way to rename files in the list before the compare?
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

AndyAinscowFreelance programmer / ConsultantCommented:
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
AndyAinscowFreelance programmer / ConsultantCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
taz8020Author Commented:
So how would I use this in the above example?
0
Fernando SotoRetiredCommented:
Can you post your FileCompare class please.
0
taz8020Author Commented:
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
Fernando SotoRetiredCommented:
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
AndyAinscowFreelance programmer / ConsultantCommented:
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
taz8020Author Commented:
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
Fernando SotoRetiredCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.