Question

I develop a little software which permits me to compare 2 folders and Add files. I take principally code from msdn.

That looks to work fine with some files but I have some troubles with 2 folders (292 and 268 files).

In my case with the 2 big folder I have thoses differences found by the method :

C:\Users\Puppie\Desktop\KittiesWarWP8\KittiesWarWP8.sdf
C:\Users\Puppie\Desktop\KittiesWarWP8\Debug\KWGameComp\KWGameComp.pdb
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\Bin\x86\Debug\KWGame_Debug_x86.xap
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\obj\x86\Debug\KWGame.csprojResolveAssemblyReference.cache
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\obj\x86\Debug\XapCacheFile.xml
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGameComp\Debug\fxc.write.1.tlog
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGameComp\Debug\KWGameComp.write.1.tlog
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGameComp\Debug\link.write.1.tlog
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Thumbs.db
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\Thumbs.db
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\obj\x86\Debug\KWGamePages.csprojResolveAssemblyReference.cache

But I have see that miss some files 292 - 268 = 24 files. The method found me only 11 ... . I check and find a little folder which one looks like to be ignored from the method :

C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\AlignmentGrid.png
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\ApplicationIcon.png
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\FlipCycleTileLarge.png
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\FlipCycleTileMedium.png
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\FlipCycleTileSmall.png
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\IconicTileMediumLarge.png
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\IconicTileSmall.png

Thoses files are totally ignored.. I have made 2 folder for test just thoses files (folder 1 and 2) and the result for the difference by the same method is :

C:\Users\Puppie\Desktop\1\Assets\AlignmentGrid.png
C:\Users\Puppie\Desktop\1\Assets\ApplicationIcon.png
C:\Users\Puppie\Desktop\1\Assets\Tiles\FlipCycleTileLarge.png
C:\Users\Puppie\Desktop\1\Assets\Tiles\FlipCycleTileMedium.png
C:\Users\Puppie\Desktop\1\Assets\Tiles\FlipCycleTileSmall.png
C:\Users\Puppie\Desktop\1\Assets\Tiles\IconicTileMediumLarge.png
C:\Users\Puppie\Desktop\1\Assets\Tiles\IconicTileSmall.png

Tada when the folder is small he found the difference perfectly, in the destination folder for the test 1 and the destination folder for the test 2 thoses pictures do not exist ( I have checked).

So i just want to know if the method on msdn have a limit ?

There is the real source code : http://msdn.microsoft.com/en-us/library/bb546137.aspx

and mine just a little modified (add Directories) :

compare method :

path A : C:\Users\Puppie\Desktop\KittiesWarWP8\

