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 8608547
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T03:38:24+00:00 2026-06-12T03:38:24+00:00

I have the following code: var directories = Directory.GetDirectories( environmentSettings.SourcePath, *, SearchOption.AllDirectories) .Where(dir =>

  • 0

I have the following code:

var directories =  Directory.GetDirectories(
        environmentSettings.SourcePath, 
        "*",
        SearchOption.AllDirectories)
    .Where(dir => !environmentSettings.FolderExclusions
                                      .Contains(Path.GetFileName(dir)));

I want to create a directory in the target path except for ones that show up in the exclusion list. This works, but only if the directory is directly under the root and does not contain sub-directories.

As an example, if the exclusion list contains a directory called Custom and the root directory is C:\App, it will exclude C:\App\Custom and not create it in the target path, but as soon as it encounters something like C:\App\Custom\Sub, it will end up creating this on the target path.

Can’t get it to work for File:

//Copy all the files

var files = Directory.GetFiles(environmentSettings.SourcePath, "*", SearchOption.AllDirectories)
      .Select(file => new FileInfo(file))
      .Where(file => !environmentSettings.FolderExclusions
                                         .Contains(file.Name) && 
                     !environmentSettings.FolderExclusions
                                         .Contains(file.Directory.Name));

The problem above, is that I don’t know how to tell how nested the file is, for example, if it is under c:\App\Custom\thumbs.db, it works fine, I think, but if it is under c:\App\Custom\sub1\sub2\thumbs.db, it still copies the file and the Custom directory which is what I don’t want. I basically need to get the directory right below c:\App and if that is Custom, then I exclude the file.

var files = Directory.GetFiles(environmentSettings.SourcePath, "*", SearchOption.AllDirectories)
            .Select(file => new FileInfo(file))
            .Where(file => !environmentSettings.FolderExclusions.Contains(file.Directory.Name) && (file.Directory.Parent == null || !environmentSettings.FolderExclusions.Contains(file.Directory.Parent.Name)));

When I run the following code to loop through all my files and put them in a target directory, I run into a Directory not found exception:

//Copy all the files
foreach (var file in files)
{
File.Copy(file.Name, file.FullName.Replace(environmentSettings.SourcePath, environmentSettings.TargetPath));
}

The problem is that the source file maybe something like \network\app1\one.mp3 and my target directory might be c:\programdata\myapp. It is saying it can’t find one.mp3 when copying the files from the source. Not sure how to handle this.

  • 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-12T03:38:26+00:00Added an answer on June 12, 2026 at 3:38 am

    Try this if this goes just for the parent folder and the first sub folder:

    var directories =  Directory.GetDirectories(environmentSettings.SourcePath, "*", SearchOption.AllDirectories)
        .Select(dir => new DirectoryInfo(dir))
        .Where(dir=>!excludes.Contains(dir.Name) && (dir.Parent == null || !excludes.Contains(dir.Parent.Name)));
    

    To enable this with files, try the following

    var files = Directory.GetFiles(sourcePath, "*", SearchOption.AllDirectories)
        .Select(file => new FileInfo(file))
        .Where(file=>!excludes.Contains(file.Directory.Name) && (file.Directory.Parent == null || !excludes.Contains(file.Directory.Parent.Name)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following code: var submitHandler = function ($link, $form, close) { var
I have the following code: var refreshId = setInterval(function() { $(#footad).html('myjshere'); }, 15500); Where
I have the following code: var carsContext = new CarsDataContext(); IQueryable<Car> allCars = carsContext.Cars;
I have the following code var dbvalue = Ned; var deleteLink = '<a href=#delete
I have the following code: var sl: THashedStringList; begin sl:= THashedStringList.Create; sl.Duplicates := dupIgnore;
I have the following code: var bool:String = true; Without an if block or
I have the following code: var GoalPanelView = Backbone.View.extend({ // Bind to the goal
I have the following code: var tlp = new TableLayoutPanel { Location = new
I am using ASP.NET 3.5 with iTextSharp and I have the following code: var
Working with Google Maps, I have the following code: var tileNames = [base, parking,

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.