Renaming Files using C#

This is an example of how to use C# to rename files. This example takes a passed path e.g. C:\Temp\ and a ‘new name’ and renames all files in the directory with the ‘new name’ plus an underscore and an incremental number.

E.g.

if the directory C:\Temp\ contains:

File.csv
MyData.csv
AnotherFile.csv
Extract.csv

Calling the function and passing “C:\Temp\” and “Import” would rename the files to:

Import_1.csv
Import_2.csv
Import_3.csv
Import_4.csv

I use code similar to this to rename data files that come from various sources, with various names but contain commonly formatted data. However the idea can be used for many things.

Firstly we need to add a reference to System.IO

Using System.IO;

Next we fill an array with the the files in the directory, using FileInfo[] and DirectoryInfo.

It is then simply a process of looping through the files, getting the current name and then renaming to the new name. NB: C# does not have a rename option for files. However, moving the file to the same location replaces the existing file with the same one with the new name.

        private void bulkRename(string fPath, string fNewName)
        {
            string fExt;
            string fFromName;
            string fToName;
            int i = 1;
            
            //copy all files from fPath to files array
            FileInfo[] files = new DirectoryInfo(fPath).GetFiles();
            //loop through all files
            foreach (var f in files)
            {
                //get the filename without the extension
                fFromName = Path.GetFileNameWithoutExtension(f.Name);
                //get the file extension
                fExt = Path.GetExtension(f.Name);

                //set fFromName to the path + name of the existing file
                fFromName = string.Format("{0}{1}", fPath, f.Name);
                //set the fToName as path + new name + _i + file extension
                fToName = string.Format("{0}{1}_{2}{3}", fPath, fNewName,i.ToString(), fExt);

                //rename the file by moving to the same place and renaming
                File.Move(fFromName, fToName);
                //increment i
                i++;
            }
        }

One thing with this code is that if the new file name already exists the move will fail causing an error. I’ve left the error trapping and workaround out of this example to simplify.

Advertisements
This entry was posted in Programming and tagged , , , . Bookmark the permalink.

2 Responses to Renaming Files using C#

  1. mghptl says:

    Thanks… its very helpful

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s