• 一首歌可以循环一整天《我们都是好孩子》
  • 在别人眼里是逃避,其实是不想牵累任何人,做孤独的自己。
  • 也许有一天我会背着包,独自漂流。
  • 既然做了决定,就不后悔,再见昆明!
  • 愿闻世间百态。
  • 外表的一切如常,内心的混乱又有谁能了解!
  • 突然想回安徽工作了!
  • 该来的还是来了,Uzi宣布退役!
  • 国内疫情已完全控制住,但是国外疫情大爆发,现在开始限制人员入境。
  • 啊啊啊,IP被墙了,好烦!

C#批量修改文件名称(替换)

程序 愿闻世间百态 3年前 (2021-01-15 11:00:47) 5759次浏览 已收录 0个评论

二、代码实现
1、界面布局
C#批量修改文件名称(替换)

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(), "提示");
            }
        }
    }
}


三、效果显示
C#批量修改文件名称(替换)

四、下载地址:

点击跳转百度云,提取码: m8tp


WECV.CN , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:C#批量修改文件名称(替换)
喜欢 (0)
[]
分享 (0)

您必须 登录 才能发表评论!