有時候需要批量替換一個文件夾下面所有文件的名稱,如果文件太多,就沒辦法手工一個一個來改了。
想想作為程序員十幾分鍾就可以搞定的東西,解決別人幾個小時工作,豈不快哉。
於是有此工具發表,界面如下:
核心代碼就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 }