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 ...
随机推荐
- 你好,C++(23) 4.4.2 工资程序成长记:用数组处理批量数据,用循环结构执行重复动作
4.4 从语句到程序 了解了各种表达式和语句之后,就相当于掌握了写作文要用到的词语和句子,但是,仅有词语和句子是无法构成一篇有意义的文章的.要完成一篇文章,先需要确定这篇文章的结构,是先分述再总述, ...
- deb文件安装命令
一般在此类发行版中可以直接双击安装 手动安装.如果您喜欢使用终端,您需要管理员权限来安装一个 .deb 文件. 打开终端后,输入: sudo dpkg -i package_file.deb 要卸载一 ...
- GetWindowText
用于得到窗口中的数据 {// TODO: If this is a RICHEDIT control, the control will not// send this notification un ...
- Input File 表单上传按钮美化
HTML <div class="input-file-button"> 上传图片<input type="file" class=" ...
- getAttribute:取得属性; setAttribute:设置属性。
input.setAttribute("title"); 当它是一个值,就是取值. input.setAttribute("title", "hell ...
- Linux_ERROR 1045 (28000): Access denied for user 'root'@'localhost'
MySQL生成了root用户的随机密码(如下截图所示),并将这个随机密码放置在/root/.mysql_secret中.并且强制在第一次登陆时修改root用户的密码.Mysql 5.6及以后版本出处于 ...
- curl 提交请求
http://forums.phpfreaks.com/topic/194255-curl-sending-array-as-post-value/ http://www.cnblogs.com/ch ...
- 设计模式 ( 十八 ):State状态模式 -- 行为型
1.概述 在软件开发过程中,应用程序可能会根据不同的情况作出不同的处理.最直接的解决方案是将这些所有可能发生的情况全都考虑到.然后使用if... ellse语句来做状态判断来进行不同情况的处理.但是对 ...
- Linux内核定时器
Linux使用struct timer_list来描述一个定时器. 重要成员: expires:定时时长 *function:超时执行函数名使用流程: 1.定义定时器变量 /*定义定时器变量结构 ...
- 美国易安信公司 EMC
EMC 提供了帮助您利用这一资产的工具.凭着我们的系统.软件.服务和解决方案,我们能够与您一道为您的公司打造一个综合性的信息基础架构.我们帮助客户设计.构建和管理智能.灵活而且安全的信息基础架构.这些 ...