Rename File Using Console Command in C#

Introduction

This tip allows us to rename multiple files at a time. 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.

Background

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.

gridview

Step 3

Call the folderbrowserdialog to choose the file to be renamed.

folderbrowserdialog

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;   
I have explained only the Rename code.

References 

Up Next
    Ebook Download
    View all
    FileInfo in C#
    Read by 9.7k people
    Download Now!
    Learn
    View all