首页 > 学院 > 开发设计 > 正文

【.NET】合并文件夹

2019-11-06 07:42:14
字体:
来源:转载
供稿:网友

背景

需要按照一定规则合并大量文件夹 这些文件中是按照命名分类的,比如说以A,B,C 在这些类别的文件夹存在一定顺序,比如1-12,13-55,需要把连续的文件夹合并


功能说明

按照规则合并文件夹


图片说明

合并前

这里写图片描述

合并后 这里写图片描述


代码

class PRogram{ static void Main(string[] args) { try { //获得根目录路径 Console.WriteLine("请输入根文件夹的路径"); string path = Console.ReadLine(); //实例化出文件夹处理对象 DirectoryInfo dir = new DirectoryInfo(path);//根据路径实例化出该根文件夹对象 DirectoryInfo[] folders = dir.GetDirectories();//获得根文件夹下的子文件夹,不包括文件 //获得文件夹种类 List<string> type = GetAllTypeFolders(folders); //在各类文件夹中依次遍历,进行相应的合并 foreach (string item in type) { List<string> subfolders = GetFolderNames(folders, item);//获得所有该类文件夹 List<string> processed = ProcessFolders(path, subfolders, item);//合并该类文件夹 while (processed != null)//直到处理完所有文件夹 { subfolders = processed; processed = ProcessFolders(path, subfolders, item); } } Console.WriteLine("已经成功合并该目录下的所有文件夹!"); Console.ReadKey(); } catch (Exception e) { Console.WriteLine(e.StackTrace); } Console.ReadKey(); } /// <summary> /// 筛选出所有文件夹的种类 /// </summary> public static List<string> GetAllTypeFolders(DirectoryInfo[] folders) { List<string> type = new List<string>(); for (int i = 0; i < folders.Length; i++) { string[] str1 = folders[i].ToString().Split('_'); string firstWord = str1[0]; bool flag = false; if (type.Count == 0) { flag = false; } foreach (string item in type) { if (item == firstword) { flag = true; } } if (flag == false) { type.Add(firstword); } } return type; } /// <summary> /// 获取文件夹中包含特殊字段的文件夹 /// </summary> public static List<string> GetFolderNames(DirectoryInfo[] folders, string folder_type) { List<string> folder_name = new List<string>(); for (int i = 0; i < folders.Length; i++) { if (folders[i].Name.Contains(folder_type)) { folder_name.Add(folders[i].Name.ToString()); } } return folder_name; } /// <summary> /// 处理文件夹 /// </summary> public static List<string> ProcessFolders(string root_path, List<string> folders, string folder_type) { List<string> processed_folders = new List<string>(); List<string> serial_folders=new List<string>(); List<string> rest_folders=new List<string>(); //将序列号存到数组中 List<string> order = new List<string>(); foreach (var item_name in folders) { string[] names = item_name.Split('_'); order.Add(names[1]); order.Add(names[2]); } bool serial_flag = true; for (int i = 1; i < order.Count -1; i = i + 2)//判断偶数位确定是否连续 { if (int.Parse(order[i]) + 1 != int.Parse(order[i + 1])) { serial_flag = false; for (int j = 0; j < i/2+1; j++) { serial_folders.Add(folders[j]); } MergeFolders(root_path, serial_folders, folder_type); for (int k = i/2+1; k < folders.Count; k++) { rest_folders.Add(folders[k]); } } } if (serial_flag==true) { MergeFolders(root_path, folders, folder_type); processed_folders = null; } else { processed_folders= rest_folders; } return processed_folders; } /// <summary> /// 合并连续文件夹 /// </summary> public static bool MergeFolders(string root_path, List<string> folders,string folder_type) { bool flag = false; try { //将序列号存到数组中 List<string> order = new List<string>(); foreach (var item_name in folders) { string[] names = item_name.Split('_'); order.Add(names[1]); order.Add(names[2]); } //创建文件夹 string start = order[0].ToString(); string end = order[order.Count - 1].ToString(); string newfoler = folder_type + "_" + start + "_" + end; DirectoryInfo dir = new DirectoryInfo(root_path); dir.CreateSubdirectory(newfoler); //移动文件 foreach (string item_folder in folders) { DirectoryInfo sub_dir = new DirectoryInfo(root_path + "//" + item_folder); FileInfo[] file = sub_dir.GetFiles(); for (int i = 0; i < file.Length; i++) { File.Move(root_path + "//" + item_folder + "//" + file[i], root_path + "//" + newfoler + "//" + file[i]); } if (item_folder != newfoler)//避免文件夹为1误删 { Directory.Delete(root_path + "//" + item_folder, true);//删除原文件夹 } } flag = true; } catch (Exception e) { Console.WriteLine(e.StackTrace); } return flag; }}

小结

对基本功是一个很好的锻炼需要有一点算法的思维要有清晰的逻辑思维
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表