PathIsDirectory: Difference between revisions

From Multi Theft Auto: Wiki
Jump to navigation Jump to search
(Created page with "__NOTOC__ {{Shared function}} {{Added feature/item|1.6.1|1.6.0|22470|Checks if a specified path points to a directory.}} ==Syntax== <syntaxhighlight lang="lua"> table pathIsDirectory ( string path ) </syntaxhighlight> {{OOP||path:isDirectory}} ===Required Arguments=== *'''path:''' A string containing a path you want to check against ===Returns=== Returns '''true''' if the path points to a directory, '''false''' otherwise. ==Example==...")
 
m (Fix function return type from table to bool)
 
Line 6: Line 6:
==Syntax==
==Syntax==
<syntaxhighlight lang="lua">
<syntaxhighlight lang="lua">
table pathIsDirectory ( string path )
bool pathIsDirectory ( string path )
</syntaxhighlight>
</syntaxhighlight>
{{OOP||[[path]]:isDirectory}}
{{OOP||[[path]]:isDirectory}}

Latest revision as of 16:44, 10 June 2024

BETA: NEW FEATURE (BUILD: 1.6.0 r22470)
Checks if a specified path points to a directory.

Syntax

bool pathIsDirectory ( string path )

OOP Syntax Help! I don't understand this!

Method: path:isDirectory(...)


Required Arguments

  • path: A string containing a path you want to check against

Returns

Returns true if the path points to a directory, false otherwise.

Example

Click to collapse [-]
Shared

This example lists entire structure of a folder

function string.startsWith(str, start)
    return string.sub(str, 1, #start) == start
end

function string.repetition(what, n)
    local out = ''
    for i=1, n do
        out = out..what
    end
    return out
end

local function getStructure(thePath)
    if thePath:startsWith('/') then
        thePath = thePath:sub(2)
    end

    local structure = {}
    for _, entry in ipairs(pathListDir(thePath)) do
        local entryPath = thePath..'/'..entry
        if pathIsDirectory(entryPath) then
            structure[entry] = getStructure(entryPath)
        elseif pathIsFile(entryPath) then
            structure[entry] = false
        end
    end
    return structure
end

local function printStructure(struct, tab)
    tab = tab or 0
    for entry, isDir in pairs(struct) do
        iprint(string.repetition(' ',tab)..'- '..entry)
        if isDir then
            printStructure(isDir, tab + 2)
        end
    end
end

printStructure(getStructure('.'))

See Also