FindFilesInFolder
 
 
 

FindFilesInFolder

Introduced

v5.0

Description

Searches a folder for files with names that match a regular expression. The search is not recursive: only the specified folder is searched. On Windows, the search is case-insensitive; on Linux, it is case-sensitive.

Scripting Syntax

oArray = FindFilesInFolder( Directory, [RegularExpression], [FindAllMatches], [ReturnFullPath] );

Return Value

By default, FindFilesInFolder returns the name of the first file that matches the regular expression. If no match is found, an empty string is returned.

If FindAllMatches is True, FindFilesInFolder returns a JScript Array that contains the names of all the files that match. The VBScript example on this page shows how to handle this JScript array in VBScript.

Parameters

Parameter Type Description
Directory String Full path of the folder to search
RegularExpression String A correctly-formed regular expression

Default Value: ".*", which matches all files

FindAllMatches Boolean True to return all files that match the regular expression. False to return only the first file that matches.

Default Value: false

ReturnFullPath Boolean True to return the full path name of the files that match. False to return only the file name.

Default Value: true

Examples

1. JScript Example

/*
        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 ) ;

2. VBScript Example

'
'       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

3. JScript Example

/*
        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

See Also

CreateFolder FileBrowser XSIUtils