C# 批量更改文件或图片名称

本例实现了批量更改文件或图片的名称。具体设计思路如下所示

  • 获取文件夹路径
  • 获取文件夹中所有文件的文件名
  • 删除文件名并对文件名进行批量修改

窗口设计如下图所示:

程序代码如下:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Collections;
using System.Threading;
namespace FileBatchChangeName
{
    public partial class Frm_Main : Form
    {
        public Frm_Main()
        {
            InitializeComponent();
        }
        string[] files;//选择文件的集合,此处声明数组是因为openFileDialog1.FileNames返回值为string[]
        FileInfo fi;//创建一个FileInfo对象,用于获取文件信息
        string[] lvFiles=new string[7];//向控件中添加的行信息
        Thread td;//处理批量更名方法的线程



        bool flag = true;


        bool IsOK = false;//判断是否应用了模板
        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)//选择模板的下拉框
        {
            int k = (int)nuStart.Value;
            if (comboBox2.Text != "")
            {
                txtTemplate.Text = comboBox2.Text.Remove(comboBox2.Text.LastIndexOf("_"));
                int B = comboBox2.SelectedIndex;
                switch (B)
                {
                    case 0:
                        if (listView1.Items.Count > 0)
                        {
                            for (int i = 0; i < listView1.Items.Count; i++)
                            {
                                string name = listView1.Items[i].SubItems[1].Text;
                                string name1 = name.Remove(name.LastIndexOf("."));
                                string name2 = "pic_" + k.ToString();
                                k = k + (int)nuAdd.Value;
                                string newName = name.Replace(name1, name2);
                                listView1.Items[i].SubItems[1].Text = newName;
                            }
                            IsOK = true;
                        }
                        break;
                    case 1:
                        if (listView1.Items.Count > 0)
                        {
                            for (int i = 0; i < listView1.Items.Count; i++)
                            {
                                string name = listView1.Items[i].SubItems[1].Text;
                                string name1 = name.Remove(name.LastIndexOf("."));
                                string name2 = "file_" + k.ToString();
                                k = k +(int) nuAdd.Value;
                                string newName = name.Replace(name1,name2);
                                listView1.Items[i].SubItems[1].Text = newName;
                            }
                            IsOK = true;
                        }
                        break;
                }
            }
        }

        private void StartNumAndAdd()//设置起始数字和增量值
        {
             int k = (int)nuStart.Value;
             if (comboBox2.Text != "")
             {
                 if (listView1.Items.Count > 0)
                 {
                     for (int i = 0; i < listView1.Items.Count; i++)
                     {
                         string name = listView1.Items[i].SubItems[1].Text;
                         string name1 = name.Remove(name.LastIndexOf("."));
                         string name2 = name1.Remove(name.LastIndexOf("_")+1)+k.ToString();
                         k = k + (int)nuAdd.Value;
                         string newName = name.Replace(name1, name2);
                         listView1.Items[i].SubItems[1].Text = newName;
                     }
                     IsOK = true;
                 }
             }
        }


        private void nuStart_ValueChanged(object sender, EventArgs e)//选择起始数字
        {
            StartNumAndAdd();
        }

        private void nuAdd_ValueChanged(object sender, EventArgs e)//选择增量值
        {
            StartNumAndAdd();
        }

        private void txtTemplate_TextChanged(object sender, EventArgs e)//更换模板样式
        {
            if (listView1.Items.Count > 0)
            {
                if (IsOK&&txtTemplate.Text.Trim()!=""&&comboBox2.Text!="")
                {
                    
                    for (int i = 0; i < listView1.Items.Count; i++)
                    {
                        string name = listView1.Items[i].SubItems[1].Text;
                        string name1 = name.Remove(name.LastIndexOf("_") + 1);
                        string newName = name.Replace(name1, txtTemplate.Text.Trim() + "_");
                        listView1.Items[i].SubItems[1].Text = newName;
                    }
                }
            }
        }


        private void ChangeName()
        {
            int flag = 0;
            try
            {
                toolStripProgressBar1.Minimum = 0;
                toolStripProgressBar1.Maximum = listView1.Items.Count - 1;
                for (int i = 0; i < listView1.Items.Count; i++)
                {
                    string path = listView1.Items[i].SubItems[4].Text;
                    string sourcePath = path + listView1.Items[i].SubItems[0].Text;
                    string newPath = path + listView1.Items[i].SubItems[1].Text;
                    File.Copy(sourcePath, newPath);
                    File.Delete(sourcePath);
                    toolStripProgressBar1.Value = i;
                    listView1.Items[i].SubItems[0].Text = listView1.Items[i].SubItems[1].Text;
                    listView1.Items[i].SubItems[6].Text = "√成功";
                }
            }
            catch(Exception ex)
            {
                flag++;
                MessageBox.Show(ex.Message);
            }
            finally
            {
                tsslError.Text = flag.ToString() + " 个错误";
            }
        }

       

        private void Form1_Load(object sender, EventArgs e)
        {
            CheckForIllegalCrossThreadCalls = false;
        }



        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (td != null)
            {
                td.Abort();
            }
        }

        private void listView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void splitContainer1_Panel1_Paint(object sender, PaintEventArgs e)
        {

        }

        private void openfile_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                listView1.GridLines = true;//是否显示网格线
                listView1.Items.Clear();//使用了listview.item.clear()后,listview控件中仍然保存着listviewitem项的结构,即listview有多个列,每列可能对应的列标题数据等。
                                        //而当你使用了listview.clear()后,整个listview内保存数据的结构就没了。
                files = openFileDialog1.FileNames;//openFileDialog1.FileNames打开的可能是多个文件,因此返回值为string[]
                for (int i = 0; i < files.Length; i++)//files.Length为打开的文件个数
                {
                    string path = files[i].ToString();
                    fi = new FileInfo(path);
                    string name = path.Substring(path.LastIndexOf("\\") + 1, path.Length - 1 - path.LastIndexOf("\\"));//listview文件名
                    string ftype = path.Substring(path.LastIndexOf("."), path.Length - path.LastIndexOf(".")); //listview扩展名
                    string createTime = fi.CreationTime.ToShortDateString();//listview日期
                    double a = Convert.ToDouble(Convert.ToDouble(fi.Length) / Convert.ToDouble(1024));
                    string fsize = a.ToString("0.0") + " KB";//listview 大小
                    lvFiles[0] = name;
                    lvFiles[1] = name;
                    lvFiles[2] = ftype;
                    lvFiles[3] = createTime;
                    lvFiles[4] = path.Remove(path.LastIndexOf("\\") + 1);
                    lvFiles[5] = fsize;

                    ListViewItem lvi = new ListViewItem(lvFiles);
                    lvi.UseItemStyleForSubItems = false;
                    lvi.SubItems[1].BackColor = Color.AliceBlue;

                    listView1.Items.Add(lvi);
                }
                tsslSum.Text = listView1.Items.Count.ToString();
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (listView1.Items.Count > 0)
            {
                for (int i = 0; i < listView1.Items.Count; i++)
                {
                    listView1.Items[i].SubItems[6].Text = "";
                }
                tsslError.Text = "";
                td = new Thread(new ThreadStart(ChangeName));
                td.Start();
            }
        }
    }
}

运行效果如下:

猜你喜欢

转载自blog.csdn.net/haier123888/article/details/112797255