Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# get file paths of just files with no extensions

Tags:

c#

path

getfiles

I am wanting to get a string array of paths of files that do not have extensions. They are binary files with no extensions if that helps.

For example, I am loading a group of file paths out of a folder /test/

I want just the path and filenames that do not have a extension (so no .txt, no .csv, no .*)

/test/dontWant.txt

/test/dontWant.csv

/test/doWant

if i do:

String[] paths = Directory.GetFiles(fDir, "*.*", SearchOption.AllDirectories);

I of course get everything in those directories.

if I then try:

String[] paths= Directory.GetFiles(fDir, "*", SearchOption.AllDirectories); 

I will still get everything in that directory.

Is there a way to just get the files of those that have no extension?


using "*." did work, and I don't know why I didn't try that to start with.

I should have been using EnumerateFiles to start with.

like image 447
RKlenka Avatar asked Jun 05 '14 16:06

RKlenka


2 Answers

You can try with this wildcard

String[] paths = Directory.GetFiles(fDir, "*.", SearchOption.AllDirectories);

also you can use this wildcard with Directory.EnumerateFiles

Directory.EnumerateFiles(fDir, "*.", SearchOption.AllDirectories);
like image 167
Grundy Avatar answered Sep 19 '22 23:09

Grundy


This will help:

var filesWithoutExtension = System.IO.Directory.GetFiles(@"D:\temp\").Where(filPath => String.IsNullOrEmpty(System.IO.Path.GetExtension(filPath)));
foreach(string path in filesWithoutExtension)
{
    Console.WriteLine(path);
}

It will return all the files w/o extension only in specified dir. If you want to include all the sub-directories you'd have to use: System.IO.Directory.GetFiles(@"D:\temp\", "*", SearchOption.AllDirectories).

UPDATE
As guys suggested, it's better to use Directory.EnumerateFiles because it consumes less ram.

like image 24
alex.b Avatar answered Sep 20 '22 23:09

alex.b