博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
批量修改文件名
阅读量:4691 次
发布时间:2019-06-09

本文共 2198 字,大约阅读时间需要 7 分钟。

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

  }

}

转载于:https://www.cnblogs.com/XiaoLang0/p/11059815.html

你可能感兴趣的文章
BZOJ 1629. [Usaco2007 Demo]Cow Acrobats
查看>>
BZOJ 1634. [Usaco2007 Jan]Protecting the Flowers 护花
查看>>
manifest.json 解析--手机web app开发笔记(三-1)
查看>>
手机web app开发笔记
查看>>
默认文档解析--手机web app开发笔记(二)
查看>>
导航页的开发--手机web app开发笔记(四)
查看>>
manifest.json 解析--手机web app开发笔记(三-2)
查看>>
webstorm配置git的使用(1)
查看>>
h5手机端自适应解决方案
查看>>
webstorm配置git的使用(2)
查看>>
使用FileSaver导出页面数据到excel
查看>>
unity 保存场景数据,读取场景
查看>>
c# 创建文件夹跟遍历文件夹
查看>>
unity 坐标转换
查看>>
unity查找场景中所有物体
查看>>
unity 获取本机的IP地址
查看>>
unity 获取鼠标跟键盘
查看>>
unity 动态添加 EventTrigger
查看>>
Hive运行引擎Tez的安装
查看>>
Hadoop添加LZO压缩支持
查看>>