ファイル検索

指定フォルダ以下のフォルダ/ファイルを列挙する

lesson001.js


var fs = WScript.CreateObject("Scripting.FileSystemObject");
var folder = fs.GetFolder(WScript.Arguments(0));

var files = folder.Files;
var enmFile = new Enumerator(files)
for (; !enmFile.atEnd(); enmFile.moveNext())
{
WScript.Echo(enmFile.item().Path);
}
delete enmFile;

var subFolders = folder.SubFolders;
var enmFolder = new Enumerator(subFolders)
for (; !enmFolder.atEnd(); enmFolder.moveNext())
{
WScript.Echo(enmFolder.item().Path);
}
delete enmFolder;

実行結果

C:\>cscript c:\study\jscript\chapter005\lesson001.js c:\study\jscript //nologo
C:\study\jscript\chapter001
C:\study\jscript\chapter002
C:\study\jscript\chapter003
C:\study\jscript\chapter004
C:\study\jscript\chapter005

ちょいと変更

lesson002.js


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

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

var files = folder.Files;
var enmFile = new Enumerator(files)
for (; !enmFile.atEnd(); enmFile.moveNext())
{
WScript.Echo(enmFile.item().Path);
}
delete enmFile;

var subFolders = folder.SubFolders;
var enmFolder = new Enumerator(subFolders)
for (; !enmFolder.atEnd(); enmFolder.moveNext())
{
WScript.Echo(enmFolder.item().Path);
}
delete enmFolder;
}

実行結果

C:\>cscript c:\study\jscript\chapter005\lesson002.js c:\study\jscript //nologo
C:\study\jscript\chapter001
C:\study\jscript\chapter002
C:\study\jscript\chapter003
C:\study\jscript\chapter004
C:\study\jscript\chapter005

サブフォルダも検索

lesson003.js


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

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

var files = folder.Files;
var enmFile = new Enumerator(files)
for (; !enmFile.atEnd(); enmFile.moveNext())
{
WScript.Echo(enmFile.item().Path);
}
delete enmFile;

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

実行結果

C:\>cscript c:\study\jscript\chapter005\lesson003.js c:\study\jscript //nologo
C:\study\jscript\chapter001\jscript.bat
C:\study\jscript\chapter001\lesson001.js
C:\study\jscript\chapter001\lesson002.js
C:\study\jscript\chapter001\lesson003.js
C:\study\jscript\chapter001\lesson004.js
C:\study\jscript\chapter001\lesson005.js
C:\study\jscript\chapter001\lesson011.js
C:\study\jscript\chapter001\test.txt
C:\study\jscript\chapter002\jscript.bat
C:\study\jscript\chapter002\lesson001.js
C:\study\jscript\chapter002\lesson002.js
C:\study\jscript\chapter002\lesson003.js
C:\study\jscript\chapter002\lesson004.js
C:\study\jscript\chapter002\lesson005.js
C:\study\jscript\chapter002\lesson006.js
C:\study\jscript\chapter003\jscript.bat
C:\study\jscript\chapter003\lesson001.js
C:\study\jscript\chapter003\lesson002.js
C:\study\jscript\chapter003\lesson003.js
C:\study\jscript\chapter003\lesson004.js
C:\study\jscript\chapter003\lesson005.js
C:\study\jscript\chapter003\lesson006.js
C:\study\jscript\chapter004\jscript.bat
C:\study\jscript\chapter004\lesson001.js
C:\study\jscript\chapter004\lesson002.js
C:\study\jscript\chapter004\lesson003.js
C:\study\jscript\chapter004\lesson004.js
C:\study\jscript\chapter004\lesson006.js
C:\study\jscript\chapter004\lesson007.js
C:\study\jscript\chapter004\lesson008.js
C:\study\jscript\chapter004\lesson009.js
C:\study\jscript\chapter004\lesson010.js
C:\study\jscript\chapter004\lesson011.js
C:\study\jscript\chapter005\jscript.bat
C:\study\jscript\chapter005\lesson001.js
C:\study\jscript\chapter005\lesson002.js
C:\study\jscript\chapter005\lesson003.js