コマンドラインで「dir」を使うと任意のフォルダ内にあるファイルの一覧が見れます。
リダイレクトという機能を使うと、コマンドの出力内容をテキストファイルに保存できます。
例えば「dir > c:\app\出力.txt」と入力すると「dir」の出力内容はコマンドラインの画面には表示されず、「c:\app\出力.txt」というファイルに保存されます。
実際の画面は以下。
ファイルができてます。
ファイルの中身は以下の通り。
上記を利用して、プログラムで利用しやすい形式のリストを取得するサンプルを作成しました。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<job> | |
<script> | |
arg = WScript.Arguments | |
if(!arg.length){ | |
WScript.Echo('引数がありません') | |
WScript.Quit() | |
} | |
fs = new ActiveXObject('Scripting.FileSystemObject') | |
shell = new ActiveXObject('WScript.Shell') | |
// テキストファイル入出力 | |
Read = function(path, swCreate, swUnicode){ | |
if(!fs.FileExists(path) && !swCreate){return ''} | |
var rs=fs.OpenTextFile(path,1,swCreate,swUnicode), str=rs.AtEndOfStream?'':rs.ReadAll() | |
rs.Close() | |
return str | |
} | |
Write = function(path,str,swUnicode,sw追記){ | |
with(fs.OpenTextFile(path,sw追記?8:2,true,swUnicode)){Write(str); Close()} | |
} | |
RAS = function(path){return Read(path).split('\r\n')} | |
gsf2 = fs.getSpecialFolder(2) | |
// dirコマンドによるファイル(フォルダ)リスト取得 | |
;(function(){ | |
var funCore=function(path, swDir){ | |
var pathTMP=gsf2+'/'+fs.getTempName()+'.txt' | |
// dirコマンドが完了するまで待機する。 | |
shell.run('cmd /C dir /A'+(swDir?'':'-')+'D /B "'+path+'" > "'+pathTMP+'"', 0, true) | |
var arr=RAS(pathTMP) | |
// 最下行は中身無いので捨てる | |
arr.pop() | |
// 後始末 | |
fs.deleteFile(pathTMP) | |
return arr | |
} | |
getFileList=function(path){ return funCore(path) } | |
getDirList =function(path){ return funCore(path,true) } | |
})(); | |
objPath = {} | |
for(i=0,L=arg.length; i<L ;i++){ | |
path = arg(i) | |
// フォルダではない引数は無視する | |
if(!fs.FolderExists(path)){ continue } | |
str = [ | |
path, | |
'', | |
'[フォルダ]', | |
getDirList(path).join('\r\n'), | |
'', | |
'[ファイル]', | |
getFileList(path).join('\r\n') | |
].join('\r\n') | |
objPath[i] = gsf2 + '/' + fs.getTempName() + '.txt' | |
Write(objPath[i], str) | |
shell.run('notepad '+objPath[i]) | |
} | |
// 後始末する前に少し待機しないと、メモ帳が起動する前にファイルを削除してしまう | |
WScript.Sleep(1000) | |
for(i in objPath){ | |
fs.DeleteFile(objPath[i]) | |
} | |
</script> | |
</job> |
上記スクリプトファイルに「スタートメニュー」フォルダを

ドロップすると、以下の出力結果になります。

ちなみに、Enumeratorを利用したサンプルがありますが、dirコマンドを使う場合と速度を比較すると雲泥の差になります。
実行結果


ドロップすると、以下の出力結果になります。

ちなみに、Enumeratorを利用したサンプルがありますが、dirコマンドを使う場合と速度を比較すると雲泥の差になります。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<job> | |
<script> | |
function ShowFolderFileList(folderspec) | |
{ | |
var fso, f, f1, fc, s; | |
fso = new ActiveXObject("Scripting.FileSystemObject"); | |
f = fso.GetFolder(folderspec); | |
fc = new Enumerator(f.files); | |
s = ""; | |
for (; !fc.atEnd(); fc.moveNext()) | |
{ | |
s += fc.item(); | |
s += "<br>"; | |
} | |
return(s); | |
} | |
fs = new ActiveXObject('Scripting.FileSystemObject') | |
shell = new ActiveXObject('WScript.Shell') | |
// テキストファイル入出力 | |
Read = function(path, swCreate, swUnicode){ | |
if(!fs.FileExists(path) && !swCreate){return ''} | |
var rs=fs.OpenTextFile(path,1,swCreate,swUnicode), str=rs.AtEndOfStream?'':rs.ReadAll() | |
rs.Close() | |
return str | |
} | |
Write = function(path,str,swUnicode,sw追記){ | |
with(fs.OpenTextFile(path,sw追記?8:2,true,swUnicode)){Write(str); Close()} | |
} | |
RAS = function(path){return Read(path).split('\r\n')} | |
gsf2 = fs.getSpecialFolder(2) | |
// dirコマンドによるファイル(フォルダ)リスト取得 | |
;(function(){ | |
var funCore=function(path, swDir){ | |
var pathTMP=gsf2+'/'+fs.getTempName()+'.txt' | |
// dirコマンドが完了するまで待機する。 | |
shell.run('cmd /C dir /A'+(swDir?'':'-')+'D /B "'+path+'" > "'+pathTMP+'"', 0, true) | |
var arr=RAS(pathTMP) | |
// 最下行は中身無いので捨てる | |
arr.pop() | |
// 後始末 | |
fs.deleteFile(pathTMP) | |
return arr | |
} | |
getFileList=function(path){ return funCore(path) } | |
getDirList =function(path){ return funCore(path,true) } | |
})(); | |
arg = WScript.Arguments | |
if(!arg.length){ | |
WScript.Echo('引数がありません') | |
WScript.Quit() | |
} | |
開始 = (new Date()).getTime() | |
a = ShowFolderFileList(arg(0)) | |
終了 = (new Date()).getTime() | |
a時間 = 終了 - 開始 | |
開始 = (new Date()).getTime() | |
b = getFileList(arg(0)) | |
終了 = (new Date()).getTime() | |
b時間 = 終了 - 開始 | |
WScript.Echo(b.length+' files.\nEnumerator:'+a時間+'ミリ秒\ndirコマンド:'+b時間+'ミリ秒') | |
</script> | |
</job> |
実行結果

0 件のコメント:
コメントを投稿