zoukankan      html  css  js  c++  java
  • 批量修改文件名

    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));
              }
            }
          }
        }

      }
    }

    支持个人观看使用,如商用或转载,请告知! -----萧朗(QQ:453929789 Email:xiaolang_xl@sina.com)
  • 相关阅读:
    (16)JavaScript的流程控制(js的循环)
    (15)javaScript入门
    (14)定位布局(子级在父级中自由定位 父级在页面中自由定位)
    (0-1)CSS 标签语法的属性
    ACM/ICPC 之 双向链表_构造列表-模拟祖玛 (TSH OJ-Zuma(祖玛))
    手记-数学分析(高等数学)中有关算法效率的公式列举(O,Θ,Ω)
    2014北大研究生推免机试(校内)-复杂的整数划分(DP进阶)
    整数划分问题-解法汇总(暂有DP-递归)
    2014北大研究生推免机试(校内)-垃圾炸弹(基础枚举)
    ACM/ICPC 之 BFS-广搜进阶-八数码(经典)(POJ1077+HDU1043)
  • 原文地址:https://www.cnblogs.com/XiaoLang0/p/11059815.html
Copyright © 2011-2022 走看看