Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I retrieve all filenames in a directory?

How do I retrieve all filenames matching a pattern in a directory? I tried this but it returns the full path instead of the filename.

Directory.GetFiles (path, "*.txt")

Do I have to manually crop the directory path off of the result? It's easy but maybe there is an even simpler solution :)

like image 353
mafu Avatar asked Sep 12 '10 11:09

mafu


People also ask

How do I get a list of filenames in a folder?

3. Type "dir /b > dirlist. txt" without quotes and press "Enter." This creates a list containing file names only. To include file sizes and dates, type "dir > dirlist.

How do you get the names of all files in a directory in C?

Call opendir() function to open all file in present directory. Initialize dr pointer as dr = opendir("."). If(dr) while ((en = readdir(dr)) != NULL) print all the file name using en->d_name.


1 Answers

foreach (string s in Directory.GetFiles(path, "*.txt").Select(Path.GetFileName))
       Console.WriteLine(s);
like image 145
Grif Avatar answered Sep 18 '22 08:09

Grif