Read filename having space in a folder

Hi,

Can anyone suggest the way to read the files in a folder where filename having space in between.

For eg: Test 1.xlsx
a b c2.xlsx
ab d.xlsx
eg for test.xlsx

Kind Regards,
Renju

I would suggest the Path library: Path.GetFileName Method (System.IO) | Microsoft Learn

So you’d have a string variable with a full path called something like MyPath which you would get the filename from by using Path.GetFileName(MyPath)

that should not be a problem when you surround the path with quotes… “ab d.xlsx”

Hi,

I tried to read filename using Directory.GetFiles("D:\ab c"). And I assigned the filenames in a string array. Since the filename having space, it is trying to open ab.xlsx as one file and c.xlsx as another file.
Kind Regards,
Renju

because you are not adding the quotes when your are reading the files like this in your loop:
instead of passing item you pass “”“+item+”“”

1 Like

Hi @bcorrea,

Its working now.

Thanks!

Kind Regards,
Renju

1 Like

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