-1

I currently have 2 folders on my desktop "START" and "END". There could be any number of files in start folder but I need to figure out the following.

If the filenames exist in the START FOLDER then check if they exist in the END Folder and if they do COPY the files that exist.

I have stated the strings in the code and have used GetFiles to find the file names in each folder. But cant piece it together ...

var USERNAME = Environment.UserName;
        string START_DIRECTORY = @"C:\Users\" + USERNAME + "\\Desktop\\START";
        string END_FOLDER_STRING = @"C:\Users\" + USERNAME + "\\Desktop\\END";
        string[] files = System.IO.Directory.GetFiles(START_DIRECTORY);
        string[] files2 = System.IO.Directory.GetFiles(END_FOLDER_STRING);
Rand Random
  • 7,300
  • 10
  • 40
  • 88
Dibsy
  • 1
  • 1
  • 2
  • https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/foreach-in – CodeCaster Jan 23 '18 at 10:04
  • If you want to find file names which exist in both folders you can use this code: `var files3 = files.Where(f => files.contains(f));` Then you have a new list of filenames. If you want to find filenames which NOT exist in END folder you can use this: `var files4 = files.Except(files2);` – Sebastian Siemens Jan 23 '18 at 10:14

2 Answers2

1

Try using LINQ, because most of the times it is faster than iterating through both collections:

First, add a class called FileNameEqualityComparer.

This class will be used to compare the elements to see if they are the same. In your case, if their file name is equal it should return true:

public class FileNameEqualityComparer : IEqualityComparer<string>
{
    public bool Equals(string x, string y)
    {
        if (ReferenceEquals(x, y))
        {
            return true;
        }

        return x != null && y != null && Path.GetFileName(x) == Path.GetFileName(y);
    }

    public int GetHashCode(string obj)
    {
        return Path.GetFileName(obj).GetHashCode();
    }
}

And then use IEnumerable.Intersect with the created IEqualityComparer to get the files shared by your two collections:

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;

...

static void Main(string[] args)
{
    var USERNAME = Environment.UserName;
    string START_DIRECTORY = @"C:\Users\" + USERNAME + "\\Desktop\\START";
    string END_FOLDER_STRING = @"C:\Users\" + USERNAME + "\\Desktop\\END";

    IEnumerable<string> startFiles = Directory.GetFiles(START_DIRECTORY);
    IEnumerable<string> endFiles = Directory.GetFiles(END_FOLDER_STRING);

    foreach(string file in startFiles.Intersect(endFiles, new FileNameEqualityComparer()))
    {
        File.Copy(file, "[the path to the folder where they should be copied]");
    }
}
appa yip yip
  • 1,404
  • 1
  • 17
  • 38
0

The simplest thing: iterate over every file in START_DIRECTORY and check if a correcposing file exists in end files. If it does, copy it. Beware that performacewise this is not the best solution

static void Main(string[] args)
{
    var USERNAME = Environment.UserName;
    string START_DIRECTORY = @"C:\Users\" + USERNAME + "\\Desktop\\START";
    string END_FOLDER_STRING = @"C:\Users\" + USERNAME + "\\Desktop\\END";
    string[] startFiles = System.IO.Directory.GetFiles(START_DIRECTORY);
    string[] endFiles = System.IO.Directory.GetFiles(END_FOLDER_STRING);

    //for every file in start files
    foreach (var startFile in startFiles)
    {
        //get its name
        var startFileName = Path.GetFileName(startFile);
        //and try to get a file with the same name in end file
        var correspondingFineInEndFiles = endFiles.FirstOrDefault(endfile => Path.GetFileName(endfile) == startFileName);
        //if the file in end file does exist
        if (correspondingFineInEndFiles != null)
        {
            //copy it, and override the old file
            File.Copy(startFile, correspondingFineInEndFiles, true);
        }
    }
}
inwenis
  • 368
  • 7
  • 24
  • Thanks! This did the Job perfectly. LINQ may be useful to know about in the future though. – Dibsy Jan 23 '18 at 11:11