Files

File Exists

Definition

fileExists(path: string) -> boolean

Usage example

print(fileExists('my_file.lua'))
-- true

Source code

function fileExists(path)
    local f = io.open(path, 'r')
    return f ~= nil and io.close(f)
end

Get Files Recursively

Definition

getFilesRecursive(path: string, maxDepth: integer) -> table
--
-- getFilesRecursive()
--
-- @param  string    path      The starting path: Default: '.' (mod's root folder)
-- @param  integer   maxDepth  The maximum depth allowed. Default: -1 (unlimited)
--
getFilesRecursive('.', -1)

Usage example

local files = getFilesRecursive('.', 2)
files = {
    "./includes/module-1.lua",
    "./includes/module-2.lua",
    "./includes/sub-folder/sub-module-1.lua",
    "./init.lua",
}

Source code

function getFilesRecursive(path, maxDepth, depth, storage)

    -- vars
    path = path or '.'
    maxDepth = maxDepth or -1
    depth = depth or 0
    storage = storage or {}

    -- validate maxDepth
    if maxDepth ~= -1 and depth > maxDepth then
        return storage
    end

    -- sanitize path leading "."
    if not string.find(path, '^%.') then
        path = '.' .. path
    end

    -- sanitize path leading double "//"
    if string.find(path, '^%.//') then
        path = string.gsub(path, '^%.//', './')
    end

    -- get dir/files
    local files = dir(path)

    -- validate files
    if not files or (type(files) == 'table' and #files == 0) then
        return storage
    end

    -- loop files
    for _, file in ipairs(files) do

        -- directory
        if file.type == 'directory' then
            getFilesRecursive(path .. '/' .. file.name, maxDepth, depth + 1, storage)

        -- lua file
        elseif string.find(file.name, '%.lua$') then
            table.insert(storage, path .. '/' .. file.name)
        end

    end

    return storage

end

Get Folders Recursively

Definition

getFoldersRecursive(path: string, maxDepth: int) -> table
--
-- getFoldersRecursive()
--
-- @param  string    path      The starting path: Default: '.' (mod's root folder)
-- @param  integer   maxDepth  The maximum depth allowed. Default: -1 (unlimited)
--
getFoldersRecursive('.', -1)

Usage example

local folders = getFoldersRecursive('.', 2)
folders = {
    "./includes",
    "./includes/sub-folder",
}

Source code

function getFoldersRecursive(path, maxDepth, depth, storage)

    -- vars
    path = path or '.'
    maxDepth = maxDepth or -1
    depth = depth or 0
    storage = storage or {}

    -- validate maxDepth
    if maxDepth ~= -1 and depth > maxDepth then
        return storage
    end

    -- sanitize path leading "."
    if not string.find(path, '^%.') then
        path = '.' .. path
    end

    -- sanitize path leading double "//"
    if string.find(path, '^%.//') then
        path = string.gsub(path, '^%.//', './')
    end

    -- get dir/files
    local files = dir(path)

    -- validate files
    if not files or (type(files) == 'table' and #files == 0) then
        return storage
    end

    -- loop files
    for _, file in ipairs(files) do

        -- directory
        if file.type == 'directory' then
            table.insert(storage, path .. '/' .. file.name)
            getFoldersRecursive(path .. '/' .. file.name, maxDepth, depth + 1, storage)
        end

    end

    return storage

end

Last updated