Get only file names from folder

Hi,
I need to get all the file names without extension from a folder.
Directory.getfiles(path) gives me the entire path of those files. But I need the result as array of strings with only the file names and not the entire path.

Thanks in advance

Hi @besakkiappan46

System.IO.Path.GetFileNameWithoutExtension(CurrentFile.ToString)

Regards,

Hi,

How about the following?

System.IO.Directory.GetFiles(path).Select(Function(f) System.IO.PAth.GetFileNameWithoutExtension(f)).ToArray()

Or if you need to remove duplication, the following will work.

System.IO.Directory.GetFiles(path).Select(Function(f) System.IO.PAth.GetFileNameWithoutExtension(f)).Distinct().ToArray()

Regards,

1 Like

I’m using the 2nd code to remove duplicates. I get error. Can you confirm whether the braces () are at the right place?
What will be the output of this? I need the result as “Array Of Strings”

Hey! @besakkiappan46 ,

Try this:

directoryPath = "C:\MyFolder"
 files = Directory.GetFiles(directoryPath)
fileNames = files.Select(Function(file) Path.GetFileNameWithoutExtension(file)).ToArray()

Regards,
NaNi

Hi,

What error do you have?
The above returns string array.

Regards,

In the last part of the code, I couldn’t provide neither Distinct() nor ToArray(). It accepts only .ToString . Are the braces at the right place?

System.IO.Directory.GetFiles(path).Select(Function(f) System.IO.PAth.GetFileNameWithoutExtension(f)).ToString

It accepts like this only. But here the result is a string. I couldn’t provide Distinct or ToArray at the end of the code which you provided.

If I tweak it like below, It accepts but I get a different error

System.IO.Directory.GetFiles(path).Select(Function(f) System.IO.PAth.GetFileNameWithoutExtension(f).Distinct().ToArray())

It says,Cannot assign system.Collection.generic.Enumerable system.Char to system.string.

Both the above codes doesn’t provide necessary result.

Hi,

Please type as it is even if candidates are not shown.

Regards,

Worked Yoichi. Thanks a lot :slight_smile:
Marked as Solution

1 Like

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