Modules/FileSystem/translator/scanDirEx: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ This function loops through all translator filesystem entries that are captured by the wildcard and the directory specifier. The wildcard is glob-style and supports '''*''' and '''?''' modifiers. The scan can be made recursive to enter every directory it finds, so that files and folders of a whole directory tree are captured. This function is used to process filesystem objects inside of directories without having to add the filenames into a configuration file....")
 
No edit summary
 
Line 1: Line 1:
<pageclass class="#3c82c8" subcaption="Translator function"></pageclass>
__NOTOC__
__NOTOC__
This function loops through all translator filesystem entries that are captured by the wildcard and the directory specifier. The wildcard is glob-style and supports '''*''' and '''?''' modifiers. The scan can be made recursive to enter every directory it finds, so that files and folders of a whole directory tree are captured. This function is used to process filesystem objects inside of directories without having to add the filenames into a configuration file. This greatly increases flexibility when processing files. This function is more flexible than [[MTA:Eir/FileSystem/translator/scanDir|scanDir]] as it triggers callbacks directly when either files or directories are found, making a distinction between files and directories very easy.
This function loops through all translator filesystem entries that are captured by the wildcard and the directory specifier. The wildcard is glob-style and supports '''*''' and '''?''' modifiers. The scan can be made recursive to enter every directory it finds, so that files and folders of a whole directory tree are captured. This function is used to process filesystem objects inside of directories without having to add the filenames into a configuration file. This greatly increases flexibility when processing files. This function is more flexible than [[MTA:Eir/FileSystem/translator/scanDir|scanDir]] as it triggers callbacks directly when either files or directories are found, making a distinction between files and directories very easy.

Latest revision as of 03:30, 23 January 2022

This function loops through all translator filesystem entries that are captured by the wildcard and the directory specifier. The wildcard is glob-style and supports * and ? modifiers. The scan can be made recursive to enter every directory it finds, so that files and folders of a whole directory tree are captured. This function is used to process filesystem objects inside of directories without having to add the filenames into a configuration file. This greatly increases flexibility when processing files. This function is more flexible than scanDir as it triggers callbacks directly when either files or directories are found, making a distinction between files and directories very easy.

Syntax

bool translator:scanDirEx ( string dirPath, string wildcard, function dirCallback, function fileCallback, bool recursive )

Arguments

  • dirPath: a path to the directory the scan shall take place or start in
  • wildcard: glob-style wild-card for filename matching; every filename that matches the wild-card is returned
  • dirCallback: the function callback that should trigger for every directory found (can be nil); the absolute path of the directory is passed to it
  • fileCallback: the directory callback that should trigger for every file found (can be nil); the absolute path of the file is passed to it
  • recursive: a boolean that specifies whether the whole directory tree at dirPath should be included into the scan

Returns

This function performs a scan of all filesystem objects captured inside of the dirPath folder using the wild-card glob-style pattern. It returns false if dirPath is an invalid path specifier for the translator.

Example

Click to collapse [-]
Client

This snippet outputs the count of directories and files in a specified directory relative to the resource instance root. This is an alternative to the scanDir way.

-- Get a handle to the resource instance directory.
local resRoot = fileCreateTranslator( "/" );

local function getFilesystemObjectCounts( path )
    local fileCount = 0;
    local dirCount = 0;

    -- Create iterator closures that are triggered for each file and directory
    local function fileIterator( filePath )
        -- filePath is always an absolute path.
        fileCount = fileCount + 1;
    end

    local function dirIterator( dirPath )
        -- filePath is always an absolute path.
        dirCount = dirCount + 1;
    end

    -- Get a list of all fs objects.
    local fsObjects =
        resRoot:scanDirEx(
            path, -- scan anywhere the script wants us to
            "*", -- include all files
            dirIterator, -- pass the callbacks to the routine
            fileIterator,
            false -- scan the specified directory only
        );

    -- We do not need to loop anymore.

    -- Return the counts.
    return fileCount, dirCount;
end

-- Output the filesystem object counts for the resource instance root.
local fileCount, dirCount = getFilesystemObjectCounts( "/" );

outputChatBox( "found " .. fileCount .. " files and " .. dirCount .. " directories in the resource folder root." );

FileSystem Translator Functions