Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7867211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T00:39:05+00:00 2026-06-03T00:39:05+00:00

I am using Directory.GetFiles to find files that will be copied. I need to

  • 0

I am using Directory.GetFiles to find files that will be copied. I need to find the paths of the files so I can use copy, but I have no idea how to find the path.
It iterates through the files fine, but I can’t copy or move them because I need the file’s source path.

This is what I have:

string[] files = Directory.GetFiles(sourcePath, "*.*", SearchOption.AllDirectories);

System.Console.WriteLine("Files Found");

// Display all the files.
foreach (string file in files)
{
  string extension = Path.GetExtension(file);
  string thenameofdoom = Path.GetFileNameWithoutExtension(file);
  string filename = Path.GetFileName(file);

  bool b = false;
  string newlocation = (@"\\TEST12CVG\Public\Posts\Temporaryjunk\");

  if (extension == ".pst" || 
    extension == ".tec" || 
    extension == ".pas" || 
    extension == ".snc" || 
    extension == ".cst")
  {
    b = true;
  }

  if (thenameofdoom == "Plasma" || 
    thenameofdoom == "Oxygas" || 
    thenameofdoom == "plasma" || 
    thenameofdoom == "oxygas" || 
    thenameofdoom == "Oxyfuel" || 
    thenameofdoom == "oxyfuel")
  {
    b = false;
  }

  if (b == true)
  {
    File.Copy(file, newlocation + thenameofdoom);
    System.Console.WriteLine("Success: " + filename);
    b = false;
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T00:39:07+00:00Added an answer on June 3, 2026 at 12:39 am

    Path.GetFullPath works, but also consider using FileInfo as it comes with many file helper methods.

    I would use a method similar to this (could use a lot more error handling (try catches…) but it’s a good start

    EDIT I noticed that you are filtering out the extensions, but requiring them, update to code allows for that

    class BackupOptions
    {
      public IEnumerable<string> ExtensionsToAllow { get; set; }
      public IEnumerable<string> ExtensionsToIgnore { get; set; }
      public IEnumerable<string> NamesToIgnore { get; set; }
      public bool CaseInsensitive { get; set; }
    
      public BackupOptions()
      {
        ExtensionsToAllow = new string[] { };
        ExtensionsToIgnore = new string[] { };
        NamesToIgnore = new string[] { };
      }
    }
    
    static void Backup(string sourcePath, string destinationPath, BackupOptions options = null)
    {
    
      if (options == null)
        optionns = new BackupOptions();
    
      string[] files = Directory.GetFiles(sourcePath, ".", SearchOption.AllDirectories);
      StringComparison comp = options.CaseInsensitive ? StringComparison.CurrentCultureIgnoreCase : StringComparison.CurrentCulture;
    
      foreach (var file in files)
      {
        FileInfo info = new FileInfo(file);
    
        if (options.ExtensionsToAllow.Count() > 0 &&
          !options.ExtensionsToAllow.Any(allow => info.Extension.Equals(allow, comp)))
          continue;
    
        if (options.ExtensionsToIgnore.Any(ignore => info.Extension.Equals(ignore, comp)))
            continue;
    
        if (options.NamesToIgnore.Any(ignore => info.Name.Equals(ignore, comp)))
          continue;
    
        try
        {
          File.Copy(info.FullName, destinationPath + "\\" + info.Name);
        }
        catch (Exception ex)
        {
          // report/handle error
        }
      }
    }
    

    With a call like:

    var options = new BackupOptions
    {
      ExtensionsToAllow = new string[] { ".pst", ".tec", ".pas", ".snc", ".cst" },
      NamesToIgnore = new string[] { "Plasma", "Oxygas", "Oxyfuel" },
      CaseInsensitive = true
    };
    
    Backup("D:\\temp", "D:\\backup", options);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using a basic Directory.GetFiles to find the files i want to use. But
Apparently I can't move files on different volumes using Directory.Move. I have read that
I need to copy multiple files from a directory using a textfile that doesnt
Writing a simple program that will find exact duplicate files on my computer, but
I have a controller that returns a list of files in a directory using
I have following foreach-loop: using System.IO; //... if (Directory.Exists(path)) { foreach(string strFile in Directory.GetFiles(path,
how can I save all files in a directory using utf-8? There is a
I'm using Directory.GetFiles() to list files according to given pattern. This works fine for
I'm using the Directory.GetFiles() method to get a list of files to operate on.
I want to find all excel files within a directory structure using recursion. The

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.