C#实现批量修改文件名2

1、上一个功能实在单一,增加了点功能,基本上能正确运行,会有一点小bug懒得改了,已经满足个人使用的需求了,有需要的同学自己修改吧。

using System;
using System.IO;
using System.Media;
using System.Text.RegularExpressions;
using System.Threading;
using System.Windows.Forms;

namespace 批量修改文件名
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private void Form1_Load(object sender, EventArgs e)
        {

        }
        private void button1_Click(object sender, EventArgs e)
        {
            int i, start = 0, flag = 0;
            int NumberOfFiles = 0;
            string FileClass;
            FileClass = comboBox3.Text.ToString();
            if (FileClass == "")
            {
                System.Media.SystemSounds.Hand.Play();
                MessageBox.Show("请选择文件类型!", "警告");
            }
            else
            {
                bool bl;
                Regex reg = new Regex("^[\u4e00-\u9fa5]$");
                //  String path = @"C:\Users\Xyan1\Desktop\music";
                string path = textBox2.Text.ToString();
                string OutPath = textBox3.Text.ToString();
                if ((path == "") || (OutPath == ""))
                {
                    System.Media.SystemSounds.Hand.Play();
                    MessageBox.Show("路径不得为空!", "警告");
                }
                else
                {
                    if (!Directory.Exists(path))//如果路径不存在
                    {
                        System.Media.SystemSounds.Hand.Play();
                        MessageBox.Show("源文件夹不存在!", "警告");
                    }
                    else
                    {
                        if (!Directory.Exists(OutPath))//如果路径不存在
                        {
                            Directory.CreateDirectory(OutPath);//创建一个路径的文件夹
                        }
                        var files = Directory.GetFiles(path, FileClass);
                        foreach (var file in files)
                        {
                            NumberOfFiles++;
                            start = 0;
                            flag = 0;
                            StreamWriter sw = File.AppendText(path + "\\文件列表.ini");
                            string Nowname = file;
                            int length = Nowname.Length;
                            for (i = 0; i < length; i++)
                            {
                                if (reg.IsMatch(Nowname.Substring(i, 1)))
                                {
                                    start = i;
                                    break;
                                }
                            }
                            for (i = 0; i < length; i++)
                            {
                                bl = (reg.IsMatch(Nowname.Substring(i, 1))) && (reg.IsMatch(Nowname.Substring(i + 1, 1)));
                                //判断是否连续两个字符都是汉字,很少有一个字的歌,大部分至少歌名有2个字
                                if (bl)
                                {
                                    flag++;
                                }
                                else
                                //否则的话就退出循环,汉字部分结束
                                {
                                    if (flag != 0)
                                    {
                                        break;
                                    }
                                }
                            }
                            flag++;
                            string Name = Nowname.Substring(start, flag);
                            sw.WriteLine(Name);//把新的歌名写到txt中生成歌单。
                            File.Move(file, OutPath + "\\" + Name + FileClass.Substring(1, FileClass.Length - 1));
                            //修改文件名,并把新文件复制到制定文件夹。
                            sw.Flush();
                            sw.Close();
                            //   MessageBox.Show("已修改文件"+Name+".mp3");
                        }
                        if (NumberOfFiles == 0)
                        {
                            MessageBox.Show("未找到该类型文件!", "通知");
                        }
                        else
                        {
                            MessageBox.Show("执行完毕!", "通知");
                        }
                    }
                }
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            int i, start = 0, flag = 0, stop = 0;
            int NumberOfFiles = 0;
            Regex reg = new Regex("^[\u4e00-\u9fa5]$");
            string FileClass;
            FileClass = comboBox3.Text.ToString();
            if (FileClass == "")
            {
                System.Media.SystemSounds.Hand.Play();
                MessageBox.Show("请选择文件类型!", "警告");
            }
            else
            {
                string path = textBox2.Text.ToString();
                string OutPath = textBox3.Text.ToString();
                string Start_Word = "";
                string Stop_Word = "";
                if ((path == "") || (OutPath == ""))
                {
                    System.Media.SystemSounds.Hand.Play();
                    MessageBox.Show("路径不得为空!", "警告");
                }
                else
                {
                    if (!Directory.Exists(path))//如果路径不存在
                    {
                        System.Media.SystemSounds.Hand.Play();
                        MessageBox.Show("源文件夹不存在!", "警告");
                    }
                    else
                    {
                        try
                        {
                            Start_Word = comboBox1.SelectedItem.ToString();
                            Stop_Word = comboBox2.SelectedItem.ToString();
                        }
                        catch
                        {
                            System.Media.SystemSounds.Hand.Play();
                            MessageBox.Show("未选择起止字符!", "警告");
                        }
                        finally
                        {
                            if (!Directory.Exists(OutPath))//如果路径不存在
                            {
                                Directory.CreateDirectory(OutPath);//创建一个路径的文件夹
                            }
                            var files = Directory.GetFiles(path, FileClass);
                            foreach (var file in files)
                            {
                                NumberOfFiles++; ;
                                start = 0;
                                flag = 0;
                                stop = 0;
                                StreamWriter sw = File.AppendText(path + "\\文件列表.ini");
                                string Nowname = file;
                                int length = Nowname.Length;
                                for (i = 0; i < length; i++)
                                {
                                    if (Nowname.Substring(i, 1) != Start_Word)
                                    {
                                        start++;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                for (i = 0; i < length; i++)
                                {
                                    if (Nowname.Substring(i, 1) != Stop_Word)
                                    {
                                        stop++;
                                    }
                                    else
                                    {
                                        break;
                                    }
                                }
                                if (start == stop)
                                {
                                    System.Media.SystemSounds.Hand.Play();
                                    MessageBox.Show("源文件不存在所选字符,程序未进行任何操作!", "警告");
                                    sw.Flush();
                                    sw.Close();
                                    break;
                                }
                                start++;
                                string Name = Nowname.Substring(start, stop - start);
                                sw.WriteLine(Name);//把新的歌名写到txt中生成歌单。
                                File.Move(file, OutPath + "\\" + Name + FileClass.Substring(1, FileClass.Length - 1));
                                //修改文件名,并把新文件复制到制定文件夹。
                                sw.Flush();
                                sw.Close();
                                //   MessageBox.Show("已修改文件"+Name+".mp3");
                            }
                            if (NumberOfFiles == 0)
                            {
                                MessageBox.Show("未找到该类型文件!", "通知");
                            }
                            else
                            {
                                MessageBox.Show("执行完毕!", "通知");
                            }
                        }
                    }
                }
            }
        }
        private void button3_Click(object sender, EventArgs e)
        {
            int i, start = 0, flag = 0, stop;
            int NumberOfFiles = 0;
            string FileClass;
            FileClass = comboBox3.Text.ToString();
            if (FileClass == "")
            {
                System.Media.SystemSounds.Hand.Play();
                MessageBox.Show("请选择文件类型!", "警告");
            }
            else
            {
                string path = textBox2.Text.ToString();
                string OutPath = textBox3.Text.ToString();
                if ((path == "") || (OutPath == ""))
                {
                    System.Media.SystemSounds.Hand.Play();
                    MessageBox.Show("路径不得为空!", "警告");
                }
                else
                {
                    if (textBox1.Text.ToString() == "")
                    {
                        System.Media.SystemSounds.Hand.Play();
                        MessageBox.Show("请输入要去除的前缀!", "警告");
                    }
                    else
                    {
                        if (!Directory.Exists(path))//如果路径不存在
                        {
                            System.Media.SystemSounds.Hand.Play();
                            MessageBox.Show("源文件夹不存在!", "警告");
                        }
                        else
                        {
                            if (!Directory.Exists(OutPath))//如果路径不存在
                            {
                                Directory.CreateDirectory(OutPath);//创建一个路径的文件夹
                            }
                            var files = Directory.GetFiles(path, FileClass);
                            foreach (var file in files)
                            {
                                NumberOfFiles ++;
                                start = 0;
                                flag = 0;
                                stop = 0;
                                StreamWriter sw = File.AppendText(path + "\\文件列表.ini");
                                string Nowname = file;
                                string Move_Word = textBox1.Text.ToString();
                                int length = Nowname.Length;
                                int lengthOf_MoveWord = Move_Word.Length;
                                int lengthOf_location = path.Length;
                                start = lengthOf_location + lengthOf_MoveWord + 1;
                                stop = length - start;
                                string Name = Nowname.Substring(start, stop);
                                sw.WriteLine(Name);//把新的歌名写到txt中生成歌单。
                                File.Move(file, OutPath + "\\" + Name);
                                //修改文件名,并把新文件复制到制定文件夹。
                                sw.Flush();
                                sw.Close();
                            }
                            if (NumberOfFiles == 0)
                            {
                                MessageBox.Show("未找到该类型文件!", "通知");
                            }
                            else
                            {
                                MessageBox.Show("执行完毕!", "通知");
                            }
                        }
                    }
                }
            }
        }
        private void button4_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dilog = new FolderBrowserDialog();
            dilog.Description = "请选择文件夹";
            if (dilog.ShowDialog() == DialogResult.OK)
            {
                textBox2.Text = dilog.SelectedPath;
            }
        }
        private void button5_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dilog = new FolderBrowserDialog();
            dilog.Description = "请选择文件夹";
            if (dilog.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = dilog.SelectedPath;
            }
        }
    }
}

2、程序界面:

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值