BlockLeftTop, PRELOAD BlockLeftBottom, PRELOAD BlockLeftStretch, PRELOAD BlockTop, PRELOAD BlockBottom, PRELOAD BlockStretch, PRELOAD BlockRightTop, PRELOAD BlockRightBottom, PRELOAD BlockRightStretch, PRELOAD
DeltaEngine

Annoying Files Remover Tool

by Benjamin Nitschke 6. April 2007 23:25

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


4/12/2007 4:50:06 PM #

Wo sind denn die 6 Kommentare hingekommen???

Dann schreibe ich es halt nochmals:
Nice easter bunny, I wish you a nice week Wink

Christian (killerbees19) | Reply



4/14/2007 2:11:17 PM #

WTF?
Which evilguy deletes the thumbs.db files?
Every little more experienced developer which browses with file-preview on,
through large directories, knows how boring and time-expensive it is,
to wait for the explorer to process and show all the file thumbnails.
And if it's completely done you're very thankful and praise the the lord for the result.
Know you can save enormous time browsing this directory. Just click on it.
A few more new/changed pictures have to be processed, and done.

Yes, there are other file-previewers out there.
But no-one is as handy as this built-in service!

A much, much more interesting thing would be an auto-indexer service which runs on the server and notices directory changes and updates the thumbs.db each time a picture has been changed or added.

That would be really nice. Thanx Smile

Jan Diederich | Reply



4/9/2010 8: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.

notebook | Reply



4/18/2010 5:35:49 PM #

Blown away by the content and quality of your site.  I like the layout and the archives.  Keep up the good stuff, ill check you out again soon.

us | Reply



4/24/2010 3:45:42 PM #

I would just say one thing to you and that is, “FANTASTIC”!! Keep it up and wish to get more details from your blog.

drills cordless | Reply



4/27/2010 11:35:40 PM #

I\'m happy I found this blog, I couldnt discover any info on this subject matter prior to. I also run a site and if you want to ever serious in a little bit of guest writing for me if possible feel free to let me know, i\'m always look for people to check out my site. Please stop by and leave a comment sometime!

Rapidshare | Reply



5/4/2010 7:28:32 PM #

AnnoyingFilesRemover. zip (6. 1 KB)Recently I had to remove a lot of .

Rapidshare Search engine | Reply


Add comment




biuquote
  • Comment
  • Preview
Loading



Disclaimer: The opinions expressed in this blog are own personal opinions and do not represent the companies view.
© 2000-2011 exDream GmbH & MobileBits GmbH. All rights reserved. Legal/Impressum

Poll

Which platform should Soulcraft be released on next?











Show Results Poll Archive

Recent Games

Soulcraft

Fireburst

Jobs @ exDream

Calendar

<<  May 2012  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910