Click here to Skip to main content
15,867,568 members
Articles / Programming Languages / C#
Article

Enhanced File Renaming Utility

Rate me:
Please Sign up or sign in to vote.
3.24/5 (13 votes)
14 Jan 20052 min read 46.1K   316   20   4
Enhanced file renaming utility to improve on the "ren" command provided by MS DOS.

Sample Image - FileUtil.jpg

Introduction

Whenever I download images from my digital camera, they are named as Image_001.jpg, Image_002.jpg, Image_003.jpg... and so on. I wanted to get rid of the "Image_" from the file names. I tried using the "REN" or "RENAME" command in MS DOS. However, I quickly found that this command would not let me strip characters from a file name. It will allow me to change the file names in bulk or increase the characters in bulk but would not let me remove a few characters in bulk. That is when I thought of writing this very small command line utility to rename the file names to whatever we want. This is in initial stages and hence lacks a few things. But I am giving away the source code for you to work on.

Description

The project simply consists of a generic function used for renaming files, a function to understand the input arguments being passed and one to display help.

Main Function

The main function will validate the input arguments for null and Length. If they are not proper, help will be shown. If they are proper, then the function to parse the arguments will be called.
C#
static void Main(string[] args)
{
    if ((args != null) && (args.Length > 0)) 
    { 
        DoStuff(args);
    } 
    else 
    { 
        WriteHelp(); 
    } 
} 

Parsing the arguments

The function DoStuff() will take the input arguments, parse them and then call the worker function which does the renaming. This function first makes sure that right arguments are being passed.

C#
if (((args[0].ToString().ToLower() == "ren") 
                    || (args[0].ToString().ToLower() == "rename"))
                    && (args.Length >= 3))

If the arguments are proper, then it will call the worker function - renameFiles(...).

C#
bool ret = renameFiles(workingFolder, findVal, replaceVal);

Otherwise, it will call the function which displays help - WriteHelp().

C#
bool ret = renameFiles(workingFolder, findVal, replaceVal);

Renaming the files

The function renameFiles(...) takes inputs for folder path, search string and replace string. It will then use the FILEINFO object to rename the files.

C#
private static bool renameFiles(string folderPath, 
                           string findString, string replaceWith) 
{
    bool ret = false;
    try
    {
        DirectoryInfo di = new DirectoryInfo(folderPath);
        string destFileName = "";
        foreach (FileInfo fi in di.GetFiles())
        {
            if (fi.Name.ToLower().IndexOf(findString) > -1 )
            {
                destFileName = fi.DirectoryName + "\\" + 
                fi.Name.ToLower().Replace(findString, replaceWith);
                File.Move(fi.FullName, destFileName);
            }
        }
        ret = true;
    }
    catch(System.Exception ex)
    {
        throw new ApplicationException("Error in renameFiles",ex);
    }
    return ret;
}

Conclusion

I have started this project to add enhancements to commands found in DOS. If you find any such idea, do write to me. I will try to incorporate it in the next release.

Update (01/14/2005)

I have updated the code so that the path is now the last parameter and optional. Thanks for the suggestion Myrddin Emrys made.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
United States United States

Comments and Discussions

 
Generalwizard Pin
steerdwip8-Nov-06 7:05
steerdwip8-Nov-06 7:05 
GeneralRe-order the Arguments Pin
Myrddin Emrys14-Jan-05 5:03
Myrddin Emrys14-Jan-05 5:03 
GeneralRe: Re-order the Arguments Pin
Nitin Kunte14-Jan-05 5:09
Nitin Kunte14-Jan-05 5:09 
Generalxmove Pin
Sebastien Lorion3-Jan-05 12:27
Sebastien Lorion3-Jan-05 12:27 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.