foreach loop finally - c#

I have a foreach loop that has try catch and finally. it goes through files in a folder and finally deletes them all....but now if i have two files, as it is deleting all( i need to delete all because of app functionality) it will not go though second file. is it possible to let foreach loop finish then delete all files in foreach method here is an example:
foreach (DirectoryInfo directory in directories)
{
foreach (FileInfo file in directory.GetFiles("*.csv"))
{
try
{
//do something with file
}
catch (Exception e)
{
//catch exception
}
finally
{
if (!IsFileLocked(file))
{
string[] files = Directory.GetFiles(#"C:\Test");
foreach (string filetoDelete in files)
{
File.Delete(filetoDelete);
}
}
}
}
}
hope it is clear enough.

Your code:
Loops through given set of directories and for each of them:
Gets "*.csv" files in that dir and with each file:
Does something and then:
Deletes files from "C:\Test".
Shouldn't you be deleting the file you just processed, or at least files from the dir you are currently processing, not files from "C:\Test"?

I'm not very sure if I'm right understood what you're asking for, but to me it seems solution pretty trivial, like this:
foreach (DirectoryInfo directory in directories)
{
foreach (FileInfo file in directory.GetFiles("*.csv"))
{
try
{
//do something with file
}
catch (Exception e)
{
//catch exception
}
}
foreach(FileInfo fileToDeleteInfo...)
{
try
{
if (!IsFileLocked(fileToDeleteInfo))
{
string[] files = Directory.GetFiles(#"C:\Test");
foreach (string filetoDelete in files)
{
File.Delete(filetoDelete);
}
}
}
catch(...)
{... }
}
In other words, move file deletion from the second (nested) foreach into the new one.
If this is not what you're asking for, please clarify.

Related

C# delete all the things inside the file

I'm trying to delete some files from a specific directory. But there are some errors.
I want my program to ignore these problems.
string[] myFiles = Directory.GetFiles(#"C:\Windows\prefetch");
foreach (string f in myFiles)
{
File.Delete(f);
}
By "errors" you mean thrown exceptions? Directory.GetFiles and File.Delete will throw exceptions if they can't find the directory/file or if you dont have permissions to access it, etc.
Since you want them ignored, you can just catch them and ignore them.
try
{
string[] myFiles = Directory.GetFiles(#"C:\Windows\prefetch");
foreach (string f in myFiles)
{
File.Delete(f);
}
}
catch (Exception)
{
//do nothing
}
use this and you will delete the entire directory and respective files recursively
Directory.Delete(#"C:\Windows\prefetch", true);

Temp files are not deleting

Hi i am actually creating a program to delete all the temp files and folders when an event is triggered. so for that i am using the following code but its not working and throwing an exception
And also for this i am using the code
private void tempfiles_Click(object sender, EventArgs e)
{
if(tempcheck.Checked)
{
string tempPath = System.IO.Path.GetTempPath();
System.IO.DirectoryInfo di = new DirectoryInfo(tempPath);
try
{
foreach (FileInfo file in di.GetFiles())
{
file.Delete();
}
foreach (DirectoryInfo dir in di.GetDirectories())
{
dir.Delete(true);
}
}
catch(Exception env)
{
MessageBox.Show("Please close all the applications and try \n" + env);
}
}
else
{
MessageBox.Show("Please check the checkbox");
}
}
Here i want to delete the folders and files without any exception but in java i use method like fileonclose().
If files are being used by another program, you will not be able to delete them. You should put your try...catch statement inside each for loop. That way you can continue trying to delete files even if one attempt fails.
To make your program more useful, you can keep track of which files were not deleted and create a log or open a window to show the file names to the user.

Delete path that is longer than 256 characters using DirectoryInfo.Delete(true) method in c#

My Goal is to use Delete the entire folder and everything inside of that folder that is older than month. Following is code that i wrote:
foreach (String dir in Directory.EnumerateDirectories(args[0]))
{
foreach (String subDir in Directory.EnumerateDirectories(dir))
{
DirectoryInfo dirInfo = new DirectoryInfo(subDir);
if (dirInfo.CreationTime < DateTime.Today.AddMonths(-1))
{
try
{
dirInfo.Delete(true);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
Isssue: dirInfo.Delete(true) statement throws an exception when path is longer than 256 characters, and does not delete the folder. How can i over come that?
Thank you
You could try moving the folder first to a shorter directory path first so its path is no longer as long, and then deleting it.
dirInfo.MoveTo("C:\temp");
dirInfo.Delete(true);

How do I give my code permission to delete files

I got my code which delete some files, Its kinda weird because its not throwing me any errors.
Essentially whats happening is that, its not deleting any files in the folder(not sure why) I've tried giving it permission to delete Only-Read files but that didnt work.
I've tried debugging but its not showing me anything.
And I understand that it cannot delete files that are being used somewhere else, I've tried deleting a few files manually and that worked, not sure why my code is not doing it automatically though, because if it hits an error it should move on the the next file right?
(This mainly applies to .rar files but also with some folders)
private void Clear_Click(object sender, EventArgs e)
{
string tempPath = Path.GetTempPath();
DirectoryInfo di = new DirectoryInfo(tempPath);
foreach (DirectoryInfo dir in di.GetDirectories())
{
try
{
dir.Delete(true);
}
catch (Exception)
{
// Log error.
continue;
}
}
//-----------------------------------------------------------------------------------------------------
//foreach (FileInfo file in di.GetFiles())
foreach (FileInfo file in di.GetFiles())
{
try
{
file.Delete();
}
catch (Exception)
{
// Log error.
continue;
}
}
//-----------------------------------------------------------------------------------------------------
// string[] extentionList = new[] { "*.jpg", "*.rar", "*.bmp", "*.gif" };
//Delete RAR Files
foreach (FileInfo file in di.GetFiles("*.rar"))
{
try
{
file.Delete();
}
catch
{
//log error I tried running it without the "Continue;" but it still wouldnt log me an error.
continue;
}
}

How to delete files while Traversing folder tree

I am not sure if I am doing this correctly or if my logic is correct.
I am trying to go down a folder structure delete files older than a certain number of days, this part I have correctly implemented, delete empty folders.
Can all this be done in one loop?
Where do I do the folder delete?
I want to delete empty folders up to 3 or 4 level down.
private static void TraverseTree(System.IO.DirectoryInfo folder, double days)
{
Stack<string> dirs = new Stack<string>();
if (!folder.Exists)
throw new ArgumentException();
dirs.Push(folder.FullName);
while (dirs.Count > 0)
{
string currentDir = dirs.Pop();
string[] subDirs;
try
{
subDirs = System.IO.Directory.GetDirectories(currentDir);
}
// An UnauthorizedAccessException exception will be thrown if we do not have
// discovery permission on a folder or file. It may or may not be acceptable
// to ignore the exception and continue enumerating the remaining files and
// folders. It is also possible (but unlikely) that a DirectoryNotFound exception
// will be raised. This will happen if currentDir has been deleted by
// another application or thread after our call to Directory.Exists. The
// choice of which exceptions to catch depends entirely on the specific task
// you are intending to perform and also on how much you know with certainty
// about the systems on which this code will run.
catch (UnauthorizedAccessException e)
{
Console.WriteLine(e.Message);
continue;
}
catch (System.IO.DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
continue;
}
string[] files = null;
try
{
files = System.IO.Directory.GetFiles(currentDir);
}
catch (UnauthorizedAccessException e)
{
Console.WriteLine(e.Message);
continue;
}
catch (System.IO.DirectoryNotFoundException e)
{
Console.WriteLine(e.Message);
continue;
}
// Perform the required action on each file here.
// Modify this block to perform your required task.
foreach (string file in files)
{
try
{
// Perform whatever action is required in your scenario.
System.IO.FileInfo fi = new System.IO.FileInfo(file);
Console.WriteLine("{0}: {1}, {2}", fi.Name, fi.Length, fi.CreationTime);
// Delete old files
if (fi.LastWriteTime < DateTime.Now.AddDays(-days))
fi.Delete();
}
catch (System.IO.FileNotFoundException e)
{
// If file was deleted by a separate application
// or thread since the call to TraverseTree()
// then just continue.
Console.WriteLine(e.Message);
continue;
}
}
// Push the subdirectories onto the stack for traversal.
// This could also be done before handing the files.
foreach (string str in subDirs)
dirs.Push(str);
}
}
Code is from MSDN.
A recursive approach would probably be cleaner.
private static void DeleteOldFilesAndFolders(string path)
{
foreach (string directory in System.IO.Directory.GetDirectories(path))
{
DeleteOldFilesAndFolders(directory);
// If the directory is empty and old, delete it here.
}
foreach (string file in System.IO.Directory.GetFiles(path))
{
// Check the file's age and delete it if it's old.
}
}
Something that I notice about your code is that the dozens of lines of "mechanism" for walking the tree structure completely overwhelms the two lines of code that actually do the work. That makes it hard to read, understand, change, debug and maintain this code.
Here's what I would do instead.
You have only three high-level operations in your program: (1) get all the files, (2) filter to find the ones to delete, (3) delete each file. So write a program which does each of those in one statement.
For the first operation, I would factor out the mechanism above into its own function: a function which implements, say, IEnumerable, and all it does is keeps on yielding out information about files. It doesn't do anything with them; its only purpose is to keep spitting out file infos.
Once you have that mechanism then you can start writing queries on top of that sequence to attain the second operation. The third operation then follows directly from the second.
In short, the main line of your program should look something like this:
var allFiles = TraverseFolder(folder);
var filesToDelete = from file in allFiles where IsOld(file) select file;
foreach(var fileToDelete in filesToDelete) Delete(fileToDelete);
Is that clear?
There is almost the same question here:
How to delete all files and folders in a directory?
This is delete by name, but you could check other properties.
Here is a more general solution to the problem which gives you a file system walker implemented non-recursively as IEnumerable.
for which your solution can probably be implemented as:
List<string> directoriesToDelete = new List<string>();
DirectoryWalker walker = new DirectoryWalker(#"C:\pathToSource\src",
dir => {
if (Directory.GetFileSystemEntries(dir).Length == 0) {
directoriesToDelete.Add(dir);
return false;
}
return true;
},
file => {
if (FileIsTooOld(file)) {
return true;
}
return false;
}
);
foreach (string file in walker)
File.Delete(file);
foreach (string dir in directoriesToDelete)
Directory.Delete(dir);
I enhanced John's solution, implementing missing code, error handling and checks:
/* Given a directory path and a datetime,
* recursively delete all files and directories contained in such directory
* (given directory included) that are younger than the given date.
*/
private bool DeleteDirectoryTree(string dir, DateTime keepFilesYoungerThan)
{
//checks
if (String.IsNullOrEmpty(dir) || !Directory.Exists(dir))
return false;
//recurse on children directories
foreach (string childDir in Directory.GetDirectories(dir))
DeleteDirectoryTree(childDir, keepFilesYoungerThan);
//loop on children files
foreach (string file in Directory.GetFiles(dir))
{
//calculate file datetime
DateTime fileDT = new DateTime(Math.Max(File.GetCreationTime(file).Ticks, File.GetLastWriteTime(file).Ticks));
//if file is old, delete it
if (fileDT <= keepFilesYoungerThan)
try
{
File.Delete(file);
Log("Deleted file " + file);
}
catch (Exception e)
{
LogError("Could not delete file " + file + ", cause: " + e.Message);
}
}
//if this directory is empty, delete it
if (!Directory.EnumerateFileSystemEntries(dir).Any())
try
{
Directory.Delete(dir);
Log("Deleted directory " + dir);
}
catch (Exception e)
{
LogError("Could not delete directory " + dir + ", cause: " + e.Message);
}
return true;
}

Categories