C# 批量复制文件到新路径并修改文件名
1 static void Main(string[] args) 2 { 3 Console.WriteLine("将去除前缀字符:[normal-]和后缀"); 4 5 Console.WriteLine("请输入需要新文件存放夹路径:"); 6 string newpath = Console.ReadLine(); 7 chek: 8 Console.WriteLine("请输入需要修改文件夹路径:"); 9 string oldpath = Console.ReadLine(); 10 11 if (oldpath != null) 12 { 13 if (!System.IO.Directory.Exists(oldpath)) 14 { 15 Console.WriteLine("文件夹不存在!"); 16 goto chek; 17 } 18 var paths = oldpath.Split('\\'); 19 var time = Convert.ToDateTime(paths[paths.Length - 1]); 20 var pathtime = newpath + "\\" + time.Year.ToString() + "\\" + time.Month.ToString() + "\\" + time.Day.ToString(); 21 22 if (!System.IO.Directory.Exists(pathtime)) 23 { 24 System.IO.Directory.CreateDirectory(pathtime); 25 } 26 27 DirectoryInfo root = new DirectoryInfo(oldpath); 28 FileInfo[] files = root.GetFiles(); 29 foreach (var item in files) 30 { 31 //删除后缀 32 var name = item.Name.Replace(item.Extension, ""); 33 //删除指定前缀 34 var newname = name.Replace("normal-", ""); 35 Console.WriteLine(item.Name + ">>" + newname); 36 //复制并修改文件名 37 //需要注意文件重复会报错 38 newpath = pathtime + "\\" + newname; 39 if (!File.Exists(newpath)) 40 { 41 File.Copy(item.FullName, newpath); 42 } 43 else 44 { 45 Console.WriteLine(newname + "该文件重复!"); 46 } 47 } 48 49 } 50 51 goto chek; 52 }