I need to check a directory to see if there are any files whose file name contains a specific keyword and if there are, to delete them. Is this possible?
For example, delete all existing files in "C:\Folder" whose file name contains the keyword "Apple".
To expand on Henk's answer, you need:
string rootFolderPath = #"C:\\SomeFolder\\AnotherFolder\\FolderCOntainingThingsToDelete";
string filesToDelete = #"*DeleteMe*.doc"; // Only delete DOC files containing "DeleteMe" in their filenames
string[] fileList = System.IO.Directory.GetFiles(rootFolderPath, filesToDelete);
foreach(string file in fileList)
{
System.Diagnostics.Debug.WriteLine(file + "will be deleted");
// System.IO.File.Delete(file);
}
BE VERY CAREFUL!
Note that I've commented out the delete command. Run it and test it carefully before you let it actually delete anything!
If you wish to recursively delete files in ALL subfolders of the root folder, add ,System.IO.SearchOption.AllDirectories); to the GetFiles call.
If you do this it is also a very good idea to refuse to run if the rootFolderPath is less than about 4 characters long (a simple protection against deleting everything in C:\ - I've been there and done that and it's not fun!!!)
You can use System.IO.Directory.GetFiles() to a list of the files, in string[] format.
Then you can use System.IO.File.ReadAllText() to read complete files, or if they are very big, open a TextReader with System.IO.File.OpenText().
If you are looking for a literal keyword, String.Contains() is all you need.
Deleting a file can be done with System.IO.File.Delete(). Make sure the file is closed again.
Edit, 2 examples of GetFiles():
string[] fileNames = System.IO.Directory.GetFiles(#"C:\");
string[] fileNames = System.IO.Directory.GetFiles(#"C:\", #"*.sys");
new List<string>(Directory.GetFiles(#"C:\Folder")).ForEach(file => {
if (file.IndexOf("apple", StringComparison.OrdinalIgnoreCase) >= 0)
File.Delete(file);
});
or
new List<string>(Directory.GetFiles(#"C:\Folder")).ForEach(file => {
Regex re = new Regex("apple", RegexOptions.IgnoreCase);
if (re.IsMatch(file))
File.Delete(file);
});
More or less, this:
string DeleteThis = "apple";
string[] Files = Directory.GetFiles(#"C:\Folder");
foreach (string file in Files)
{
if (file.ToUpper().Contains(DeleteThis.ToUpper()))
{
File.Delete(file);
}
}
new List<string>(Directory.GetFiles(#"C:\Folder")).ForEach(file => { if (file.ToUpper().Contains("apple".ToUpper())) File.Delete(file); });
Related
i am writing a short code to move files from one directory to another. My code is simple, working fine and looks like this:
public void copy()
{
string sourcePath = #"/Users/philip/Desktop/start";
string destinationPath = #"/Users/philip/Desktop/Ziel";
string[] files = Directory.GetFiles(sourcePath)
foreach (string s in files)
{
string fileName = System.IO.Path.GetFileName(s);
string destFile = System.IO.Path.Combine(destinationPath, fileName);
System.IO.File.Copy(s, destFile, true);
}
}
The Programm gets all files from the sourcepath and combines the targetpath in the foreach loop vor every file, containing of target path and filename. Then it moves it. Everything works fine.
My aim is now, not to store all files from my directory into the string array. I only want to get the files that have CreationTime after 01.07.2021. Is there an easy and quick way to do it?
I already used this to get the files, but it specifies a singular date and not all files after a specific date:
var files = Directory.GetFiles(sourcePath).Where(x => new FileInfo(x).CreationTime.Date == DateTime.Today.Date);
I would be glad if you could help me out.
Best regards,
Liam
If you want to avoid having to check the creation date on every single FileInfo you can order your files. Like so:
var directory = new DirectoryInfo(sourcePath);
var fileInfos = directory.GetFiles().OrderByDescending(fileInfo => fileInfo.CreationDate);
var result = new List<FileInfo>();
foreach (var fileInfo in fileInfos)
{
if (fileInfo.CreationDate >= DateTime.Today)
result.Add(fileInfo);
else
break; // We can break early, because we ordered our dates descending
// meaning every date after this one is smaller
}
This has upsides and downsides, ordering a huge collection of files could take longer than "just" simply iterating over all and comparing the dates, but you'll need to benchmark it on your own
You could use FileInfo
FileInfo fileInfo = new(s);
if (fileInfo.CreationTime >= DateTime.Parse("01/07/2021"))
{
...
}
So I'm working on a program that will list all the files in a directory. Pretty simple. Basically, when I do this: List<string> dirs = new List<string>(Directory.EnumerateFiles(target));, I don't want it to include the directory and all. Just the file name. When I run my code;
List<string> dirs = new List<string>(Directory.EnumerateFiles(target));
Console.WriteLine($"Folders and files in this directory:\n");
foreach (string i in dirs) {
Console.WriteLine($"> {i}");
}
it gives me the following:
C:\Users\Camden\Desktop\Programming\Visual Studio\C#\DirectoryManager\DirectoryManager\bin\Debug\DirectoryManager.exe
I just want the DirectoryManager.exe part, so I looked it up and I found that you can replace strings inside of strings. Like so: i.Replace(target, "");. However, this isn't doing anything, and it's just running like normal. Why isn't it replacing, and how should I instead do this?
Use methods from the System.IO.Path class.
var fullfile = #"C:\Users\Camden\Desktop\Programming\Visual Studio\C#\DirectoryManager\DirectoryManager\bin\Debug\DirectoryManager.exe";
var fileName = Path.GetFileName(fullfile); // DirectoryManager.exe
var name = Path.GetFileNameWithoutExtension(fullfile); // DirectoryManager
The simplest way is to use the Select IEnumerable extension
(you need to have a using Linq; at the top of your source code file)
List<string> files = new List<string>(Directory.EnumerateFiles(target)
.Select(x => Path.GetFileName(x)));
In this way the sequence of files retrieved by Directory.EnumerateFiles is passed, one by one, to the Select method where each fullfile name (x) is passed to Path.GetFileName to produce a new sequence of just filenames.
This sequence is then returned as a parameter to the List constructor.
And about your question on the Replace method. Remember that the Replace method doesn't change the string that you use to call the method, but returns a new string with the replacement executed. In NET strings are immutable.
So if you want to look at the replacement you need
string justFileName = i.Replace(target, "");
An alternative to using Directory.EnumerateFiles, would be DirectoryInfo.EnumerateFiles. This method returns an IEnumerable<FileInfo>. You can then make use of the FileInfo.Name property of each of the returned objects. Your code would then become:
var files = new DirectoryInfo(target).EnumerateFiles();
Console.WriteLine("Files in this directory:\n");
foreach (FileInfo i in files) {
Console.WriteLine($"> {i.Name}");
}
For just the list of file names:
List<string> fileNames = new DirectoryInfo(target).EnumerateFiles().Select(f => f.Name).ToList();
Alternatively, if you want both files and directories, you can use EnumerateFileSystemInfos. If you need to know if you have a file vs a directory you can query the Attributes property and compare it to the FileAttributes flags enumeration.
var dirsAndFiles = new DirectoryInfo(target).EnumerateFileSystemInfos();
Console.WriteLine("Folders and files in this directory:\n");
foreach (var i in dirsAndFiles) {
var type = (i.Attributes & FileAttributes.Directory) == FileAttributes.Directory ? "Directory" : "File";
Console.WriteLine($"{type} > {i.Name}");
}
The FileSystemInfo.Name property will return either the file's name (in case of a file) or the last directory in the hierarchy (for a directory)--so just the subdirectory name and not the full path ("sub" instead of "c:\sub").
I have json files that i'm trying to classify so the file names are as such:
inputTestingSetting_test
inputTestingSetting_test1310
inputTestingSetting_test1310_ckf
inputTestingSetting_test1310_ols
inputTestingSetting_test1310_sum
inputTestingSetting_test1311_ckf
inputTestingSetting_test1311_ols
inputTestingSetting_test1311_sum
So the output that i want in the ListBox lbJsonFileNames will be
test
test1310
test1311
currently my codes are
DirectoryInfo dInfo = new DirectoryInfo(tbJSFolder.Text);
FileInfo[] Files = dInfo.GetFiles("*.json");
List<jSonName> jsonName = new List<jSonName>();
foreach (FileInfo file in Files)
{
string filename = Path.GetFileNameWithoutExtension(file.Name);
string[] fileNameSplit = filename.Split('_');
jsonName = new List<jSonName>{
new jSonName(fileNameSplit[0],fileNameSplit[1])
};
for(int i=0;i<jsonName.Count;i++)
{
if(jsonName[i].TestNumber == fileNameSplit[1])
{
lbJsonFileNames.Items.Add(jsonName[i].TestNumber);
}
}
}
so my output for lbJsonFileNames is what i want, however it is repeated. is it possible to just show one? i've tried to put jsonName[i].TestNumber to jsonName[i+1].TestNumber. but failed as it is out of range.
is there a way to read the file names, and then compare it with the previous file name to see if it is the same? and if it is the same, ignore, move on to the next file name, if it's different then it is added into the ListBox
changed my codes to
DirectoryInfo dInfo = new DirectoryInfo(tbJSFolder.Text);
FileInfo[] Files = dInfo.GetFiles("*.json");
List<jSonName> jsonName = new List<jSonName>();
HashSet<string> fileNames = new HashSet<string>();
foreach (FileInfo file in Files)
{
string filename = Path.GetFileNameWithoutExtension(file.Name);
string[] fileNameSplit = filename.Split('_');
fileNames.Add(fileNameSplit[1]);
}
foreach(var value in fileNames)
{
lbJsonFileNames.Items.Add(value);
}
got what i want now thanks all~
Your code basically says to put the following into list box:
test
test1310
test1310
test1310
test1310
test1311
test1311
test1311
Before you add as in lbJsonFileNames.Items.Add(jsonName[i].TestNumber);, check for duplicate first. Maybe you can put that list into a Set variable. Set will automatically remove the duplicate. Then put the Set back to lbJsonFileNames.
[Edit] Sorry there is no Set in dot net. Please use HashSet instead.[/Edit]
Your code did not mention what jSonName class is like and the constructor parameters stand for. However to get your output from your input can be much easier:
string[] all = Directory.GetFiles(tbJSFolder.Text, "*.json")
.Select(x => Path.GetFileNameWithoutExtension(x))
.Select(x => x.Split(new char[] { '_' })[1])
.Distinct().ToArray();
lbJsonFileNames.Items.AddRange(all);
In my C: drive I have a folder called "Temp" and in 3 text files called
abc.txt
pqr.txt
xyz.txt
Using the following code:
DirectoryInfo directory = new DirectoryInfo(#"C:\Temp");
FileInfo[] Files = directory.GetFiles("*.txt");
List<string> filenames = new List<string>();
foreach (FileInfo file in Files)
{
filenames.Add(file.Name);
}
I have got all the names of the files in a list. I also have a folder on the C: drive called "Results". Using this list, can someone tell me how I can create text files in the "Results" folder with the names:
result_abc.txt
result_pqr.txt
result_xyz.txt
You could simply do the following to create a set of new empty files from your input.
filenames.ForEach(f => File.Create(Path.Combine(#"C:\Results", "result_" + f)).Dispose());
Also, to get the filenames, you could have used the shorter
var filenames = Files.Select(fi => fi.Name).ToList();
instead of the foreach.
Finally, if you are not doing anything else with the Files, you could reduce this to:
foreach (var fi in new DirectoryInfo(#"C:\Temp").EnumerateFiles("*.txt"))
File.Create(Path.Combine(#"C:\Results", "result_" + fi.Name)).Dispose();
I have a folder that contains these files:
Erb3PCustsExport-303_20080318_223505_000.xml
Erb3PCustsExport-303_20080319_063109_000_Empty.xml
Erb3PCustsImport-303_20080319_123456.xml
Erb3PDelCustsExport-303_20080319_062410_000.xml
Erb3PResosExport-303_20080318_223505_000_Empty.xml
Erb3PResosExport-303_20080319_062409_000.xml
I just care about the files that have CustsExport word in their names.
My question:
How to get these files?
What I have tried:
I got the folder name from app settings section in App.Config like this:
string folderPath = ConfigurationManager.AppSettings["xmlFolder"];
Then I got all the file names like this:
foreach (string file in Directory.EnumerateFiles(folderPath, "*.xml"))
{
}
My problem:
In that way, I got all the files. However, I am just interested in files that have CustsExport in their names.
Could you help me please?
Note:
I am working on .NET 4.5
Try this:
foreach (string file in Directory.EnumerateFiles(folderPath, "*CustsExport*.xml"))
{
}
Or your can use regex:
Regex reg = new Regex(#".*CustsExport.*\.xml",RegexOptions.IgnoreCase);
var files = Directory.GetFiles(yourPath, "*.xml")
.Where(path => reg.IsMatch(path))
.ToList();
You can use string.Contains("")
string folderPath = ConfigurationManager.AppSettings["xmlFolder"];
foreach (string file in Directory.EnumerateFiles(folderPath, "*.xml"))
{
if (file.Contains("CustsExport"))
{
//add your code
}
}