【.NET】合并文件夹
来源:互联网 发布:linux vi命令详解 编辑:程序博客网 时间:2024/06/05 11:58
背景
需要按照一定规则合并大量文件夹
这些文件中是按照命名分类的,比如说以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; }}
小结
- 对基本功是一个很好的锻炼
- 需要有一点算法的思维
- 要有清晰的逻辑思维
1 0
- 【.NET】合并文件夹
- Java 多文件夹合并
- linux下合并文件夹
- 合并文件夹下文本
- 合并文件夹一
- 合并文件夹二
- Java 多文件夹合并
- java 文件夹合并
- java 合并文件夹下多个文件
- windows下文件夹合并分割
- Linux下合并覆盖文件夹
- vb.net 合并图片
- asp.net合并excel
- 合并.net程序集
- .Net DataGrid 合并单元格
- .Net实现合并文件
- ASP.NET DataTable合并
- VB.NET合并图片
- 算法训练 方格取数(动态规划)
- 圆桌问答 (2017 第一季)
- 算法设计与应用基础:第二周(1)
- 表达式的值(exp)
- Android N 新特性
- 【.NET】合并文件夹
- C++中参数传递方式讨论
- 字符匹配
- stars
- 算法基础之插入排序
- mongodb 的 curd
- [Thinking in Java]
- 九度 oj 题目1072:有多少不同的面值组合?
- Color the ball