Question

private void button4_Click(object sender, EventArgs e)
{
    FileStream outputFileStream = new FileStream("log.txt", FileMode.Create, FileAccess.Write);
    StreamWriter writer = new StreamWriter(outputFileStream);

    // writing block

    string originalPathFile = @"C:\Users\user\Downloads\CaptchaCollection\Small\";
    string duplicatePath = @"C:\Users\user\Downloads\CaptchaCollection\Small\Duplicates\";
    string movedOriginal = @"C:\Users\user\Downloads\CaptchaCollection\Small\Sorted\";

    var files = Directory.GetFiles(originalPathFile)
                            .Select(nameWithExtension => Path.GetFileNameWithoutExtension(nameWithExtension))
                            .Where(name => { int number; return int.TryParse(name, out number); })
                            .Select(name => int.Parse(name))
                            .OrderBy(number => number).ToArray();

    while (files.Length > 1)
    {
        string duplicateOfFolder = Directory.CreateDirectory(duplicatePath + files[0].ToString()).FullName;

        for (int j = 1; j < files.Length; j++)
        {
            Bitmap im1 = new Bitmap(originalPathFile + files[0].ToString() + ".png");
            Bitmap im2 = new Bitmap(originalPathFile + files[j].ToString() + ".png");

            if (compare(im1, im2))
            {
                File.Move(originalPathFile + files[j].ToString() + ".png", duplicateOfFolder + files[j].ToString() + ".png");
                writer.WriteLine(files[j].ToString() + ".png" + " is a duplicate of " + files[0].ToString() + ".png \n");
            }
        }

        File.Move(originalPathFile + files[0].ToString() + ".png", movedOriginal + files[0].ToString() + ".png");
        writer.WriteLine(files[0].ToString() + ".png " + "has had its duplicates removed.");

        files = Directory.GetFiles(originalPathFile)
                            .Select(nameWithExtension => Path.GetFileNameWithoutExtension(nameWithExtension))
                            .Where(name => { int number; return int.TryParse(name, out number); })
                            .Select(name => int.Parse(name))
                            .OrderBy(number => number).ToArray();
    }

    writer.Close();
    outputFileStream.Close();
}

So this button basically moves duplicate files of an image visually. I got this code from one of my previous questions I've asked. Now I want to use a new folder to place duplicates of a specific image.

For example: 1.png has 5 visual duplicates (65.png,87.png,100.png,103.png,156.png). I want to move all the duplicates to this directory instead of just placing it in the Duplicates directory: C:\Users\user\Downloads\CaptchaCollection\Small\Duplicates\1\

Now instead what's happening is that it apparently is renaming and regenerating some images. I can't really describe this in words because I can't really see what's going on. What's not happening is that those files are not being moved to the directory of a duplicated file organization.

Folders will create but instead it's not placing it in the proper folder.

Was it helpful?

Solution

If I understood your requirement correctly then I think issue is in following lines.

if (compare(im1, im2))
{
    File.Move(originalPathFile + files[j].ToString() + ".png", duplicateOfFolder + files[j].ToString() + ".png");
    writer.WriteLine(files[j].ToString() + ".png" + " is a duplicate of " + files[0].ToString() + ".png \n");
}

You are actually comparing first file with others but still copying the files in duplicate folder.

Replace following line

File.Move(originalPathFile + files[j].ToString() + ".png", duplicateOfFolder + files[j].ToString() + ".png");

with

String path = duplicateOfFolder;
if (!Directory.Exists(path)) 
{
   Directory.CreateDirectory(path);
}

File.Move(originalPathFile + files[j].ToString() + ".png", path + "\\" + files[j].ToString() + ".png");

This should work.

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