一、思路流程
1、界面设计
a.文件路径
b.查找内容
c.替换内容
d.日志打印
2、执行流程
a.判断路径、查找内容、替换内容是否为空,并给出提示;
b.根据路径判断该文件夹下是否有文件
c.查找内容(当替换文本为“#all#”时,替换内容无效,直接以时间戳命名)
d.替换内容
e.日志打印
1、界面设计
a.文件路径
b.查找内容
c.替换内容
d.日志打印
2、执行流程
a.判断路径、查找内容、替换内容是否为空,并给出提示;
b.根据路径判断该文件夹下是否有文件
c.查找内容(当替换文本为“#all#”时,替换内容无效,直接以时间戳命名)
d.替换内容
e.日志打印
2、代码模块
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; using System.IO; namespace Change_File { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void select_filepath_Click(object sender, EventArgs e) //弹出一个选择目录的对话框 { FolderBrowserDialog path = new FolderBrowserDialog(); path.ShowDialog(); this.text_path.Text = path.SelectedPath; } private void text_path_TextChanged(object sender, EventArgs e) { } private void change_name_Click(object sender, EventArgs e) { // string date_time = string textpath = this.text_path.Text; try { if (string.IsNullOrEmpty(textpath) || string.IsNullOrEmpty(this.find_text.Text) || string.IsNullOrEmpty(this.replace_text.Text)) //判断 textpath 是否为空 { string notice_text = "注意:请检查文件路径/替换文本/替换为是否均已填写..."; MessageBox.Show("注意:请检查文件路径/替换文本/替换为是否均已填写...", "提示"); this.list_logs.Items.Add(DateTime.Now.ToLongTimeString().ToString() + " ==> " + notice_text); list_logs.TopIndex = list_logs.Items.Count - 1;//打印框显示最新信息。 // Application.Exit(); } else { DirectoryInfo dir = new DirectoryInfo(textpath); var fileInfos = dir.GetFiles(); if (fileInfos.Length == 0) { string notice_text = "注意:该文件夹下没有文件!"; this.list_logs.Items.Add(DateTime.Now.ToLongTimeString().ToString() + " ==> " + notice_text); list_logs.TopIndex = list_logs.Items.Count - 1;//打印框显示最新信息。 MessageBox.Show(notice_text, "提示"); } else { int count_file = 0; int count_no = 0; foreach (var fileInfo in fileInfos) { string[] old_name_arr = fileInfo.Name.ToString().Split('.'); string file_ext = old_name_arr[old_name_arr.Length - 1]; string old_name = fileInfo.Name.ToString().Replace("."+file_ext, ""); if (this.find_text.Text == "#all#") { count_file++; string new_name = DateTime.Now.ToUniversalTime().Ticks.ToString() + "." + file_ext; string new_path = textpath + "/new/"; if (false == Directory.Exists(new_path)) { //创建 new 文件夹 Directory.CreateDirectory(new_path); this.list_logs.Items.Add(DateTime.Now.ToLongTimeString().ToString() + " ==> " + "创建 new 文件夹!"); } fileInfo.MoveTo(this.text_path.Text + "\\new\\" + new_name); this.list_logs.Items.Add(DateTime.Now.ToLongTimeString().ToString() + " [" + count_file + "] " + old_name + " ==> " + new_name); // this.list_logs.Items.Add(new_path + new_name); list_logs.TopIndex = list_logs.Items.Count - 1; } else { if (old_name.IndexOf(this.find_text.Text) >= 0) { count_file++; string new_name = old_name.Replace(this.find_text.Text, this.replace_text.Text) + "." + file_ext; string new_path = textpath + "/new/"; if (false == Directory.Exists(new_path)) { //创建 new 文件夹 Directory.CreateDirectory(new_path); this.list_logs.Items.Add(DateTime.Now.ToLongTimeString().ToString() + " ==> " + "创建 new 文件夹!"); } fileInfo.MoveTo(this.text_path.Text + "\\new\\" + new_name); this.list_logs.Items.Add(DateTime.Now.ToLongTimeString().ToString() + " [" + count_file + "] " + old_name + " ==> " + new_name); // this.list_logs.Items.Add(new_path + new_name); list_logs.TopIndex = list_logs.Items.Count - 1; } else { count_no++; this.list_logs.Items.Add(DateTime.Now.ToLongTimeString().ToString() + " [" + count_file + "] " + "跳过文件:" + old_name); // this.list_logs.Items.Add(new_path + new_name); list_logs.TopIndex = list_logs.Items.Count - 1; continue; } } } this.list_logs.Items.Add(DateTime.Now.ToLongTimeString().ToString() + " ==> " + "已成功修改" + count_file + "个文件,跳过" + count_no + "个文件!"); list_logs.TopIndex = list_logs.Items.Count - 1;//打印框显示最新信息。 MessageBox.Show("已成功修改" + count_file + "个文件,跳过"+count_no+"个文件!", "提示"); } } } catch (Exception ex) { this.list_logs.Items.Add(DateTime.Now.ToLongTimeString().ToString() + " ==> " + ex.ToString()); list_logs.TopIndex = list_logs.Items.Count - 1;//打印框显示最新信息。 MessageBox.Show(ex.ToString(), "提示"); } } } }
四、下载地址:
点击跳转百度云,提取码: m8tp