MTA:Eir/FileSystem/translator/chdir: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(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 t...")
 
mNo edit summary
 
Line 4: Line 4:
==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
bool translator.chdir ( string dirPath )  
bool translator:chdir ( string dirPath )  
</syntaxhighlight>
</syntaxhighlight>


Line 25: Line 25:
         local myPath = table.concat( { ... }, " " );
         local myPath = table.concat( { ... }, " " );


         cmdTranslator.chdir( myPath );
         cmdTranslator:chdir( myPath );
     end
     end
);
);
Line 36: Line 36:


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


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


         -- Output the filesystem entries to the chatbox.
         -- Output the filesystem entries to the chatbox.

Latest revision as of 23:23, 16 January 2022

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