c# 控制台应用程序批量修改文件夹下的后缀名(包括子文件夹)
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace ConsoleApplication2 { class Program { static void Main(string[] args) { DirectoryInfo theFolder = new DirectoryInfo("E:\\620000S00000_20160707\\"); // 目录文件位置 FileInfo[] fileInfo = theFolder.GetFiles(); // 获得当前的文件夹内的所有文件数组 int i = 1; foreach (FileInfo NextFile in fileInfo) //遍历文件 { if (NextFile.Extension == ".db") // 得到你想要的格式 { string dbName = NextFile.Name; string name = dbName.Replace(".db", ""); Log(""); Log("============"+i+": " + name + "=============="); UpdateName(name); i++; } } Log("批量修改成功"); Console.ReadLine(); } public static void UpdateName(string dname) { //批量更改文件名 string path1 = @"E:\ContDB\" + dname; DirectoryInfo di = new DirectoryInfo(path1);//此处写你的文件目录地址 if (!File.Exists(path1)) { foreach (FileInfo fi in di.GetFiles("*.txt"))//举例:可以用 di.GetFiles("*.jpg") 只重名jpg文件 { //逐个修改文件后缀 String FileName = fi.Name; Console.WriteLine(FileName);//输出文件名称 string getExtension = Path.GetExtension(FileName); Console.WriteLine("扩展名:{0}", getExtension); string newFileName = FileName.Replace(getExtension, ".csv"); Console.WriteLine("更改后:{0}", newFileName); string newDirectoryName = Path.Combine(path1, newFileName); fi.MoveTo(newDirectoryName);//必须有这步 } } } static void Log(string s) { Console.WriteLine("" + s); } } }
引用命名空间:using System.IO;