![]() |
Languages »
C# »
Utilities
Beginner
License: The Code Project Open License (CPOL)
Batch Renamer in C#By Mohammad DayyanRenames files |
C# 3.0, Windows (WinXP, Vista), .NET (.NET 3.5), Visual Studio (VS2008)
|
|
Advanced Search Add to IE Search |
|
|
|
||||||||||||||||
In this project, you will familiarize yourself with Random and Directory and File classes and a little with the Thread class.
There were some applications that did this work like FileRen.
For batch renaming, first we must get the user's folder name:
private void ChooseFolder_Click(object sender, EventArgs e)
{
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
{
SelectedPathText.Text = folderBrowserDialog1.SelectedPath;
}
}
Second, we must get all of the file names into the above folder. For this, I used the Thread class.
private void rename_Click(object sender, EventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure ?",
"Warning", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.Yes)
{
Thread tr = new Thread(new ThreadStart(chnTarget));
tr.Start();
}
}
private void chnTarget()
{
this.Invoke(new ThreadStart(ChangeName));
}
Then I wrote the ChangeName method:
private void ChangeName()
{
Random rand = new Random(DateTime.Now.Second);
string folder_path = SelectedPathText.Text;
long progress_value = 0;
string new_filename = "";
cancel = false;
progressBar1.Value = 0;
try
{
string[] FullPathFileNames = System.IO.Directory.GetFiles(folder_path);
progressBar1.Maximum = FullPathFileNames.Length;
if (FullPathFileNames.Length == 0) throw new Exception("No File Found \n");
foreach (string filename in FullPathFileNames)
{
if (cancel) break;
progress_value++;
try
{
if (!radioRandom.Checked)
{
new_filename = folder_path + "\\" +
textBoxTemplate.Text +
progress_value +
Path.GetExtension(filename); //gets extension of a file
}
else
{
if (checkBoxOmitExtension.Checked)
new_filename = folder_path + "\\" + rand.Next();
else
new_filename = folder_path + "\\" + rand.Next() +
Path.GetExtension(filename);
}
File.Move(filename, new_filename);
Application.DoEvents();
}
catch (Exception)
{
Application.DoEvents();
}
progressBar1.Value = (int)progress_value;
progressBar1.Update();
}
SelectedPathText.Text = "Complete";
cancel = false;
}
catch (Exception ex)
{
SelectedPathText.Text = ex.Message;
}
}
That's all.
| You must Sign In to use this message board. | ||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
|
||||||||||||||||||||||
General
News
Question
Answer
Joke
Rant
Admin
|
PermaLink |
Privacy |
Terms of Use
Last Updated: 30 Jul 2008 Editor: Deeksha Shenoy |
Copyright 2008 by Mohammad Dayyan Everything else Copyright © CodeProject, 1999-2009 Web16 | Advertise on the Code Project |