Avatar of hankknight
hankknight
Flag 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?
C#.NET ProgrammingASP.NET

Avatar of undefined
Last Comment
Daniel Kline

8/22/2022 - Mon
Daniel Kline

I haven't tested it, but it would make sense to try "*."  instead of *.txt"
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!
hankknight

ASKER
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?
Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
Jason Evans

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
Easwaran Paramasivam

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
SOLUTION
Log in to continue reading
Log In
Sign up - Free for 7 days
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Daniel Kline

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.
kaufmed

@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.
Get an unlimited membership to EE for less than $4 a week.
Unlimited question asking, solutions, articles and more.
Daniel Kline

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.