PnP
PnP

Reputation: 3185

Copy files exclude one folder

Basically I want to copy everything within a folder excluding one folder, which happens to be where logs are stored for my program. (I know I could just store logs elsewhere, but I have my reasons). So far I have:

private void btnCopyFiles_Click(object sender, EventArgs e)
    {
        try
        {
            //Copy all the files
            foreach (string newPath in Directory.GetFiles(@"\\xxx\yyy", "*.*",
                            SearchOption.AllDirectories)
                            .Where(p => p != Logging.fullLoggingPath))
                File.Copy(newPath, newPath.Replace(@"\\xxx\yyy", @"C:\bbb"));
            using (StreamWriter w = File.AppendText(Logging.fullLoggingPath))
            {
                Logging.Log("All necessary files copied to C:\\bbb", w);
            }
        }
        catch
        {
            using (StreamWriter w = File.AppendText(Logging.fullLoggingPath))
            {
                Logging.Log("Error copying files, make sure you have permissions to access the drive and write to the folder.", w);
            }
        }
    }

How can I modify this to exclude one specific folder within \\xxx\yyy

Upvotes: 1

Views: 3792

Answers (3)

Sorceri
Sorceri

Reputation: 8033

Check for the name of the folder and skip it

foreach (string newPath in Directory.GetFiles(@"\\xxx\yyy", "*.*", SearchOption.AllDirectories))
{
    String currentFolder = "";
    int lastSlash = newPath.LastIndexOf("\\");
    int secondLastSlash = newPath.Substring(0, newPath.Length - (newPath.Length - lastSlash)).LastIndexOf("\\")+1;
        currentFolder = newPath.Substring(secondLastSlash,(lastSlash-secondLastSlash))

    If(!currentFolder.Equals(NameOfFolderNotToInclude))
        File.Copy(newPath, newPath.Replace(@"\\xxx\yyy", @"C:\bbb"), true);
}
        using (StreamWriter w = File.AppendText(Logging.fullLoggingPath))
        {
            Logging.Log("All necessary files copied to C:\\bbb", w);
        }

lastSlash variable is the position of the last \ in the string which indicates the text after it is the actual file. We then create a substring that removes everything after the last \ in the string to the path of the file. This gives us a path up to the folder. We then utilize the substring to find the next \ so that we can extract out just the specific folder.

so in a path such as C:\users\someone\desktop\test.txt lastSlash would indicate the \ before test.txt using this we create a substring that is equal to C:\users\someone\desktop we then use the same method to find the last \ in this path we combine it all together to extract the folder of desktop.

You can use Jim Mischel's suggestion as it is more user friendly and readable. Example using that below

foreach (string newPath in Directory.GetFiles(@"\\xxx\yyy", "*.*", SearchOption.AllDirectories))
{
    if(Path.GetDirectory(newPath) != PathToExclude)
        File.Copy(newPath, newPath.Replace(@"\\xxx\yyy", @"C:\bbb"), true);
}
        using (StreamWriter w = File.AppendText(Logging.fullLoggingPath))
        {
            Logging.Log("All necessary files copied to C:\\bbb", w);
        }

Upvotes: 3

Jim Mischel
Jim Mischel

Reputation: 134035

In your loop, just add a conditional to exclude that directory

If (Path.GetDirectory(newPath) == PathToExclude)
    Continue;

Upvotes: 5

Alireza
Alireza

Reputation: 10486

    foreach (string newPath in Directory.GetFiles(@"\\xxx\yyy", "*.*",
                    SearchOption.AllDirectories)
                    .Where(p=>p!=Logging.fullLoggingPath))
                File.Copy(newPath, newPath.Replace(@"\\xxx\yyy", @"C:\bbb"));
    using (StreamWriter w = File.AppendText(Logging.fullLoggingPath))
    {
         Logging.Log("All necessary files copied to C:\\bbb", w);
    }

Upvotes: 4

Related Questions