ファイル検索

指定フォルダ以下のファイルを列挙する (更新時刻順にソート)

lesson005.js


var fs = WScript.CreateObject("Scripting.FileSystemObject");
var hash = new Array();
putsDis(fs, WScript.Arguments(0), hash);

var array = new Array();
for (file in hash)
{
array.push({key:file, val:hash[file]});
}
array.sort(function (a,b){ return (a.val > b.val) ? 1 : -1 ; });
for (var i = 0; i < array.length; i++)
{
WScript.Echo(array[i].val + " " + array[i].key);
}

function putsDis(fs, path, hash)
{
var folder = fs.GetFolder(path);

var files = folder.Files;
var enmFile = new Enumerator(files)
for (; !enmFile.atEnd(); enmFile.moveNext())
{
var date = new Date(enmFile.item().DateLastModified);
var y = date.getFullYear();
var m = date.getMonth() + 1;
var d = date.getDate();
var h = date.getHours();
var n = date.getMinutes();
var s = date.getSeconds();

y = LPad(y,"0000") + "\/";
m = LPad(m,"00") + "\/";
d = LPad(d,"00") + " ";
h = LPad(h,"00") + ":";
n = LPad(n,"00") + ":";
s = LPad(s,"00");

hash[enmFile.item().Path] = y + m + d + h + n + s;
}
delete enmFile;

var subFolders = folder.SubFolders;
var enmFolder = new Enumerator(subFolders)
for (; !enmFolder.atEnd(); enmFolder.moveNext())
{
putsDis(fs, enmFolder.item().Path, hash);
}
delete enmFolder;
}

function LPad(source, pad)
{
source = pad + source;
source = source.substr(source.length - pad.length, pad.length);
return source;
}

連想配列を使う意味は無かったな。。。

実行結果


C:\>cscript c:\study\jscript\chapter005\lesson005.js c:\study\jscript //nologo
2008/05/22 23:41:42 C:\study\jscript\chapter003\lesson005.js
2008/07/03 12:54:15 C:\study\jscript\chapter001\lesson001.js
2008/07/03 12:58:09 C:\study\jscript\chapter001\lesson002.js
2008/07/03 12:58:15 C:\study\jscript\chapter001\lesson003.js
2008/07/03 12:58:22 C:\study\jscript\chapter001\lesson004.js
2008/07/03 12:58:27 C:\study\jscript\chapter001\lesson005.js
2008/07/03 13:23:23 C:\study\jscript\chapter002\lesson001.js
2008/07/03 14:25:46 C:\study\jscript\chapter002\lesson002.js
2008/07/03 14:32:02 C:\study\jscript\chapter002\lesson003.js
2008/07/07 16:31:38 C:\study\jscript\chapter002\lesson004.js
2008/07/07 16:35:08 C:\study\jscript\chapter002\lesson005.js
2008/07/07 23:05:36 C:\study\jscript\chapter002\jscript.bat
2008/07/07 23:07:22 C:\study\jscript\chapter002\lesson006.js
2008/07/08 00:01:55 C:\study\jscript\chapter003\lesson001.js
2008/07/08 00:17:42 C:\study\jscript\chapter003\lesson002.js
2008/07/08 09:58:28 C:\study\jscript\chapter003\lesson003.js
2008/07/08 13:27:22 C:\study\jscript\chapter003\lesson004.js
2008/07/08 13:39:50 C:\study\jscript\chapter001\test.txt
2008/07/08 14:00:10 C:\study\jscript\chapter003\lesson006.js
2008/07/08 14:01:56 C:\study\jscript\chapter003\jscript.bat
2008/07/08 16:10:54 C:\study\jscript\chapter004\lesson001.js
2008/07/08 16:14:42 C:\study\jscript\chapter004\lesson002.js
2008/07/08 16:17:13 C:\study\jscript\chapter004\lesson003.js
2008/07/09 14:33:51 C:\study\jscript\chapter004\lesson004.js
2008/07/10 09:51:41 C:\study\jscript\chapter004\lesson006.js
2008/07/16 10:28:02 C:\study\jscript\chapter001\jscript.bat
2008/07/17 11:04:04 C:\study\jscript\chapter001\lesson011.js
2008/07/23 15:20:02 C:\study\jscript\chapter004\lesson007.js
2008/07/23 15:38:04 C:\study\jscript\chapter004\lesson008.js
2008/07/24 11:02:53 C:\study\jscript\chapter004\lesson011.js
2008/07/24 11:03:09 C:\study\jscript\chapter004\lesson010.js
2008/07/24 11:03:22 C:\study\jscript\chapter004\jscript.bat
2008/07/24 11:03:29 C:\study\jscript\chapter004\lesson009.js
2008/08/02 09:58:04 C:\study\jscript\chapter005\lesson003.js
2008/08/02 09:58:11 C:\study\jscript\chapter005\lesson002.js
2008/08/02 09:58:17 C:\study\jscript\chapter005\lesson001.js
2008/08/02 10:49:16 C:\study\jscript\chapter005\lesson004.js
2008/08/02 10:50:05 C:\study\jscript\chapter005\jscript.bat
2008/08/02 11:45:10 C:\study\jscript\chapter005\lesson005.js