Delta Engine Blog

AI, Robotics, multiplatform game development and Strict programming language

Annoying Files Remover Tool


AnnoyingFilesRemover.zip (6.1 KB)

Recently I had to remove a lot of .svn and Thumbs.db files from certain directories because I wanted to move them or someone else had created thumb files (which is really annoying). After deleteing those files by hand a couple of times I decided it would be better to have some tool doing that for me. That should be easy to do, shouldn't it?

I started by opening VS Orcas and coding for a while, but for some reason the commenter plugin for CodeRush was not working anymore, which already annoyed me. Then the Intellisense crashed several times, but VS Orcas did not close, which was a nice feature. But without CodeRush it was a little bit annoying to generate all that code by hand and then to write all the comments also by hand. I removed the LINQ test code I used to go through all directories and added some foreach loops (just 2 lines more, how cares) and went back to VS 2005 for this project.

The next big problem was the removal of read-only files and directories, which is just not allowed in the .NET framework, you will get the following exception:

System.UnauthorizedAccessException occurred
  Message="Der Zugriff auf den Pfad entries wurde verweigert."
  Source="mscorlib"
  StackTrace:
       bei System.IO.Directory.DeleteHelper(String fullPath, String userPath, Boolean recursive)
       bei System.IO.Directory.Delete(String fullPath, String userPath, Boolean recursive)
       bei System.IO.Directory.Delete(String path, Boolean recursive)
       bei AnnoyingFilesRemover.RemoverForm.SafeDeleteDirectory(String directory) in C:\code\Communities\AnnoyingFilesRemover\RemoverForm.cs:Zeile 153.

The MSDN help for Directory.Delete gives you the explanation that this happens because you are trying to delete a read-only file.

   UnauthorizedAccessException: The caller does not have the required permission.

This is only half the story because if you look in File.Delete you see a more detailed error explanation:

    UnauthorizedAccessException: The caller does not have the required permission.

    -or- path is a directory.

    -or- path specified a read-only file.


And the directory I wanted to delete was ".svn", which is read-only and hidden. An easy way around that is to change the directory attributes. But that won't help if there are still files in the directory because you will still get the same error. Instead you have to replace all the file attribute for all sub directories and files too. I wrote the following method to do that.

#region RemoveAllAttributes
/// <summary>
/// Helper to remove all file and sub directory readonly attributes
/// to make SafeDeleteDirectory work!
/// </summary>
/// <param name="directory">Directory to change (will be deleted later
/// anyway)</param>
private static void RemoveAllAttributes(string directory)
{
    // Get all files and sub directories.
    string[] files = Directory.GetFiles(directory);
    string[] directories = Directory.GetDirectories(directory);

    // Kill all attributes, make files normal and directories just directories
    foreach (string file in files)
        File.SetAttributes(file, FileAttributes.Normal);
    foreach (string dir in directories)
        RemoveAllAttributes(dir);

    // Finally set the main directory to a normal directory!
    File.SetAttributes(directory, FileAttributes.Directory);
} // RemoveAllAttributes(directory)
#endregion


The rest of the tool is easy to understand and there is nothing special about this tool except that I find it very useful right now. It is also very fast, I let it run over 20 000 files and it was done immediately and had deleted several hundert files in the process. Again: Be careful what you type in the filters textbox and save important directories before messing with them.

As always, here is the download and source code (both in .NET 2.0 since I have removed all LINQ features that were not important for this project anyway):

Comments (1) -

  • notebook

    4/9/2010 10:58:09 AM |

    Loved to read your blog. I would like to suggest you that traffic show most people read blogs on Mondays. So it should encourage bloggers to write new write ups over the weekend primarily.

Comments are closed