Home... |
This sample application shows how to use x to create an application that renames files in bulk.
using System.IO;
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;
  }
}
  InitializeComponent();
  folderBrowserDialog1.SelectedPath = @"c:\";
 
if(folderBrowserDialog1.ShowDialog() == DialogResult.OK)
  {
  label2.Text = folderBrowserDialog1.SelectedPath;
  }
  string currentItem = listBox1.SelectedItem.ToString();
  label4.Text = ((currentItem.Length) - 4).ToString();
  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();
  }
  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?");
}