We help IT Professionals succeed at work.

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

hankknight
hankknight asked
on
2,528 Views
Last Modified: 2014-05-12
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?
Comment
Watch Question

Daniel KlineSr. SharePoint Developer

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

Author

Commented:
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!

Author

Commented:
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?
Jason EvansSenior Software Developer

Commented:
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

This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Daniel KlineSr. SharePoint Developer
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015
Commented:
This one is on us!
(Get your first solution completely free - no credit card required)
UNLOCK SOLUTION
Daniel KlineSr. SharePoint Developer

Commented:
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.
CERTIFIED EXPERT
Most Valuable Expert 2011
Top Expert 2015

Commented:
@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.
Daniel KlineSr. SharePoint Developer

Commented:
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.

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Join our community and discover your potential

Experts Exchange is the only place where you can interact directly with leading experts in the technology field. Become a member today and access the collective knowledge of thousands of technology experts.

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.