Click here to Skip to main content
Licence 
First Posted 3 Jan 2005
Views 33,501
Bookmarked 19 times

Enhanced File Renaming Utility

By | 14 Jan 2005 | Article
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

About the Author

Nitin Kunte



United States United States

Member

By MNK Infotech Inc

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
Generalwizard Pinmembersteerdwip7:05 8 Nov '06  
GeneralRe-order the Arguments PinmemberMyrddin Emrys5:03 14 Jan '05  
GeneralRe: Re-order the Arguments PinmemberNitin Kunte5:09 14 Jan '05  
Generalxmove PinmemberSébastien Lorion12:27 3 Jan '05  

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

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

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 14 Jan 2005
Article Copyright 2005 by Nitin Kunte
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid