Rename A File Using DOS Command

We can use FileInfo.MoveTo method to rename a file in C#. The following code snippet renames a source file into a new file by using the MoveTo method.  

  1. // Source file to be renamed  
  2. string sourceFile = @"C:\Temp\MaheshChand.jpg";  
  3. // Create a FileInfo  
  4. System.IO.FileInfo fi = new System.IO.FileInfo(sourceFile);  
  5. // Check if file is there  
  6. if (fi.Exists)  
  7. {  
  8.     // Move file with a new name. Hence renamed.  
  9.     fi.MoveTo(@"C:\Temp\Mahesh.jpg");  
  10.     Console.WriteLine("File Renamed.");  
  11. }  
Moving Files Applicaiton 
The following article is an application that uses UI to bulk move files. 
This helps us to learn how to use console commands in C# and the basics of background workers. Some of us may spend a lot of time renaming files. This application allows us to rename multiple files at a time.


I must rename many documents in my office. For renaming, it may take too much time to do it. Or I need to depend upon another third-party application. So I decided to create my own application.

Using the Code

Let me explain my application in a step-by-step manner.

Step 1

I have created an empty form. In the form, I have placed a 2 Command buttons, a Background Worker, a FolderBrowserDialog and a GridView as shown below:

upload excel

Step 2

Create an Excel file that can have an old file name and a new file name, then import the Excel file into the GridView.


Step 3

Call the folderbrowserdialog to choose the file to be renamed.


Step 4

Then, I have entered the file rename code in a renamer method as below:
  1. int datas=dataGridView1.Rows.Count;                                                                              
  2. for(int i=0;i<=datas-1;i++)          
  3. {                                             
  4.    String oldname,newname;                                                       
  5.    oldname=dataGridView1.Rows[i].Cells[0].Value.ToString();                 
  6.    newname=dataGridView1.Rows[i].Cells[1].Value.ToString();                                                   
  7.    System.Diagnostics.Process process = new System.Diagnostics.Process();                    
  8.    System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();                 
  9.    startInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden;                                      
  10.    startInfo.FileName = "cmd.exe";                                                           
  11.    startInfo.WorkingDirectory=@""+location+"";                                                                        
  12.    startInfo.Arguments = "/k Rename "+oldname+" "+newname;   
  13.    process.StartInfo = startInfo;                                                                             
  14.    process.Start();          
  15. }   
First, I have counted the number of rows available in Excel and then assigned that value to an integer.
  1. int datas=dataGridView1.Rows.Count; // count the number of rows in the gridview   
I have used a loop to execute depending on the number of rows available in the GridView. This helps us to execute all the values available in the GridView.
  1. for(int i=0;i<=datas-1;i++)   
Then, I have taken the old filename and the new filename from the GridView rows.
  1. String oldname,newname;   
  2. oldname=dataGridView1.Rows[i].Cells[0].Value.ToString();   
  3. newname=dataGridView1.Rows[i].Cells[1].Value.ToString();    
Then, I created an object for the process and startinfo.
  1. System.Diagnostics.ProcessStartInfo startInfo = new System.Diagnostics.ProcessStartInfo();    
  2. System.Diagnostics.Process process = new System.Diagnostics.Process();   
Open the command prompt using the following code:
  1. startInfo.FileName = "cmd.exe";   
Set the working directory:
  1. startInfo.WorkingDirectory=@""+location+"";   
Send the Argument to rename the file using the following line:
  1. startInfo.Arguments = "/k Rename "+oldname+" "+newname;   
In this article and application, we learned how to move files of a folder.