This question already has answers here:
Using Directory.GetFiles with a regex in C#?
(4 answers)
Exclude certain file extensions when getting files from a directory
(9 answers)
Closed 9 years ago.
For finding all .txt files, we can use this:
Directory.GetFiles(#"c:\","*.txt")
Is there any way to find all files not matching a pattern (for ex: all files not having extension .txt).
You can try LINQ:
var files = Directory.EnumerateFiles("C:\\").Where(x => !x.EndsWith(".txt")).ToList();
No builtin way as search pattern. But you could use Linq:
var files = Directory.EnumerateFiles(dir)
.Where(fn => !Path.GetExtension(fn).Equals(".txt", StringComparison.OrdinalIgnoreCase))
.ToArray();
Note that i've used EnumerateFiles instead of GetFiles. The latter loads al files into memory before you can start processing, with EnumerateFiles you can start enumerating and filtering the collection of names before the whole collection is returned.
use linq
var files = Directory.GetFiles(dir)
.Where(file=> !file.EndsWith(".txt").ToList();
Related
This question already has answers here:
Exact file extension match with GetFiles()?
(8 answers)
Closed 4 years ago.
I need to get all .txt files in all subfolders of a specified folder so I do:
var files = Directory.GetFiles(reportsFolder, "*.txt", SearchOption.AllDirectories);
However, in some folders I also have files with extensions like .txt_TODO, and these are also being retrieved by GetFiles.
How can I skip these files?
Just filter the result by comparing the extension to ".txt", e.g. using LINQ:
var files = Directory.GetFiles(reportsFolder, "*.txt", SearchOption.AllDirectories)
.Where(f => Path.GetExtension(f).Equals(".txt", StringComparison.OrdinalIgnoreCase))
.ToArray();
This question already has answers here:
Getting all file names from a folder using C# [duplicate]
(7 answers)
Closed 4 years ago.
I need to search a root folder for particular subdirectory named "XYZ","ABC". And need to get the filenames from these two folder by iterating these two folder one by one.
I have used the below code to find subdirectory but not sure how to find filenames from this list.
IEnumerable<string> list = Directory.GetDirectories(root).Where(s => s.Equals("XYZ"));
The easiest way would be:
List<string> allFiles = new List<string>();
foreach (string subDir in list)
allFiles.AddRange (Directory.GetFiles (subDir));
The result is all files in both directories in one list with full path included.
This question already has answers here:
Check if value exists in dataTable?
(5 answers)
Closed 6 years ago.
I have a datatable which contain a column Path of file. Now i want to filter file Path is exist or not.
DataTable.Select(File.Exists(ColumnsName))
Would you please help me how may i filter.
You can filter datatable by file path column by checking existence using File.Exists
var result = dataTable.AsEnumerable().Where(r=>File.Exists(r.Field<string>("Path"));
DataSets are a pretty old concept in .NET so to use LINQ you need a bit of extra syntax:
dataTable.Rows.Cast<DataRow>().Select(row => File.Exists(row.Field<String>(columnName)))
This will return an IEnumerable<Boolean> that determines if the files exist.
This question already has answers here:
Sorting Directory.GetFiles()
(13 answers)
Closed 8 years ago.
I am creating the image of the PowerPoint file programmatically. And after saving the Images to the local drive I am getting the files using DirectoryInfo.GetFiles().
I am saving the image files with the sequence numbers.
My Files:
My issue is when I get the files, are not in the sequence I need them in.
The files sequence which I am getting in the FileInfo[] is :
Can any one help me to solve this issue?
The function doesn't make any guarantees about order but you can achieve the desired result with a simple LINQ query;
FileInfo[] sortedFiles = DirectoryInfo.GetFiles().OrderByDescending(x => x.Name).ToArray();
Try this
foreach (FileInfo fi in directory.GetFiles().OrderBy(fi=>fi.FileName))
{
}
This question already has answers here:
How to implement glob in C#
(4 answers)
Closed 6 years ago.
Given a path c:\someFolder\**\*.exe. How can I get a list of files using this directory path. I know that one could use Directory.GetFiles(directoryPath) but this only works when there are no wildcard characters in directoryPath.
See: How to implement glob in C#