C# get file paths of just files with no extensions
Asked Answered
C

4

16

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.

Circular answered 5/6, 2014 at 16:56 Comment(9)
try String[] paths = Directory.GetFiles(fDir, "*.", SearchOption.AllDirectories);Flabellate
@Flabellate Nope. That'd be any file name ending with a dot. I don't know of any file like that!Rattray
@tnw, are you try it???Flabellate
@yep, it works :-), because file without extension have name "name."Flabellate
@Flabellate Interesting, I honestly did not expect that to work. Just tried it myself and it does. CoolRattray
@RKlenka, are you try this wildcard "*."?Flabellate
A pattern of 'star dot' as above passed to Directory.GetFiles seems to match files without extensions but also files with no names and ONLY extensions. I am seeing this in .NET 3.5. Anybody else seeing behavior like this? Any ideas on why this is happening?Hrutkay
“using "*." did work” why not working for me?Liquor
EnumerateFiles is not good if you create directory during the process.Liquor
D
12

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.

Dunnite answered 5/6, 2014 at 17:2 Comment(2)
This code works like a boss. Just tested it in a console app.Henriettahenriette
EnumerateFiles is not good if you create directory during the process.Liquor
F
23

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);
Flabellate answered 5/6, 2014 at 17:11 Comment(1)
Although accepted answer works nice, but I prefer this solution because it's more readable and also less code used inCarnation
D
12

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.

Dunnite answered 5/6, 2014 at 17:2 Comment(2)
This code works like a boss. Just tested it in a console app.Henriettahenriette
EnumerateFiles is not good if you create directory during the process.Liquor
S
9

You will need to do a 2nd pass filter on it.

//If you are using .NET 3.5 you can still use GetFiles, EnumerateFiles will just use less ram.
String[] paths = Directory.EnumerateFiles(fDir, "*.*", SearchOption.AllDirectories)
                          .Where(file => Path.GetFileName(file) == Path.GetFileNameWithoutExtension(file))
                          .ToArray();

So what this does is it passes your file path to GetFileName and GetFileNameWithoutExtension, if both of those return the same string it then includes the result in the array.

Stirring answered 5/6, 2014 at 17:5 Comment(2)
+1 for Directory.EnumerateFiles and using less ram.Dunnite
EnumerateFiles is not good if you create directory during the process.Liquor
P
3

As an alternative to aleksey.berezan's answer, you can do the following in .NET 4+. EnumerateFiles will return files as they are traversed in the directory tree.

foreach(var file in Directory.EnumerateFiles(fDir, "*.*", SearchOption.AllDirectories).Where(s => string.IsNullOrEmpty(Path.GetExtension(s))))
{

}
Petrifaction answered 5/6, 2014 at 17:6 Comment(1)
EnumerateFiles is not good if you create directory during the process.Liquor

© 2022 - 2024 — McMap. All rights reserved.