Link to home
Start Free TrialLog in
Avatar of hankknight
hankknightFlag for Canada

asked on

ASP.NET/C#: Find all files without an extension

I use this to find all .txt files on drive d:
var FilesPath = Directory.GetFiles(@"d:\", "*.txt", SearchOption.AllDirectories);

Open in new window

Using ASP.NET/C#, how can I find all files that do NOT have any extension?
Avatar of Daniel Kline
Daniel Kline
Flag of United States of America image

I haven't tested it, but it would make sense to try "*."  instead of *.txt"
Avatar of hankknight

ASKER

Using "*." was a good idea but it does not work.  Strangly it returns results like ".xyz" which is odd.  If you have a chance, please test this and make a recommendation.  Thanks!
From a command prompt, this returns the results I want:
dir *. | find /V "."

Open in new window

Could that be used somehow with my C# code?
I tried Daniel's suggestion of "*." and that worked for me.

As a plan B, you could try this:

string dir = @"D:\Tmp";

var files = Directory.GetFiles(dir, "*.*", SearchOption.AllDirectories)
    Where(f => !Path.HasExtension(f))
    .ToList();

Open in new window


Which will return a list of filenames which do not have an extension. This might be useful to you.

You could create FileInfo instances of each filename, if you wanted to get more detail about a file, rather then just it's name e.g.

var fileInfo = new FileInfo("C:\test");

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Easwaran Paramasivam
Easwaran Paramasivam
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Kaufmed... reread the request.  He wanted to find all the files without extensions.  He didn't ask for files that didn't have ".txt" in the extension.  According to convention, the two examples you gave do have the extensions you identified and would thus be excluded from the desired result.
@Daniel Kline

Please understand my comment. The point of my post is that, using GetExtension, any filename that contains a dot will be interpreted as having an extension--the extension being whatever is to the right of the dot. This may or may not be an extension. My example was to simply highlight the scenario where a misbegotten program (or user) forgot to add the extension to a file. In those cases, GetExtension would return incorrect results. Don't focus on my use of .txt--it was only an example.
Not a problem.  It's a good example of different perspectives that lead to different solutions all based on the same requirements.  That's what makes our jobs so challenging, reading between the lines, but not too much.  If there is one challenge in a developers career its sharpening the ability to crystalize requirements and solutions from the users best efforts at communicating their needs.