D语言下文件过滤copy的三种方式比较
来源:互联网 发布:比特彗星监听端口堵塞 编辑:程序博客网 时间:2024/05/23 14:19
module cpAmrs;import std.stdio;import std.file;import std.exception;import std.path:isValidPath,baseName;import std.datetime;import std.string;import std.algorithm.searching;extern(C) int setlocale(int, char*);static this(){ import core.stdc.wchar_; fwide(core.stdc.stdio.stdout, 1); setlocale(0, cast(char*)"china");}void main(string[] args){ writeln(" args[0] "~ args[0]); if(args.length <=1) { writeln("请输入 要查找的目录 要查找的文件扩展名 copy的目地目录"); writeln("程序自动退出!"); return; } writeln("准备对比!"); writeln(" start 0*************************************************************** "); StopWatch sw; sw.start(); cpFiles(args[1],args[2],args[3]); sw.stop(); writeln(" cpFiles use time is ",sw.peek().msecs," msec"); writeln(" start 1*************************************************************** "); sw.reset(); sw.start(); cpFiles1(args[1],args[2],args[3]); sw.stop(); writeln(" cpFiles1 use time is ",sw.peek().msecs," msec"); writeln(" start 2*************************************************************** "); sw.reset(); sw.start(); cpFiles2(args[1],args[2],args[3]); sw.stop(); writeln(" cpFiles2 use time is ",sw.peek().msecs," msec"); }void cpFiles(string strDir,string strExt,string cpTo){ import std.algorithm.iteration; import std.algorithm.searching; if(!isValidPath(strDir)) { writeln(strDir ~ "is not Valid Path."); return; } auto fFiles = dirEntries(strDir, SpanMode.depth).filter!(f => f.name.endsWith("."~strExt)); //if(!exists(cpTo~"\0")) foreach(f; fFiles) { cpFile(f,cpTo~"0\\"); }}void cpFile(DirEntry f,string cpTo){ auto a = SysTime(DateTime.fromISOExtString("2016-03-04T20:00:00")); auto b = SysTime(DateTime.fromISOExtString("2016-03-04T22:30:00")); if(f.timeLastModified.opCmp(a) >= 0 && f.timeLastModified.opCmp(b) <= 0) { writeln("Will Copy file ",f.name); f.copy(cpTo ~ baseName(f.name)); //string strcopy = "copy " ~ f.name ~ " " ~ "F:\\HJTZY2"; //import std.process; //enforce(spawnShell(strcopy.dup()),"copyFile() error"); }}void cpFiles1(string strDir,string strExt,string cpTo){ if(!isValidPath(strDir)) { writeln(strDir ~ "is not Valid Path."); return; } auto dFiles = dirEntries(strDir,"{"~strExt~"}", SpanMode.depth);//.filter!(f => f.name.endsWith("."~strExt)) writeln(strDir); foreach(f; dFiles) { cpFile(f,cpTo); }}void cpFiles2(string strDir,string strExt,string cpTo){ foreach(f;dirEntries(strDir, SpanMode.depth)) { if(!f.name.endsWith("."~strExt)) continue; try { cpFile(f,cpTo~"2\\"); } catch(Exception e) { writeln("file name is "~f.name," catch %s", e.msg); } }}
编译并测试
dmd cpAmrs -releasepausecpAmrs F:\HJTZY\1a amr f:\weixin_test\
比较结果:在运行了一次后,第3个稳定而且快,第2个只适合当前目录,第1种第一次运行时稍长些,以后可能会比第3个稍少,总体来说,第3个稳定。
原创,转发请标注来自 未来之眼 谢谢。
2 0
- D语言下文件过滤copy的三种方式比较
- 获取项目下文件的三种方式
- Linux下文件的三种时间
- Git下文件的三种状态
- 不同方式下文件读取和存储的比较(OpenCV 中FileStorage与C++中文件流ofstream/ifsteam比较)
- Java加载src目录下文件的几种方式
- linux下文件比较的工具
- Linux下文件的三种时间属性
- Linux下文件的三种时间标记
- 操作sys/device下文件节点的三种方法
- Linux下文件的三种时间戳
- CMD下文件copy命令
- VC下文件(夹)拷贝的方式
- windows下文件路径与Linux下文件路径的比较
- dos下文本复制与粘贴的三种方式
- linux下文件的三种时间属性和vim的三种模式
- /etc/ld.so.conf.d/目录下文件的作用
- /etc/ld.so.conf.d/目录下文件的作用
- 手把手教你怎么去除Android APP里面的广告
- 设计模式——策略模式
- ctex与texlive中的pdflatex与xelatex的区别
- fxiaoke 1--- 环境搭建以及常用ant命令及记录
- 序列式容器List之初始化及其几种(不支持随机)访问形式
- D语言下文件过滤copy的三种方式比较
- Eclipse添加hibernate
- 【杭电】[3785]寻找大富翁
- hadoop集群启动成功但live node为0
- Udp打洞和TCP打洞之我见
- POJ 2823 Sliding Window(单调队列||线段树)
- 看了这个就弄明白各种数据类型了
- Linux命令总结
- HDU 1575 Tr A(矩阵快速幂)