GetFiles not getting the correct files [duplicate] - c#

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();

Related

How to find files which were last modified 1 year ago [duplicate]

This question already has answers here:
Check last modified date of file in C#
(5 answers)
Closed 3 years ago.
I am writing some code for a program that archives files. So I need to find files which were last modified one year ago.
string[] as_Datien = Directory.GetFiles(s_Pfad, "*.*", SearchOption.AllDirectories);
for (int i_Stelle = 0; i_Stelle < as_Datien.GetLength(0); i_Stelle++)
{
}
I want to check if a file was last modified 1 year ago.
You can try using Linq and FileInfo to get file's last modification date:
DateTime threshold = DateTime.Now.AddYears(-1);
// files which was modified earlier than 1 year ago
string[] as_Datien = Directory
.EnumerateFiles(s_Pfad, "*.*", SearchOption.AllDirectories)
.Where(file => new FileInfo(file).LastWriteTime < threshold)
.ToArray();
You are probably looking for the File.GetLastWriteTime() method.
It returns the date and time of the last edition of files and/or folders.
Check the doc out.
As has already been mentioned use File.GetLastWriteTime() and check that date against today's date one year ago using DateTime.Now.AddYears(-1)

C# search sub directory and files [duplicate]

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.

File.Move all files of type [duplicate]

This question already has answers here:
Visual C#: Move multiple files with the same extensions into another directory
(2 answers)
Closed 5 years ago.
The following code is working, but I need to amend it so it moves all .txt files instead of just the test.txt file.
File.Move(#"C:\Desktop\test\test.txt", #"C:\Desktop\test\old\test.txt");
How can I amend it to do this?
It's moving them from the test folder to the test\old subfolder
whith this you can get all files with path and after that with for you can copy all files to your path
string[] files = System.IO.Directory.GetFiles(path, "*.txt");

How to sort DirectoryInfo.GetFiles() [duplicate]

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))
{
}

Find files not matching a pattern [duplicate]

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();

Categories