FileDirLocationOperator - 文件或目录位置操作.

时间:2022-10-27 15:57:11
 using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace MoveFiles.code {
//文件或目录的 FullName 或 Name.
public enum FileOrDirectoryNameType { FULLNAME, SIMNAME }
//文件/目录 的匹配方式.
public enum MatchPattern { STARTWITH, ENDWITH, EQUALS, CONTAINS } class FileDirLocationOperator { //默认处理的是 路径或文件名.
public FileOrDirectoryNameType FileOrDirType = FileOrDirectoryNameType.SIMNAME;
//默认 文件/目录名包含指定模式的字符串时,处理.
public static MatchPattern OpePattern = MatchPattern.EQUALS; public string Root { private set; get; } /// <summary>
/// 构造函数(初始化存储文件系统操作对象集合).
/// </summary>
/// <param name="root"></param>
/// <param name="FileOrDirType"></param>
/// <param name="OpePattern"></param>
/// <param name="OpeType"></param>
public FileDirLocationOperator(string root, FileOrDirectoryNameType FileOrDirType = FileOrDirectoryNameType.SIMNAME, MatchPattern OpePattern = MatchPattern.EQUALS) {
if (String.IsNullOrEmpty(root)) throw new ArgumentNullException(root);
Root = root;
} public void RenameDirBySubDir() {
string desFullName = "";
System.IO.DirectoryInfo desDir = null;
//第一层目录.
foreach (System.IO.DirectoryInfo dir1st in new System.IO.DirectoryInfo(Root).GetDirectories()) {
//第二层目录.
string desSimName = "";
int endIndex = -;
foreach (System.IO.DirectoryInfo dir2nd in dir1st.GetDirectories()) {
endIndex = dir2nd.Name.LastIndexOf("_CSharp");
if (endIndex != -) {
desSimName = dir2nd.Name.Substring(, endIndex);
break;
}
} //foreach:dir2nd if (string.IsNullOrEmpty(desSimName)) continue;
desFullName = Root + "\\" + desSimName;
desDir = new System.IO.DirectoryInfo(desFullName);
if (!desDir.Exists) {
desDir.Create();
dir1st.MoveTo(desDir.FullName);
}
} //foreach:dir1st
} /// <summary>
/// 将指定目录下的所有文件和目录都移到上一目录,并删除被移空的当前指定的目录.
/// </summary>
/// <param name="dir">指定的目录.</param>
public void SubFileSysToParent(System.IO.DirectoryInfo dir) {
foreach (System.IO.FileSystemInfo sub in dir.GetFileSystemInfos()) {
Move(sub, dir.Parent.FullName + "\\" + sub.Name);
}
//删除已被移空的目录.
if (dir.GetFileSystemInfos().Length == )
dir.Delete();
} /// <summary>
/// 按文件的名称在当前目录创建对应的目录,并将文件移进去.例如,在当前目录中有个文件为"fileName.txt",
/// 则在当前目录下创建目录"fileName",并将"fileName.txt"移进"fileName"中.
/// </summary>
/// <param name="filter">文件的后缀名.</param>
public void MoveFilesByRandomCreateDir(string filter) {
string desFullName = ""; //目标文件的名称.
System.IO.DirectoryInfo dirDes = null;
foreach (System.IO.FileInfo fInfo in new System.IO.DirectoryInfo(Root).GetFiles(filter)) {
desFullName = string.Concat(fInfo.DirectoryName, "\\", System.IO.Path.GetFileNameWithoutExtension(fInfo.Name));
dirDes = new System.IO.DirectoryInfo(desFullName);
//对于不存在的目录,需要创建.
if (!dirDes.Exists) {
dirDes.Create();
}
fInfo.MoveTo(string.Concat(dirDes.FullName, "\\", fInfo.Name));
}
} /// <summary>
/// 判断是否匹配.
/// </summary>
/// <param name="dirOrFileName">源文件或目录名.</param>
/// <param name="namePattern">待匹配的字符串.</param>
/// <param name="ignoreCase">是否忽略大小写.</param>
/// <returns></returns>
public static bool Match(string dirOrFileName, string namePattern, bool ignoreCase) {
switch (OpePattern) {
case MatchPattern.CONTAINS:
return dirOrFileName.Contains(namePattern);
case MatchPattern.EQUALS:
if (ignoreCase)
return dirOrFileName.Equals(namePattern, StringComparison.CurrentCultureIgnoreCase);
else
return dirOrFileName.Equals(namePattern, StringComparison.CurrentCulture);
case MatchPattern.STARTWITH:
return dirOrFileName.StartsWith(namePattern);
case MatchPattern.ENDWITH:
return dirOrFileName.EndsWith(namePattern);
default:
throw new ArgumentNullException("MatchPattern:OperationPattern");
}
} /// <summary>
/// 移动文件或目录.
/// </summary>
/// <param name="fSysSrc">源文件或目录.</param>
/// <param name="desDirName">目标文件或目录名.</param>
private void Move(System.IO.FileSystemInfo fSysSrc, string desDirName) {
//文件.
if (fSysSrc is System.IO.FileInfo) {
System.IO.FileInfo f = (System.IO.FileInfo)fSysSrc;
f.MoveTo(desDirName);
}
//目录.
else if (fSysSrc is System.IO.DirectoryInfo) {
System.IO.DirectoryInfo d = (System.IO.DirectoryInfo)fSysSrc;
d.MoveTo(desDirName);
}
else throw new ArgumentNullException(fSysSrc.FullName);
}
}
}