From 989462da7dbcc9c1c0502efa822a3f24a4224b61 Mon Sep 17 00:00:00 2001 From: Linden <65407488+thelindat@users.noreply.github.com> Date: Mon, 1 Apr 2024 19:13:51 +1100 Subject: [PATCH] feat(imports/server): getFilesInDirectory --- imports/getFilesInDirectory/server.lua | 38 ++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 imports/getFilesInDirectory/server.lua diff --git a/imports/getFilesInDirectory/server.lua b/imports/getFilesInDirectory/server.lua new file mode 100644 index 0000000..7e10157 --- /dev/null +++ b/imports/getFilesInDirectory/server.lua @@ -0,0 +1,38 @@ +---@param path string +---@param pattern string +---@return table string[] +---@return integer fileCount +function lib.getFilesInDirectory(path, pattern) + local resource = cache.resource + + if path:find('^@') then + resource = path:gsub('^@(.-)/.+', '%1') + path = path:sub(#resource + 3) + end + + local files = {} + local fileCount = 0 + local windows = string.match(os.getenv('OS') or '', 'Windows') + local command = ('%s%s%s'):format( + windows and 'dir "' or 'ls "', + (GetResourcePath(resource):gsub('//', '/') .. '/' .. path):gsub('\\', '/'), + windows and '/" /b' or '/"' + ) + + local dir = io.popen(command) + + if dir then + for line in dir:lines() do + if line:match(pattern) then + fileCount += 1 + files[fileCount] = line + end + end + + dir:close() + end + + return files, fileCount +end + +return lib.getFilesInDirectory