前段时间在网上下了很多教程,文件名都是以这段字符开头“跟我一起来学XXX”,看着很是碍眼,因为文件名很长导致在文件夹里浏览的时候后面都是省略号,手动改又嫌太麻烦,有100多个文件,还是让电脑自动帮我改吧,自己动手丰衣足食,就用C#写个简单的小工具。
列目录函数
获取扩展名
重命名方法
RenameTool.rar
列目录函数
private void listDirectory(string path)
{
DirectoryInfo dir = new DirectoryInfo(path);
FileInfo[] files=dir.GetFiles();
string extension=getExtension();
fileList.Items.Clear();
foreach (FileInfo file in files)
{
if (extension != string.Empty)
{
if (extension.IndexOf(file.Extension.ToUpper()) >= -1 && file.Extension != string.Empty)
{
fileList.Items.Add(file.Name);
}
}
else
{
fileList.Items.Add(file.Name);
}
}
}
{
DirectoryInfo dir = new DirectoryInfo(path);
FileInfo[] files=dir.GetFiles();
string extension=getExtension();
fileList.Items.Clear();
foreach (FileInfo file in files)
{
if (extension != string.Empty)
{
if (extension.IndexOf(file.Extension.ToUpper()) >= -1 && file.Extension != string.Empty)
{
fileList.Items.Add(file.Name);
}
}
else
{
fileList.Items.Add(file.Name);
}
}
}
获取扩展名
private string getExtension()
{
StringBuilder sb = new StringBuilder(50);
foreach (Control control in this.groupBox1.Controls)
{
if (control.Tag.ToString().Equals("tagExtension"))
{
if ((control as CheckBox).Checked)
{
sb.Append(control.Text+";");
}
}
}
return sb.ToString();
}
{
StringBuilder sb = new StringBuilder(50);
foreach (Control control in this.groupBox1.Controls)
{
if (control.Tag.ToString().Equals("tagExtension"))
{
if ((control as CheckBox).Checked)
{
sb.Append(control.Text+";");
}
}
}
return sb.ToString();
}
重命名方法
private void btnRename_Click(object sender, EventArgs e)
{
if (MessageBox.Show("这个操作是不可逆的,确定要继续吗?", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
return;
FileInfo file;
string preName = this.txtPreName.Text.Trim();
string pre=preName;
int preLen = (int)this.preLength.Value;
try
{
for (int i = 0; i < fileList.Items.Count; i++)
{
file = new FileInfo(fbd.SelectedPath + "\\" + fileList.Items[i].ToString());
if (this.chkAuto.Checked) pre = i.ToString() + "." + preName;
if (fileList.Items[i].ToString().Length >= preLen)
{
file.MoveTo(fbd.SelectedPath + "\\"+pre + fileList.Items[i].ToString().Substring(preLen));
}
else
{
file.MoveTo(fbd.SelectedPath + "\\"+pre + fileList.Items[i].ToString());
}
}
MessageBox.Show("重命名完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
listDirectory(fbd.SelectedPath);
}
catch
{
MessageBox.Show("重命名发生错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
{
if (MessageBox.Show("这个操作是不可逆的,确定要继续吗?", "询问", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
return;
FileInfo file;
string preName = this.txtPreName.Text.Trim();
string pre=preName;
int preLen = (int)this.preLength.Value;
try
{
for (int i = 0; i < fileList.Items.Count; i++)
{
file = new FileInfo(fbd.SelectedPath + "\\" + fileList.Items[i].ToString());
if (this.chkAuto.Checked) pre = i.ToString() + "." + preName;
if (fileList.Items[i].ToString().Length >= preLen)
{
file.MoveTo(fbd.SelectedPath + "\\"+pre + fileList.Items[i].ToString().Substring(preLen));
}
else
{
file.MoveTo(fbd.SelectedPath + "\\"+pre + fileList.Items[i].ToString());
}
}
MessageBox.Show("重命名完成", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
listDirectory(fbd.SelectedPath);
}
catch
{
MessageBox.Show("重命名发生错误", "提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
RenameTool.rar