首页 > 代码库 > FileDirLocationOperator - 文件或目录位置操作.
FileDirLocationOperator - 文件或目录位置操作.
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 6 namespace MoveFiles.code { 7 //文件或目录的 FullName 或 Name. 8 public enum FileOrDirectoryNameType { FULLNAME, SIMNAME } 9 //文件/目录 的匹配方式. 10 public enum MatchPattern { STARTWITH, ENDWITH, EQUALS, CONTAINS } 11 12 class FileDirLocationOperator { 13 14 //默认处理的是 路径或文件名. 15 public FileOrDirectoryNameType FileOrDirType = FileOrDirectoryNameType.SIMNAME; 16 //默认 文件/目录名包含指定模式的字符串时,处理. 17 public static MatchPattern OpePattern = MatchPattern.EQUALS; 18 19 public string Root { private set; get; } 20 21 /// <summary> 22 /// 构造函数(初始化存储文件系统操作对象集合). 23 /// </summary> 24 /// <param name="root"></param> 25 /// <param name="FileOrDirType"></param> 26 /// <param name="OpePattern"></param> 27 /// <param name="OpeType"></param> 28 public FileDirLocationOperator(string root, FileOrDirectoryNameType FileOrDirType = FileOrDirectoryNameType.SIMNAME, MatchPattern OpePattern = MatchPattern.EQUALS) { 29 if (String.IsNullOrEmpty(root)) throw new ArgumentNullException(root); 30 Root = root; 31 } 32 33 public void RenameDirBySubDir() { 34 string desFullName = ""; 35 System.IO.DirectoryInfo desDir = null; 36 //第一层目录. 37 foreach (System.IO.DirectoryInfo dir1st in new System.IO.DirectoryInfo(Root).GetDirectories()) { 38 //第二层目录. 39 string desSimName = ""; 40 int endIndex = -1; 41 foreach (System.IO.DirectoryInfo dir2nd in dir1st.GetDirectories()) { 42 endIndex = dir2nd.Name.LastIndexOf("_CSharp"); 43 if (endIndex != -1) { 44 desSimName = dir2nd.Name.Substring(0, endIndex); 45 break; 46 } 47 } //foreach:dir2nd 48 49 if (string.IsNullOrEmpty(desSimName)) continue; 50 desFullName = Root + "\\" + desSimName; 51 desDir = new System.IO.DirectoryInfo(desFullName); 52 if (!desDir.Exists) { 53 desDir.Create(); 54 dir1st.MoveTo(desDir.FullName); 55 } 56 } //foreach:dir1st 57 } 58 59 /// <summary> 60 /// 将指定目录下的所有文件和目录都移到上一目录,并删除被移空的当前指定的目录. 61 /// </summary> 62 /// <param name="dir">指定的目录.</param> 63 public void SubFileSysToParent(System.IO.DirectoryInfo dir) { 64 foreach (System.IO.FileSystemInfo sub in dir.GetFileSystemInfos()) { 65 Move(sub, dir.Parent.FullName + "\\" + sub.Name); 66 } 67 //删除已被移空的目录. 68 if (dir.GetFileSystemInfos().Length == 0) 69 dir.Delete(); 70 } 71 72 /// <summary> 73 /// 按文件的名称在当前目录创建对应的目录,并将文件移进去.例如,在当前目录中有个文件为"fileName.txt", 74 /// 则在当前目录下创建目录"fileName",并将"fileName.txt"移进"fileName"中. 75 /// </summary> 76 /// <param name="filter">文件的后缀名.</param> 77 public void MoveFilesByRandomCreateDir(string filter) { 78 string desFullName = ""; //目标文件的名称. 79 System.IO.DirectoryInfo dirDes = null; 80 foreach (System.IO.FileInfo fInfo in new System.IO.DirectoryInfo(Root).GetFiles(filter)) { 81 desFullName = string.Concat(fInfo.DirectoryName, "\\", System.IO.Path.GetFileNameWithoutExtension(fInfo.Name)); 82 dirDes = new System.IO.DirectoryInfo(desFullName); 83 //对于不存在的目录,需要创建. 84 if (!dirDes.Exists) { 85 dirDes.Create(); 86 } 87 fInfo.MoveTo(string.Concat(dirDes.FullName, "\\", fInfo.Name)); 88 } 89 } 90 91 /// <summary> 92 /// 判断是否匹配. 93 /// </summary> 94 /// <param name="dirOrFileName">源文件或目录名.</param> 95 /// <param name="namePattern">待匹配的字符串.</param> 96 /// <param name="ignoreCase">是否忽略大小写.</param> 97 /// <returns></returns> 98 public static bool Match(string dirOrFileName, string namePattern, bool ignoreCase) { 99 switch (OpePattern) {100 case MatchPattern.CONTAINS:101 return dirOrFileName.Contains(namePattern);102 case MatchPattern.EQUALS:103 if (ignoreCase)104 return dirOrFileName.Equals(namePattern, StringComparison.CurrentCultureIgnoreCase);105 else106 return dirOrFileName.Equals(namePattern, StringComparison.CurrentCulture);107 case MatchPattern.STARTWITH:108 return dirOrFileName.StartsWith(namePattern);109 case MatchPattern.ENDWITH:110 return dirOrFileName.EndsWith(namePattern);111 default:112 throw new ArgumentNullException("MatchPattern:OperationPattern");113 }114 }115 116 /// <summary>117 /// 移动文件或目录.118 /// </summary>119 /// <param name="fSysSrc">源文件或目录.</param>120 /// <param name="desDirName">目标文件或目录名.</param>121 private void Move(System.IO.FileSystemInfo fSysSrc, string desDirName) {122 //文件.123 if (fSysSrc is System.IO.FileInfo) {124 System.IO.FileInfo f = (System.IO.FileInfo)fSysSrc;125 f.MoveTo(desDirName);126 }127 //目录.128 else if (fSysSrc is System.IO.DirectoryInfo) {129 System.IO.DirectoryInfo d = (System.IO.DirectoryInfo)fSysSrc;130 d.MoveTo(desDirName);131 }132 else throw new ArgumentNullException(fSysSrc.FullName);133 }134 }135 }
FileDirLocationOperator - 文件或目录位置操作.
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。