Charles Morrison
Charles Morrison

Reputation: 428

c# get specific filename

How do I grab the specific filename for a file based on my current read in method,

I am grabbing the files in this manner:

    var lines = Directory.GetFiles(path, "prefix*.csv").Select(
        fn => File.ReadAllLines(fn).Select(a => a.Split(',')).ToList()).ToList();

after each file is done I want to do a move of the file from one location to another but I do not have the exact filename:

     File.Move(path, destPath);

Upvotes: 0

Views: 165

Answers (2)

Jonathon Reinhart
Jonathon Reinhart

Reputation: 137547

Don't abuse LINQ. If you need intermediate pieces of data (like the filename) then break pieces out of your chained LINQ statement to do what you're trying to do.

foreach (var filename in Directory.GetFiles(path, "prefix*.csv)) {
    var linesFromOneFile = File.ReadAllLines(filename)
                               .Select(a => a.Split(',')).ToList();

    // Whatever else with 'filename'
    File.Move(...);
}

Upvotes: 3

walther
walther

Reputation: 13598

In that case I believe it would be wiser to split your operation into multiple steps instead of one chained command. Store your filenames, process it however you need to and then iterate through the collection of filenames and simply move them where you want.

Upvotes: 0

Related Questions