Home...

File Rename Application

This sample application shows how to use x to create an application that renames files in bulk.

Create the Form

  1. in toolbox, from Dialogs tab, drag FolderBrowserDialog onto the form
  2. Drag a GroupBox onto the form, and then drag a listbox, 4 labels, and two buttons into the groupbox. Arrange these controls so that they look something like the Select files groupbox in the diagram
  3. Drag a 2nd GroupBox onto the form, and then drag 3 radio buttons, 3 labels, 3 text boxes and one button onto the groupbox. Arrange the controls to look like the diagram.
  4. Add an Exit button at the bottom of the form, as seen in the following illustration.
UI layout

Add Code

  1. Add a using statement for system.io.


  2. using System.IO;

  3. Add a struc at the top of the Form1 class.


  4. public struct strucFileNames
    {
      public string OldName;
      public string NewName;
      public string Path;

      public strucFileNames(string OldName, string NewName, string Path)
      {
        this.NewName = NewName;
        this.OldName = OldName;
        this.Path = Path;
      }
    }

  5. Add code to the Form1 constructor to initialize the folderBrowserDialog control. Be sure you point to the correct label to display the path.


  6.   InitializeComponent();
      folderBrowserDialog1.SelectedPath = @"c:\";

      if(folderBrowserDialog1.ShowDialog() == DialogResult.OK)
      {
        label2.Text = folderBrowserDialog1.SelectedPath;
      }

  7. Add an event handler for the list box: select listbox, click Events in the Properties Window toolbar, dblclick SelectedIndexChanged


  8.   string currentItem = listBox1.SelectedItem.ToString();
      label4.Text = ((currentItem.Length) - 4).ToString();

  9. Add simple event handlers for 3 of the buttons: clear list, list files, exit.


  10.   private void button1_Click(object sender, EventArgs e)
      {
        listBox1.Items.Clear();
      }

      private void button2_Click(object sender, EventArgs e)
      {
        DirectoryInfo dirFolder = new DirectoryInfo(label2.Text);
        foreach (FileSystemInfo fsi in dirFolder.GetFileSystemInfos())
          listBox1.Items.Add(fsi.Name);
      }

      private void button4_Click(object sender, EventArgs e)
      {
        Application.Exit();
      }

  11. Add the event handler for the file name change button


  12.   private void button3_Click(object sender, EventArgs e)
      {
        //leading text
        if (radioButton1.Checked)
        {
          try
          {
            ListBox.SelectedObjectCollection so = new ListBox.SelectedObjectCollection(listBox1);
            object[] listBoxItems = new object[so.Count];
            so.CopyTo(listBoxItems, 0);
            int intReplaceCount = Convert.ToInt16(textBox1.Text);
            string strReplaceCharacters = textBox3.Text;

            foreach (object file in listBoxItems)
            {
                strucFileNames FileName;
              FileName.OldName = file.ToString();
              FileName.NewName = FileName.OldName.Remove(0, intReplaceCount);
              FileName.NewName = strReplaceCharacters + FileName.NewName;
              FileName.Path = folderBrowserDialog1.SelectedPath;
              File.Move(FileName.Path + @"\" + FileName.OldName, FileName.Path + @"\" + FileName.NewName);
            }
          }
          catch (Exception exc)
          {
            Console.WriteLine("Exception caught - '{0}'", exc.Message);
          }
        }

        //trailing text
        else if (radioButton2.Checked)
        {
          try
          {
            ListBox.SelectedObjectCollection so = new ListBox.SelectedObjectCollection(listBox1);
            object[] listBoxItems = new object[so.Count];
            so.CopyTo(listBoxItems, 0);
            int intRemoveCount = Convert.ToInt16(textBox1.Text);
            string strReplaceCharacters = textBox3.Text;

            foreach (object file in listBoxItems)
            {
              strucFileNames FileName;
              FileName.OldName = file.ToString();

              if (intRemoveCount > 0)
              {
                int intOldNameRemovePoint = Convert.ToInt16((FileName.OldName.Length - 4) - intRemoveCount);
                FileName.NewName = FileName.OldName.Remove(intOldNameRemovePoint, intRemoveCount);
                FileName.NewName = FileName.NewName.Insert(intOldNameRemovePoint, textBox3.Text);
                FileName.Path = folderBrowserDialog1.SelectedPath;
                File.Move(FileName.Path + @"\" + FileName.OldName, FileName.Path + @"\" + FileName.NewName);
              }
            }
          }
          catch (Exception exc)
          {
            Console.WriteLine("Exception caught - '{0}'", exc.Message);
          }
        }

    //inner text
    else if (radioButton3.Checked)
    {
        try
        {
          ListBox.SelectedObjectCollection so = new ListBox.SelectedObjectCollection(listBox1);
          object[] listBoxItems = new object[so.Count];
          so.CopyTo(listBoxItems, 0);
          int ReplaceCount = Convert.ToInt16(textBox1.Text);
          int StartPosition = Convert.ToInt16(textBox2.Text);
          string ReplacementCharacters = textBox3.Text;

          foreach (object file in listBoxItems)
          {
            strucFileNames FileName;
            FileName.OldName = file.ToString();
            FileName.NewName = FileName.OldName.Remove(StartPosition, ReplaceCount);
            FileName.NewName = FileName.NewName.Insert(StartPosition, ReplacementCharacters);
            FileName.Path = folderBrowserDialog1.SelectedPath;
            File.Move(FileName.Path + @"\" + FileName.OldName, FileName.Path + @"\" + FileName.NewName);
          }
        }

        catch (Exception exc)
        {
          Console.WriteLine("Exception caught - '{0}'", exc.Message);
        }
      }
      else MessageBox.Show("Forget to select a radio button?");
    }

Build and Debug