Monday, April 22, 2013

Folder Renamer

Folder Renamer

With source code in visual basic. Free to use.


hyphen
Last afternoon, I when to a little party for my friend’s wife in the restaurant: Pacini at Sainte-Julie in the Québec Province. I take a lot of picture with my camera from Canon. Using the ZoomBrowser Ex from Canon, I could easily import all the pictures I want in my computer. The program automatically creates folder and sob folder based on the year, the month, the day and a description. Cool! But the problem is the date separator. All the folders created with the Canon’s ZoomBrowser Ex uses underscore “_” and I want to use hyphen “-”. I checked everywhere in that software I haven’t found any option related to the separator. What happens if I have a lot of pictures in a lot of folder? I have to rename then one by one? No way!
Honestly, I love my little camera from Canon and I like to use the importer for years, but I never thought I could program a little program to rename my folder the way I would like them.  Today, I decided to program a little application to help me rename all the folders under my Pictures folders. That way, I won’t have to look for a folder because they are sorted differently because of the underscore versus hyphen.



For those who want to download my program, there is no problem. The program is free to use. Otherwise, I will take one moment to explain the code.

form1

To make a program that renames all my folders under my Pictures Folder, I used a few objects:

  1. FolderBrowserDialog
  2. TextBox
  3. Button
  4. Checkbox
  5. Form


I think that is all.

I also use my same find files and find directory from one of my previous post: search file - Improve performance
I slightly change it to handle sub-folders. I also use two TextBox to get the character to find and another to replace. Here is a piece of code written in Visual Basic:


For Each foundDirectory As String In My.Computer.FileSystem.GetDirectories(dirPath, FileIO.SearchOption.SearchTopLevelOnly, String_to_Search)

               Dim aArray() As String = foundDirectory.Split("\")
                Dim sFolder1 As String = aArray(aArray.Length - 1)
                Dim sFolder2 As String = sFolder1.Replace(TextBox2.Text, TextBox3.Text)
                My.Computer.FileSystem.RenameDirectory(foundDirectory,
sFolder2)

            Next


Yes I know, I declare the variable inside the FOR loop and I know is not a good practice. Sorry, I only want to show something quickly and I wanted to preserve one of the previous codes.
I use the “My.Computer.FileSystem.RenameDirectory” method to rename each directory that contains the little keyword. That keyword is in TextBox2. I replace it with the character inside TextBox3.

I also use the method “My.Computer.FileSystem.GetDirectories” to easily retrieve all folders that matches TextBox2.

renamed
Of course, I made this quickly; there is not enough robustness in the programming. We have to check if the path doesn’t exceed the system-defined maximum length. You could read this interesting article: Long Paths in .NET, Part 1 of 3 [Kim Hamilton]. Also have to make sure the inside TextBox1, it doesn’t contain any invalid path, empty path or anything with invalid char such as the semi- colon “:”. You also have to make sure TextBox1 is not a relative path that starts with “\\” or “\”.
Inside the code, I also put very basic permission verification. You have to put more stuff to make the program better. (I haven’t).



Reference:

Project with code sample : ReplaceCharInFolder.zip
Download the program from this page : Folder Renamer Download

http://www.pacini.ca/qc/



SQL Server Developer Edition 2012 and Microsoft Visual Studio Pro 2012 are very cheap on Amazon ! Free Delivery. Check here:
  




No comments:

Post a Comment