How to delete files while Traversing folder tree - c#

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;
}

Related

Ignore UnauthorizedAccessException while getting Files and SubDirectories of a directory

I am using the code below to get the Files and the SubDirectories of a directory and then populate a TreeView control. I am getting an UnauthorizedAccessException exception. I tried to handle it using a try and catch but in vain...
void GetFilesAndSubDirs(DirectoryInfo root, TreeNodeCollection nodes)
{
FileInfo[] files = null;
DirectoryInfo[] subDirs = null;
try
{
files = root.GetFiles("*.*");
subDirs = root.GetDirectories();
}
catch (UnauthorizedAccessException e)
{
MessageBox.Show(e.Message);
}
catch (DirectoryNotFoundException e)
{
MessageBox.Show(e.Message);
}
TreeNode parent = FindNode(root.Name, nodes);
if (files != null)
{
foreach (FileInfo fiInfo in files)
{
TreeNode fileNode = new TreeNode(fiInfo.Name);
fileNode.ImageIndex = 1;
fileNode.SelectedImageIndex = 1;
parent.Nodes.Add(fileNode);
}
}
if (subDirs != null)
{
foreach (DirectoryInfo dirInfo in subDirs)
{
TreeNode dirNode = new TreeNode(dirInfo.Name);
dirNode.ImageIndex = 0;
dirNode.SelectedImageIndex = 0;
parent.Nodes.Add(dirNode);
GetFilesAndSubDirs(dirInfo, parent.Nodes);
}
}
}
UPDATE #1
When I comment the line of the recursive call, it works just fine.
Is this expected behavior or should you have rights to access this directory?
Have you tried running Visual Studio as an administrator? You as a user might have rights to view it but the application does not necessarily do.
Although the answer and comment are correct to some degree. They're not handling the issue at hand; you "try to catch but in vain" - of course it is in vain - you made it so.
At first you try to get the directory which is named root - you'll get the exception and still try to continue with that same "root" variable, which will be null or at least not set correctly.
When you receive an error message (and for some reason just relate that message to the user directly) you should stop the process. You have an exception (which is by all means a reason to stop processing - it's an unexpected error) - you could never assume the process after the exception is going to run as expected.
I suggest you (in this case) show the messagebox and "return" and don't go forward into the process.
Although it's not holy or sacred - I suggest you'd read up on "defensive programming (C#)"
EDIT #1
Alter the beginning of the method along the line of this:
void GetFilesAndSubDirs(DirectoryInfo root, TreeNodeCollection nodes)
{
FileInfo[] files = null;
DirectoryInfo[] subDirs = null;
try
{
files = root.GetFiles("*.*");
subDirs = root.GetDirectories();
}
catch (UnauthorizedAccessException e)
{
MessageBox.Show(e.Message);
return; // unexpected behavior : notice to user and stop
}
catch (DirectoryNotFoundException e)
{
MessageBox.Show(e.Message);
return; // unexpected behavior : notice to user and stop
}

How can i get all files on disk with a specific extension using 'Directory.getFiles' and save them in a list

I'm doing a console project whose goal is to search the entire disk for all files with the extension '.config'
I've tried something like:
foreach (string file in Directory.GetFiles("C:\\", "*.config", SearchOption.AllDirectories))
{
Console.WriteLine(file);
Console.ReadLine();
}
but gave me an error "denied access to path (...)".
On the internet I found this code:
Stack<string> pending = new Stack<string>();
pending.Push("C:\\");
while (pending.Count != 0)
{
var path = pending.Pop();
string[] next = null;
try
{
next = Directory.GetFiles(path, "*.config");
}
catch { }
if (next != null && next.Length != 0)
foreach (var file in next)
{
Console.WriteLine(file);
Console.ReadLine();
}
try
{
next = Directory.GetDirectories(path);
foreach (var subdir in next) pending.Push(subdir);
}
catch { }
}
but it just shows the path clicking always in 'enter' and I want to save those files/path in a list.
Someone can help?
There are two things you can do to improve that code:
Use Directory.EnumerateFiles() and Directory.EnumerateDirectories() to avoid making a copy of the names of all the files in each directory.
Make the return type of the method IEnumerable<string> to make it easier to consume.
We also need to be very careful about exceptions caused by attempting to access protected files and directories. The code below is also complicated by the fact that you're not allowed to yield return from inside a try/catch block, so we have to rearrange the code somewhat.
(Also note that we have to dispose the enumerator returned from .GetEnumerator(); normally this is done automatically when you use foreach, but in this case we can't - because of having to avoid doing yield return in a try/catch - so we have to use using to dispose it.)
Here's a modification of your original code to do this:
public static IEnumerable<string> GetFiles(string root, string spec)
{
var pending = new Stack<string>(new []{root});
while (pending.Count > 0)
{
var path = pending.Pop();
IEnumerator<string> fileIterator = null;
try
{
fileIterator = Directory.EnumerateFiles(path, spec).GetEnumerator();
}
catch {}
if (fileIterator != null)
{
using (fileIterator)
{
while (true)
{
try
{
if (!fileIterator.MoveNext()) // Throws if file is not accessible.
break;
}
catch { break; }
yield return fileIterator.Current;
}
}
}
IEnumerator<string> dirIterator = null;
try
{
dirIterator = Directory.EnumerateDirectories(path).GetEnumerator();
}
catch {}
if (dirIterator != null)
{
using (dirIterator)
{
while (true)
{
try
{
if (!dirIterator.MoveNext()) // Throws if directory is not accessible.
break;
}
catch { break; }
pending.Push(dirIterator.Current);
}
}
}
}
}
As an example, here's how you could use a console app to list all the accessible ".txt" files on the "C:\" drive:
static void Main()
{
foreach (var file in GetFiles("C:\\", "*.txt"))
{
Console.WriteLine(file);
}
}
Replace the lines
Console.WriteLine(file);
Console.ReadLine();
with a method to store them in a list.
For example
foundFiles.Add(file);
Then when the method is done, you can read all found file paths from this list.
Notes:
This will not yield all files on the system that match the filter.
Only files where your application has access to their respective directory are found this way.
For example the Windows directory and user directories of other users are usually protected. (assuming you run on Windows)
Keep in mind, that some files might be protected independently of their directory.
So when trying to read them, also consider the fact, that the read might fail.
Just encompass the read with a try catch.
Regarding the error "denied access to path (...)", sometimes you have to run Visual Studio as an a administrator in order to access some folders in the C:\ drive.

Get the exception folder to a listbox C#

I'm creating a method to handle directory and file exceptions.
I have listed all the directories and subdirectories of the C: \ Users \ folder
listBox1.Items.AddRange(Directory.GetDirectories("C:\\Users\\", "*", SearchOption.AllDirectories));
Some folders are protected by windows, and when you run a command for those folders it gives us an exception.
If there is any possible, How can I save the folder that returned the exception to a listbox when the exception happens? thanks
If you catch the Exception the Message will tell you the folder that threw the exception in the format of
Access to the path 'C:\Users\SomeFolder' is denied.
you just need to remove the static text, the simplest way is(you maybe able to find a more stylish way to do this, but you get the idea):
try
{
listBox1.Items.AddRange(Directory.GetDirectories("C:\\Users\\", "*", SearchOption.AllDirectories));
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
SomeListBox.Items.Add(ex.Message.Replace("Access to the path '", "").Replace("' is denied.", ""));
}
EDIT: if you want to traverse all the folders and keep going you might want to take a look at recursion. you could slightly modify the above code to make it a function.
void getDirectories(string path)
{
try
{
string[] directories = Directory.GetDirectories(path, "*", SearchOption.TopDirectoryOnly);
listBox1.Items.AddRange(directories);
foreach(string directory in directories)
{
getDirectories(directory);
}
}
catch (Exception ex)
{
//MessageBox.Show(ex.Message);
SomeListBox.Items.Add(ex.Message.Replace("Access to the path '", "").Replace("' is denied.", ""));
}
}
In case you want all folders with exceptions:
You can use the method with the following call:
List<DirectoryInfo> list = GetDirectories(new DirectoryInfo(#"root directory"), 0, 2);
I've implemented a max level, because in a deep structure it could take a long time before the search is done. If you don't need it, just remove the unnecessary lines of code ;)
List<DirectoryInfo> GetDirectories(DirectoryInfo di, int level, int maxLevel)
{
List<DirectoryInfo> exceptionList = new List<DirectoryInfo>();
foreach (DirectoryInfo directoryInfo in di.GetDirectories("*", SearchOption.TopDirectoryOnly))
{
try
{
DirectorySecurity directorySecurity = directoryInfo.GetAccessControl();
if (level + 1 < maxLevel)
{
exceptionList.AddRange(GetDirectories(directoryInfo, level + 1, maxLevel));
}
}
catch (UnauthorizedAccessException)
{
exceptionList.Add(directoryInfo);
}
}
return exceptionList;
}

UnauthorizedAccessException with getDirectories

Hello everyone I currently got subdirectories I wanted through this call:
foreach (DirectoryInfo dir in parent)
{
try
{
subDirectories = dir.GetDirectories().Where(d => d.Exists == true).ToArray();
}
catch(UnauthorizedAccessException e)
{
Console.WriteLine(e.Message);
}
foreach (DirectoryInfo subdir in subDirectories)
{
Console.WriteLine(subdir);
var temp = new List<DirectoryInfo>();
temp = subdir.GetDirectories("*", SearchOption.AllDirectories).Where(d => reg.IsMatch(d.Name)).Where((d => !d.FullName.EndsWith("TESTS"))).Where(d => !(d.GetDirectories().Length == 0 && d.GetFiles().Length == 0)).Where(d => d.GetFiles().Length > 3).ToList();
candidates.AddRange(temp);
}
}
foreach(DirectoryInfo dir in candidates)
{
Console.WriteLine(dir);
}
so now my issue is that my final list called candidates I get nothing because im getting an access issue due to one of the folders called lost+found in my subdirectories folder in the try block. I tried using try and catch to handle the exception so I could keep doing my checks I actually dont care about this folder and im trying to just ignore it but I'm not sure how to go about ignoring it out of my get directories search any thoughts? I already tried doing a filter with .where to ignore any folder that contained the folder name but that didnt work either it just stopped my program at the folder name.
I have the same question (ResourceContext.GetForCurrentView call exception) about this exception (UnauthorizedAccessException), and this link gives an answer to the reason why this happens:
http://www.blackwasp.co.uk/FolderRecursion.aspx
Short quote:
... Key amongst these is that some of the folders that you attempt to
read could be configured so that the current user may not access them.
Rather than ignoring folders to which you have restricted access, the
method throws an UnauthorizedAccessException. However, we can
circumvent this problem by creating our own recursive folder search
code. ...
solution:
private static void ShowAllFoldersUnder(string path, int indent)
{
try
{
foreach (string folder in Directory.GetDirectories(path))
{
Console.WriteLine("{0}{1}", new string(' ', indent), Path.GetFileName(folder));
ShowAllFoldersUnder(folder, indent + 2);
}
}
catch (UnauthorizedAccessException) { }
}
You can use recursion like Microsoft explains: link.

How do I check the "cut" permission of a folder in windows based on .net

In my project ,I want to move a folder to a destination.Here is my thought.
First scenario, I want to check Can I move the folder,If I don't have the permission I will not check the sub-items in the folder. Move action is done.
second scenario,If I have the permisson to move the folder,I will check all the sub-items in the folder,then move the items that I can move and leave the items that I can't move.
So I don't know How to implement the first scenario.If I catch the unauthorizedaccessexception in the scenario.I may block the second scenario,because in the second scenario,while moving folder,it will also throw the exception if some sub-items can not be moved.Can someone give me some suggestions?
I did not test this but it should give you a start:
public static void MoveDirectory(string source, string target)
{
var delSourceAtEnd = true;
var sourcePath = source.TrimEnd('\\', ' ');
var targetPath = target.TrimEnd('\\', ' ');
var files = Directory.EnumerateFiles(sourcePath, "*", SearchOption.AllDirectories)
.GroupBy(Path.GetDirectoryName);
foreach (var folder in files)
{
var failed = false;
var targetFolder = folder.Key.Replace(sourcePath, targetPath);
Directory.CreateDirectory(targetFolder);
foreach (var file in folder)
{
var targetFile = Path.Combine(targetFolder, Path.GetFileName(file));
try
{
File.Move(file, targetFile);
} catch (UnauthorizedAccessException uae)
{
failed = true;
delSourceAtEnd = false;
}
}
if (!failed) Directory.Delete(folder.Key);
}
if (delSourceAtEnd) Directory.Delete(source, false);
}
This is heavily based on this answer, which shows different options how you can move a directory manually and handle single files and folders individually.

Categories