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)
  • 相关阅读:
    闭包
    laravel 控制器
    laravel 模板
    laravel 视图
    laravel 请求
    laravel 分页
    解决启动Apache遇到的问题Permission denied: AH00072: make_sock: could not bind to address 0.0.0.0:8888
    一张号称一篇程序覆盖Python基础的代码
    [转载]激活Navicat Premium
    C语言自定义函数的形参为数组时需要注意传入长度
  • 原文地址:https://www.cnblogs.com/XiaoLang0/p/11059815.html
Copyright © 2011-2022 走看看