Modules/FileSystem/translator/chdir

From Multi Theft Auto: Wiki
Revision as of 02:51, 19 January 2022 by Danilo (talk | contribs) (Created page with "__NOTOC__ This function changes the current directory pointer of the translator. All operations on the FileSystem translator are executed relative to the current directory. The translator, if possible, is asked to prevent deletion of the current directory. ==Syntax== <syntaxhighlight lang="lua"> bool translator:chdir ( string dirPath ) </syntaxhighlight> ==Arguments== *'''dirPath:''' a path to a directory that should be made current directory ==Returns== This functio...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

This function changes the current directory pointer of the translator. All operations on the FileSystem translator are executed relative to the current directory. The translator, if possible, is asked to prevent deletion of the current directory.

Syntax

bool translator:chdir ( string dirPath ) 

Arguments

  • dirPath: a path to a directory that should be made current directory

Returns

This function returns true if the directory pointed at by dirPath is an existing directory and can be accessed by the translator, false otherwise.

Example

Click to collapse [-]
Client

This snippet creates a simple command-based filesystem explorer.

-- Create a generic FileSystem translator.
local cmdTranslator = fileCreateTranslator( "/" );

-- Set up some commands.
addCommandHandler( "chdir",
    function(...)
        local myPath = table.concat( { ... }, " " );

        cmdTranslator:chdir( myPath );
    end
);

addCommandHandler( "dir",
    function()
        -- todo: resolve a path given as arguments.

        local myEntries = {};

        local function itemIterator( path )
            table.insert( myEntries, cmdTranslator:relPath( path ) );
        end

        cmdTranslator:scanDirEx( "/", "*", itemIterator, itemIterator, false );

        -- Output the filesystem entries to the chatbox.
        for m,n in ipairs( myEntries ) do
            outputChatBox( n );
        end

        -- todo: print statistics (file size, number of files, number of directories, ...)
    end
);

FileSystem Translator Functions