v5.0
1 つのフォルダを検索し、ファイル名が正規表現の条件と一致するファイルを探します。 再帰的な検索は実行せずに指定のフォルダのみを検索します。 Windows では大文字/小文字が区別されず、Linux では区別されます。
oArray = FindFilesInFolder( Directory, [RegularExpression], [FindAllMatches], [ReturnFullPath] ); |
デフォルトでは、FindFilesInFolder は正規表現と初めて一致したファイルの名前を戻します。
ファイルが見つからない場合は空の文字列を戻します。
FindAllMatches を True に設定すると、FindFilesInFolder
は条件と一致するすべてのファイルの名前を含むJScript 配列を戻します。 このページの VBScript の例は、VBScript で
JScript 配列を処理する方法を説明するものです。
| パラメータ | タイプ | 詳細 |
|---|---|---|
| Directory | 文字列 | 検索するファイルの完全パス |
| RegularExpression | 文字列 | 有効な書式の正規表現
デフォルト値:".*"(すべてのファイル) |
| FindAllMatches | ブール | True の場合、正規表現と一致するすべてのファイルが戻されます。 False の場合、最初に一致したファイルのみを戻します。
デフォルト値: false |
| ReturnFullPath | ブール | True の場合は一致するファイルの完全パスを戻します。 False の場合はファイル名のみを戻します。
デフォルト値: true |
/*
Example that shows how to get all the .scntoc files in the
current project
*/
var strTestFolder = XSIUtils.BuildPath( Application.InstallationPath( siProjectPath ), "Scenes" ) ;
/*
This regular expression matches any string
that ends with ".scntoc". This is the same
as the command-line wildcard "*.scntoc".
*/
var regularExpressionToMatch = ".*\.scntoc$" ;
var aTocFiles = FindFilesInFolder(
strTestFolder,
regularExpressionToMatch,
true, // Find all matches
false ) ; // Return file name only
strMsg = "\nFound the following .scntoc files in " + strTestFolder + "\n" ;
for ( var i = 0 ; i < aTocFiles.length ; i++ )
{
strMsg += aTocFiles[i] + "\n" ;
}
Application.LogMessage( strMsg ) ;
|
'
' Example that shows how to get the list of .scntoc files
'
'in a folder.
dim strTestFolder, strTocFiles, strMsg, aTocFiles, regularExpressionToMatch
strTestFolder = XSIUtils.BuildPath( Application.InstallationPath( siProjectPath ),"Scenes" )
' This regular expression matches any string
' that ends with ".scntoc". This is the same
' as the command-line wildcard "*.scntoc".
regularExpressionToMatch = ".*\.scntoc$"
'The command returns a JScript array, which appears as a
'comma-delimited string in VBScript
strTocFiles = FindFilesInFolder( _
strTestFolder, _
regularExpressionToMatch, _
true, _
false )
'It is easy to convert it to a VBScript array
aTocFiles = split( strTocFiles, "," )
strMsg = vbCrLf & "Found the following .scntoc files in " & strTestFolder & vbCrLf
for i = 0 to Ubound( aTocFiles )
strMsg = strMsg & aTocFiles(i) & vbCrLf
next
Application.LogMessage strMsg
|
/*
This example shows how the FindFilesInFolder command could
be implemented in JScript as a self-installed custom command.
The custom command uses the FileSystemObject class and
JScript Regular Expression support
*/
function XSILoadPlugin( in_reg )
{
in_reg.Author = "Softimage SDK";
in_reg.Name = "FindFilesInFolderExample";
in_reg.Major = 1;
in_reg.Minor = 0;
in_reg.RegisterCommand("FindFilesInFolder","FindFilesInFolder");
return true;
}
function FindFilesInFolder_Init( ctxt )
{
var oCmd = ctxt.Source;
oCmd.Description = "Search for files with a particular pattern in a directory";
oCmd.SetFlag(siNoLogging,true);
oCmd.ReturnValue = true;
var oArgs = oCmd.Arguments;
oArgs.Add("Directory",siArgumentInput,"", siString);
oArgs.Add("RegularExpression",siArgumentInput,".*", siString);
oArgs.Add("FindAllMatches",siArgumentInput, false, siBool );
oArgs.Add("ReturnFullPath",siArgumentInput, true, siBool );
return true;
}
function FindFilesInFolder_Execute
(
in_Directory,
in_RegularExpression,
in_FindAllMatches,
in_ReturnFullPath
)
{
var strFlags = "" ;
if ( !XSIUtils.IsFileSystemCaseSensitive() )
strFlags = "i" ; // Case-insensitive filesystem
var oRegExp = new RegExp( in_RegularExpression, strFlags ) ;
var oFSO = new ActiveXObject( "Scripting.FileSystemObject" ) ;
var oFolder = oFSO.GetFolder( in_Directory ) ;
var aResults = new Array() ;
enumFiles = new Enumerator(oFolder.files);
for (; !enumFiles.atEnd(); enumFiles.moveNext())
{
var oFile = enumFiles.item();
strName = oFile.Name ;
if ( 0 == strName.search( oRegExp ) )
{
if ( in_ReturnFullPath )
strMatch = XSIUtils.BuildPath( in_Directory, oFile.Name ) ;
else
strMatch = oFile.Name ;
if ( in_FindAllMatches )
aResults.push( strMatch ) ;
else
return strMatch ;
}
}
if ( in_FindAllMatches )
return aResults ;
else
return "" ; // No Matches
}
// Add a push method to the JScript Array Object
// (Array.Push was added in Jscript 5.5 but we cannot rely on this)
// @cc_on
// @if (@_jscript_version < 5.5)
var push = function(){
for( var i = 0; arguments[ i ] != null; i++ )
this[this.length++] = arguments[ i ];
return( this );
}
Array.prototype.push = push;
// @end
|