FileDirLocationOperator - 文件或目录位置操作.
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);
}
}
}
FileDirLocationOperator - 文件或目录位置操作.的更多相关文章
- Perl文件、目录常用操作
注意,这些操作的对象是文件名(相对路径/绝对路径),而非文件/目录句柄,句柄只是perl和文件系统中文件的关联通道,而非实体对象. 创建文件 在unix类操作系统中有一个touch命令可以非常方便的创 ...
- 【node】fs模块,文件和目录的操作
检查文件是否存在,查询文件信息 fs.stat() fs.stat('./server.js', function (err, stat) { if (stat && stat.isF ...
- 转:eclipse打开文件或者目录位置(不使用插件)
http://my.oschina.net/havoc/blog/204958?p= 首先说不使用插件的方法 WINDOWS eclipse配置 Run-->External Tools--&g ...
- Java——文件及目录File操作
API file.listFiles(); //列出目录下所有文件及子目录fileList[i].isFile() //判断是否为文件 fileList[i].isDirectory() //判断是否 ...
- Python之文件操作:文件、目录的操作
一.创建 1.创建文件 open(path,'w') 2.创建目录 (1)os.mkdir(pt[, mode=0777]) 新建一个目录pt,参数mode表示生成的目录的权限,默认是超级权限,也就是 ...
- 12 hdfs常用文件、目录拷贝操作、删除操作
package com.da.hbase.tool.utils; import com.da.hbase.tool.common.Const; import org.apache.hadoop.con ...
- Java NIO.2 —— 文件或目录拷贝操作
拷贝整个文件树是可以递归每个目录和文件调用 Files.copy()方法.在使用的时候有一下注意事项. 在往目录拷贝文件之前,首先要保证目录已经存在.拷贝源目录(不论是否为空)都会生成目标目录.整个任 ...
- Java NIO.2 —— 文件或目录删除操作
文件删除 删除单个文件的操作很简单,如果要删除一个目录树的话,需要实现FileVisitor 接口然后递归地调用delete() 或deleteIfExists()方法.在看代码之前,需要注意一下问题 ...
- eclipse打开文件或者目录位置
1.点击Run-->External Tools-->External Tools Configurations... 右击program,点击new 2.填写名称,Location,Ar ...
随机推荐
- asp.net能不托管吗?
弱弱地问一句,整个部署在IIS中的asp.net项目能不托管吗? 或者说有没有用纯粹的非托管语言(比方说C语言)写的非托管asp.net项目?
- image转文件
UIImage *image = self.imageCompainPhoto.image; NSData *imageData = UIImagePNGRepresentation(image); ...
- Python3 如何优雅地使用正则表达式(详解七)
常见问题 正则表达式是一个非常强大的工具,但在有些时候它并不能直观地按照你的意愿来运行.本篇我们将指出一些最常见的错误. 使用字符串方法 有时使用 re 模块是个错误!如果你匹配一个固定的字符串或者单 ...
- Spark任务调度流程及调度策略分析
Spark任务调度 TaskScheduler调度入口: (1) CoarseGrainedSchedulerBackend 在启动时会创建DriverEndPoint. 而DriverE ...
- LIBSVM之一
libSVM简单的介绍 libSVM是台湾林智仁(Chih-Jen Lin) 教授2001年开发的一套支持向量机库,这套库运算速度挺快,可以很方便的对数据做分类或回归.由于libSVM程序小,运用灵活 ...
- Ubuntu下Git服务端搭建
1安装git $ sudo add-apt-repository ppa:git-core/ppa $ sudo apt-get update $ sudo apt-get install git 测 ...
- $provide.decorator
$provide.decorator 是angular 提供的一个功能. 目的是让我们可以扩展或装修我们的服务. var app = angular.module("app", [ ...
- ural 1192 Ball in a Dream
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> # ...
- mkbundle(1) - Linux man page
mkbundle(1) - Linux man page Name mkbundle, mkbundle2 - Creates a bundled executable. Synopsis mkbun ...
- Web应用部署工具
Fabric----python写的,没试用过,基本上是local函数是调用本地命令,run是调用远程命令,看了些sample,还是觉得挺方便的. jekins------java的集成测试工具,也可 ...