Directory.GetFiles with a List of Files

Hi all, how can I read in several different excel files using the Directory.GeFiles function in conjunction with a ForEach item In loop?
The files I want to read in have a specific name such as:

test1.xlsx
Test2.xlsx
Test3.xlsx

I do not want to read all files but in this example only Test1.xlsx and Test3.xlsx.

For each file in Directory.GetFiles(“your_directory”)
Then you can get filename for each file and set your if condition, i.e.

if Path.GetFilename(file.ToString).ToString = test.1.xlsx

Directory.GetFiles(YourDirPath, “Test*.xlsx”) for a prefiltering
Or
Filtering before for each e.g.
arrFiles = Directory.GetFiles(YourDirPath)
arrMask = {“Test1”,“Test3”}
arrFilesFiltered = arrFiles.Where(Function (x) arrMask.Any(Function (f) x.Contains(f))).toArray

3 Likes

Hi @ppr I have tried your suggestion but it does not bring any result, what am I doing wrong?


And Also:
grafik

we would suggest to debug your implmementation and to inspect the variable values to identify the location where it is breaking

Hi @NHoe

In addition to what @ppr said

And if you want a recursive search for all files within the sub-folders of the root directory then

Directory.GetFiles(in_RootFolderPath, “Test*.xlsx”, SearchOption.AllDirectories)

The error was mine, your suggested solution works very well, thank you.

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.