using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 批量修改文件名称
{
public partial class 批量修改文件名称 : Form
{
public 批量修改文件名称()
{
InitializeComponent();
}
string StartFolderName = "";
string EndFolderName = "";
private void button1_Click(object sender, EventArgs e)
{
FolderBrowserDialog FileDialog = new FolderBrowserDialog();
if (FileDialog.ShowDialog() == DialogResult.OK)
{
StartFolderName = FileDialog.SelectedPath;
textBox1.Text = StartFolderName;
}
}
private void button2_Click(object sender, EventArgs e)
{
FolderBrowserDialog FileDialog = new FolderBrowserDialog();
if (FileDialog.ShowDialog() == DialogResult.OK)
{
EndFolderName = FileDialog.SelectedPath;
textBox2.Text = EndFolderName;
}
}
private void button3_Click(object sender, EventArgs e)
{
if (textBox3.Text == null || textBox4.Text == null || textBox3.Text == "" || textBox4.Text == "")
{
return;
}
StartFolderName = textBox1.Text;
EndFolderName = textBox2.Text;
string[] files = Directory.GetFiles(StartFolderName, "*.*", SearchOption.AllDirectories);
foreach (string str in files)
{
string tempName = Path.GetFileNameWithoutExtension(str);
if (tempName.Contains(textBox3.Text))
{
string newName = tempName.Replace(textBox3.Text, textBox4.Text);
if (Directory.GetFiles(EndFolderName).Contains(EndFolderName + "\\" + newName + Path.GetExtension(str)))
{
//(老的文件名,新的文件名=指定路径+新文件名+扩展名);
File.Move(EndFolderName + "\\" + newName + Path.GetExtension(str), EndFolderName + "\\" + newName + Path.GetExtension(str));
}
else
{
//(老的文件名,新的文件名=指定路径+新文件名+扩展名);
File.Copy(str, EndFolderName + "\\" + newName + Path.GetExtension(str));
}
}
}
}
}
}