path B : C:\Users\Puppie\Desktop\Logiciel pour projet\KittiesWarWP8\

 private void checkFiles()
    {
        List<string> dir = new List<string>();
        int lenghtPath = pathA.Length - (new DirectoryInfo(pathA).Name).Length - 1;

        System.IO.DirectoryInfo dir1 = new System.IO.DirectoryInfo(pathA);
        System.IO.DirectoryInfo dir2 = new System.IO.DirectoryInfo(pathB);

        // Take a snapshot of the file system.
        IEnumerable<System.IO.FileInfo> list1 = dir1.GetFiles("*", System.IO.SearchOption.AllDirectories);
        IEnumerable<System.IO.FileInfo> list2 = dir2.GetFiles("*", System.IO.SearchOption.AllDirectories);

        //A custom file comparer defined below
        FileCompare myFileCompare = new FileCompare();

        // Find the set difference between the two folders. 
        // For this example we only check one way. 
        var queryList1Only = (from file in list1 select file).Except(list2, myFileCompare);
        int temp;

        foreach (var v in queryList1Only)
        {
            if (dir.Count == 0)
            {

                if (!new FileInfo(pathB + (v.FullName.Remove(0, pathA.Length))).Exists)
                {
                    //we do a little verification
                    //the software must do not take the base folder
                    if (!(new FileInfo(v.FullName).DirectoryName + @"\").Equals(pathA))
                    {
                        dir.Add(new FileInfo(v.FullName).DirectoryName);
                        this.checkedListBox1.Items.Add("INEXISTANT |    " + dir[dir.Count - 1]);
                    }

                    this.checkedListBox1.Items.Add("INEXISTANT |    " + v.FullName);
                }
                else
                {
                    //we do a little verification
                    //the software must do not take the base folder
                    if (!(new FileInfo(v.FullName).DirectoryName + @"\").Equals(pathA))
                    {
                        dir.Add(new FileInfo(v.FullName).DirectoryName);
                        this.checkedListBox1.Items.Add("MODIFIE        |    " + dir[dir.Count - 1]);
                    }

                    this.checkedListBox1.Items.Add("MODIFIE        |    " + v.FullName);
                }
            }
            else
            {
                bool find = false;
                int save = 0;

                for (int i = 0; i < dir.Count; i++)
                {
                    if ((new FileInfo(v.FullName).DirectoryName).Equals(dir[i]))
                    {
                        find = true;
                        save = i;
                        i = dir.Count;
                    }
                }

                if (!find)
                {
                    if (!new FileInfo(pathB + (v.FullName.Remove(0, pathA.Length))).Exists)
                    {
                        dir.Add(new FileInfo(v.FullName).DirectoryName);
                        this.checkedListBox1.Items.Add("INEXISTANT |    " + dir[dir.Count - 1]);

                        this.checkedListBox1.Items.Add("INEXISTANT |    " + v.FullName);
                    }
                    else
                    {
                        dir.Add(new FileInfo(v.FullName).DirectoryName);
                        this.checkedListBox1.Items.Add("MODIFIE        |    " + dir[dir.Count - 1]);

                        this.checkedListBox1.Items.Add("MODIFIE        |    " + v.FullName);
                    }
                }
                else
                {
                    if (!new FileInfo(pathB + (v.FullName.Remove(0, pathA.Length))).Exists)
                        this.checkedListBox1.Items.Add("INEXISTANT |    " + v.FullName);
                    else
                        this.checkedListBox1.Items.Add("MODIFIE        |    " + v.FullName);
                }

            }

        }
    }

compare Class :

class FileCompare : System.Collections.Generic.IEqualityComparer<System.IO.FileInfo>
{
    public FileCompare() { }

    public bool Equals(System.IO.FileInfo f1, System.IO.FileInfo f2)
    {
        return (f1.Name == f2.Name &&
                f1.Length == f2.Length);
    }

    // Return a hash that reflects the comparison criteria. According to the  
    // rules for IEqualityComparer<T>, if Equals is true, then the hash codes must 
    // also be equal. Because equality as defined here is a simple value equality, not 
    // reference identity, it is possible that two or more objects will produce the same 
    // hash code. 
    public int GetHashCode(System.IO.FileInfo fi)
    {
        string s = String.Format("{0}{1}", fi.Name, fi.Length);
        return s.GetHashCode();
    }
}

thanks in advance for your help ;).

PS : sorry for my medium english

Was it helpful?

Solution

I see an issue with your FileCompare class. When checking for equality, you are only comparing the file Name. You are also checking the size which will help but doesn't guarantee against false positives of files with the same name in different directories.

It would be better to check the relative path from dir1 and dir2. Without seeing full file lists and sizes, I can't say that this will fix your problem, but it is one place that could cause problems.

Update: More of an explanation.

When I saw that there were 24 files that were different, but the code only turned up 11, my first thought was that maybe the code was finding false positives -- a single file in one of the directory structures was matching 2 or more files in the other directory structure. For example:

dir1 file:
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\AlignmentGrid.png

dir2 files:
C:\Users\Puppie\Desktop\Logicielpourprojet\KittiesWarWP8\KWGamePages\Assets\AlignmentGrid.png

C:\Users\Puppie\Desktop\Logicielpourprojet\KittiesWarWP8\KWGamePages\AdditionalAssets\AlignmentGrid.png

So if you have duplicate files in different and multiple directories, they aren't showing up using your FileCompare because your FileCompare considers them to be the same file.

Using my example, I would expect the dir1 file to match the first dir2 file, but not the second one. Because the relative paths are different, I'd consider those to be different files. If you would also consider them to be different files, then you need to update your FileCompare class to differentiate between the two by checking the path relative to dir1 and dir2.

OTHER TIPS

OK I have made a change in Compare file like this :

public bool Equals(System.IO.FileInfo f1, System.IO.FileInfo f2)
        {
            return ((f1.FullName.Remove(0, this.pathA.Length)).Equals(f2.FullName.Remove(0, this.pathB.Length)) &&
                    f1.Length == f2.Length);
        }

now the result change a lot ^^ :

C:\Users\Puppie\Desktop\KittiesWarWP8\KittiesWarWP8.sdf C:\Users\Puppie\Desktop\KittiesWarWP8\Debug\KWGameComp\KWGameComp.pdb C:\Users\Puppie\Desktop\KittiesWarWP8\Debug\test3Comp\SimpleVertexShader.cso C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\Bin\Debug\ApplicationIcon.png C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\Bin\Debug\Background.png C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\Bin\x86\Debug\KWGameComp.pdb C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\Bin\x86\Debug\KWGame_Debug_x86.xap C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\obj\Debug\App.g.cs C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\obj\Debug\App.g.i.cs C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\obj\Debug\KWGame.dll C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\obj\Debug\KWGame.pdb C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\obj\x86\Debug\DesignTimeResolveAssemblyReferences.cache C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\obj\x86\Debug\KWGame.csprojResolveAssemblyReference.cache C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\obj\x86\Debug\MainPage.g.i.cs C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGame\obj\x86\Debug\XapCacheFile.xml C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGameComp\Debug\fxc.write.1.tlog C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGameComp\Debug\KWGameComp.write.1.tlog C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGameComp\Debug\link.write.1.tlog C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGameLib\bin\Windows Phone\Debug\KWGameLib.dll C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGameLib\bin\Windows Phone\Debug\KWGameLib.pdb C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGameLib\obj\Windows Phone\Debug\KWGameLib.dll C:\Users\Puppie\Desktop\KittiesWarWP8\KWGame\KWGameLib\obj\Windows Phone\Debug\KWGameLib.pdb C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\AlignmentGrid.png C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\ApplicationIcon.png C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Thumbs.db C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\FlipCycleTileLarge.png C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\FlipCycleTileMedium.png C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\FlipCycleTileSmall.png C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\IconicTileMediumLarge.png C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\IconicTileSmall.png C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\Thumbs.db C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Bin\x86\Debug\KWGameComp.pdb C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\obj\x86\Debug\KWGamePages.csprojResolveAssemblyReference.cache C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Properties\AppManifest.xml C:\Users\Puppie\Desktop\KittiesWarWP8\KWMapGenerator\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs C:\Users\Puppie\Desktop\KittiesWarWP8\KWMapGenerator\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs C:\Users\Puppie\Desktop\KittiesWarWP8\KWMapGenerator\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs C:\Users\Puppie\Desktop\KittiesWarWP8\KWMapGeneratorNokia\obj\Debug\TemporaryGeneratedFile_036C0B5B-1481-4323-8D20-8F5ADCB23D92.cs C:\Users\Puppie\Desktop\KittiesWarWP8\KWMapGeneratorNokia\obj\Debug\TemporaryGeneratedFile_5937a670-0e60-4077-877b-f7221da3dda1.cs C:\Users\Puppie\Desktop\KittiesWarWP8\KWMapGeneratorNokia\obj\Debug\TemporaryGeneratedFile_E7A71F73-0F8D-4B9B-B56E-8E70B10BC5D3.cs

43 differences ^^' (292 - 269 = 23 (nonexistent files) + 20 (modified files)). And as we can see :

    C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\AlignmentGrid.png
    C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\ApplicationIcon.png
    C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\FlipCycleTileLarge.png
    C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\FlipCycleTileMedium.png
    C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\FlipCycleTileSmall.png
C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\IconicTileMediumLarge.png
    C:\Users\Puppie\Desktop\KittiesWarWP8\KWGamePages\Assets\Tiles\IconicTileSmall.png

Effectively the problem come from a false positive ^^. I think my copare method is good but if you see a error again tell me where please . I will check if I have 20 modified files in real ^^.

If it's all good I will approve @Joel Rondeau answer ;).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top