D语言下文件过滤copy的三种方式比较

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 -release
pause
cpAmrs F:\HJTZY\1a amr f:\weixin_test\

比较结果:在运行了一次后,第3个稳定而且快,第2个只适合当前目录,第1种第一次运行时稍长些,以后可能会比第3个稍少,总体来说,第3个稳定。

原创,转发请标注来自 未来之眼 谢谢。

你可能感兴趣的:(D语言的使用技巧)