/// <summary> /// 获取所有文件 /// </summary> /// <param name="dirPath">文件夹路径</param> /// <param name="suffix">文件后缀</param> /// <returns></returns> public Result GetDirFileName(string dirPath, string suffix) { return(RunFun((logPath) => { DirectoryInfo info = new DirectoryInfo(ToolFile.GetAbsolutelyPath(dirPath)); if (info == null) { Res.Msg = "文件夹不存在"; return Res; } if (string.IsNullOrWhiteSpace(suffix)) { suffix = ""; } suffix = suffix.Replace(".", "").Trim().ToLower(); string tmp = string.Format(SegmentingLine, "不包含的文件") + "\n"; foreach (var item in info.GetFiles().OrderByDescending(c => c.FullName)) { if (string.IsNullOrWhiteSpace(suffix) || ToolFile.GetSuffix(item.FullName).ToLower() == suffix) { WriteLog(logPath, item.Name, true, false); } else { tmp += item.FullName + "\t" + item.CreationTime + "\n"; } } tmp += string.Format(SegmentingLine, "") + "\n"; WriteLog(logPath, tmp, true, false); return Res; })); }
/// <summary> /// 报告分组 /// </summary> /// <param name="tigerPath">文件夹</param> /// <param name="group">分组条件,多个用','分割</param> /// <param name="max">文件夹最大占用</param> /// <returns></returns> public Result ReportGroup(string tigerPath, string group, double max) { return(RunFun(logpath => { string res = "分组" + DateTime.Now.Millisecond + "\\"; tigerPath = ToolFile.GetAbsolutelyPath(tigerPath); DirectoryInfo dir = new DirectoryInfo(tigerPath); // 移除所有文件夹 while (dir.GetDirectories().Length > 0) { Directory.Delete(dir.GetDirectories()[0].FullName, true); } List <string> groupList = group.Split(',').Where(c => !string.IsNullOrWhiteSpace(c)).ToList(); Dictionary <string, long> dic = new Dictionary <string, long>(); List <Task> taskList = new List <Task>(); for (int i = 0; i < dir.GetFiles().Length; i++) { WriteLog(logpath, i + ""); string path = tigerPath + res; FileInfo field = dir.GetFiles()[i]; List <string> nameList = field.Name.Replace("." + ToolFile.GetSuffix(field.Name), "").Split('_').Where(c => !string.IsNullOrWhiteSpace(c)).ToList(); if (nameList.Count != 2) { WriteLog(logpath, field.Name); continue; } DataRow dr = GetEntryInfo(nameList[1]); foreach (var item in groupList) { if (dr.Table.Columns.Contains(item)) { path += dr[item] + "\\"; } } if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } else { if (dic.Keys.Contains(path) && max > 0) { double memory = dic[path] * 1.0; if (max * 1024 * 1024 < memory + field.Length) { path = ToolString.RemoveCharAfter(path, "\\", true) + "(" + dic.Keys.Where(c => c.Contains(path)).Count() + ")\\"; Directory.CreateDirectory(path); } } } if (dic.Keys.Contains(path)) { dic[path] += field.Length; } else { dic.Add(path, field.Length); } taskList.Add(Task.Run(() => { File.Copy(field.FullName, path + field.Name, true); })); } Task.WaitAll(taskList.ToArray()); dir = new DirectoryInfo(tigerPath + res); foreach (var key in dic.Keys) { WriteLog(logpath, (dic[key] * 1.0 / 1020 / 1024) + "M\t" + key); } return Res; })); }