批量改文件名小工具

有时候需要批量替换一个文件夹下面所有文件的名称,如果文件太多,就没办法手工一个一个来改了。

想想作为程序员十几分钟就可以搞定的东西,解决别人几个小时工作,岂不快哉。

于是有此工具发表,界面如下:

 

程序下载

核心代码就20多行

 1 Computer MyComputer = new Computer();
 2         int totalReplace = 0;
 3         //递归调用
 4         private void DoReplace(string dir, bool includeChild)
 5         {
 6             DirectoryInfo di = new DirectoryInfo(dir);
 7             FileInfo[] files = di.GetFiles();
 8             if (files.Length > 0)
 9             {
10                 foreach (FileInfo f in files)
11                 {
12                     string newFileName = f.Name.Replace(txtOld.Text, txtRep.Text);
13                     if(f.Name!=newFileName)
14                     {
15                         MyComputer.FileSystem.RenameFile(f.FullName, newFileName);
16                         totalReplace++;
17                     }
18                 }
19             }
20 
21             DirectoryInfo[] dirs= di.GetDirectories();
22             if (dirs.Length > 0)
23             {
24                 foreach (DirectoryInfo d in dirs)
25                 {
26                     DoReplace(d.FullName, includeChild);
27                 }
28             }
29         }
原文地址:https://www.cnblogs.com/tuyile006/p/15397830.html