hankknight
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);
Using ASP.NET/C#, how can I find all files that do NOT have any extension?
I haven't tested it, but it would make sense to try "*." instead of *.txt"
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!
ASKER
From a command prompt, this returns the results I want:
dir *. | find /V "."
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:
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.
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();
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");
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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.
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.