Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Enhanced File Renaming Utility

0.00/5 (No votes)
14 Jan 2005 1  
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.
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.

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(...).

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

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

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.

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