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.
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);
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.
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.
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))))
{
}
Related
I'm trying to list files on the directory using a wildcard in the directory name.
Something like:
var fileEntries = Directory.GetFiles("C:\\Users\\*\\Desktop\\statistics.txt");
When I'm trying to run this I get an exception about illegal characters.
I don't know what is the username on each PC in my network so it isn't possible to use regex (I think so).
So how can I search for files in a directory using the wildcard on the directory name?
Depending what you want to do with files inside wildcarded path, you can start with:
var dirs = Directory.GetDirectories("c:\\Users", "Desktop*", SearchOption.AllDirectories);
foreach (var d in dirs)
{
var files = Directory.GetFiles(d, "statistics.txt", SearchOption.AllDirectories);
}
One potential option is an overloaded method of Directory.GetFiles, here is documentation.
Using that method, your solution may looks something like:
Directory.GetFiles(
#"C:\Users",
"statistics.txt",
SearchOption.AllDirectories);
Note that SearchOption.AllDirectories will search all subdirectories of "C:\Users" folder. This may have a negative impact on performance, but if you are trying to return all files in a directory and subsequent subdirectories with a certain name, I think it is your best option.
I'm trying to obtain all of the folder paths that have files inside them, while excluding the folder paths that only have other folders in them. I'm using
Directory.GetDirectories(dirPath, "*", SearchOption.AllDirectories);
Which does what I need it to, except that it returns the paths of folders that only have other folders in them.
One way to do this would be to EnumerateFiles for the directory and all it's sub-directories, and get a Distinct() list of their directory names:
List<string> directoriesWithFiles = Directory
.EnumerateFiles(rootDir, "*", SearchOption.AllDirectories)
.Select(Path.GetDirectoryName)
.Distinct()
.ToList();
The first way I thought to do this was to use EnumerateDirectories, and then for each directory use EnumerateFiles to filter out directories that don't contain any files. But this turned out to be much slower than the method above:
List<string> directoriesWithFiles = Directory
.EnumerateDirectories(rootDir, "*", SearchOption.AllDirectories)
.Where(d => Directory.EnumerateFiles(d).Any())
.ToList();
How would I search all the files in directory and all its sub directories for a specific extension
Directory.GetFiles(path, ".txt", SearchOption.AllDirectories);
The code above returns no files
you need to use wild card notation
Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
in your case you're searching for files ".txt" name, instead you need tell to API to retrieve to you all files that has txt extension.
Because you're searching literally for the file named .txt
Use a wildcard character like so: *.txt and it should pull up any .txt files.
See documentation: http://msdn.microsoft.com/en-us/library/ms143316.aspx
I believe its your search pattern or second parameter. should be "*.txt".
Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
The filter needs to be "*.txt":
Directory.GetFiles(path, "*.txt", SearchOption.AllDirectories);
How would one list the files in a directory into an Array? Files only, I could care less for folders. I know in python it's:
for file in os.listdir('Blah'):
#BlahBlahBlah
However, I'm not sure how I would go about doing so in C#.
Thank you for your help!
Use Directory.GetFiles method
string[] filesArray = Directory.GetFiles("yourpath");
Returns the names of files (including their paths) in the specified
directory.
Remember to include System.IO
You can also use Directory.GetFiles Method (String, String) to search files by specifying search patterns. Something like:
string[] fileArray = Directory.GetFiles(#"c:\", "X*");
return all files starting with Character X
You may use:
if(Directory.Exists("yourpath"))
to check if the path exists
using System.IO;
string[] files = Directory.GetFiles("PATH");
OR
string[] files = Directory.GetFiles("PATH","*.docx",SearchOption.AllDirectories);
OR
string[] files = Directory.GetFiles("PATH","*.pdf",SearchOption.TopDirectoryOnly);
OR
string[] files = Directory.GetFiles("PATH","*.xlsx");
Try following...Use System.IO directory
string[] filePaths = Directory.GetFiles(#"D:\MyDir\");
I have a custom object that contains methods that returns all directory and file names in a root
string[] dirlist = obj.GetDirectories();
//which returns all dir names in root
string[] filelist = obj.GetFiles();
//which return all file names in root
I cannot modify these methods. Once I get the dirlist, how do i get a list of all subdirs in it as well as files in the subdirs, ignoring the security exceptions. It can be nested to multiple levels. Anything in .NET 4?
Update: string[] dirList can also be read as List dirlist. Please give a solution that uses the latest features of .NET
DirectoryOne
- SubDirOne
- SubDirTwo
- FileOne
- FileTwo
- SubDirThree
DirectoryTwo
DirectoryOne
There are already built-in .NET methods to do this:
// get all files in folder and sub-folders
Directory.GetFiles(path, "*", SearchOption.AllDirectories);
// get all sub-directories
Directory.GetDirectories(path, "*", SearchOption.AllDirectories);
Somehow I get the feeling this isn't the solution you're looking for though.
Update:
I think I may know what you're trying to ask, since you tagged it as LINQ. If you want to get a list of all sub-directories and sub-folders given a list of directories, you can use the following code:
// get all files given a collection of directories as a string array
dirList.SelectMany(x => Directory.GetFiles(x, "*", SearchOption.AllDirectories));
// get all sub-directories given a collection of directories as a string array
dirList.SelectMany(x => x.Directory.GetDirectories(x, "*", SearchOption.AllDirectories));
Have a look at the Directory Class
foreach(string dir in Directory.GetDirectories("c:\","",SearchOption.AllDirectories))
{
Console.Writeline(dir);
foreach(string file in Directory.GetFiles(dir))
{
Console.Writline(file);
}
}
This will print all directories under C:\ and then all the files in each of those directories.
Does that help?