Skip to main content

Exploring Lua Automation IDE: Listing Directories in a Directory

Efficient Directory Enumeration with Lua Automation IDE

Lua Automation IDE provides powerful file handling and directory enumeration capabilities. In this article, we'll explore an example of listing files in a directory, handling non-existent directories, and customizing file type searches. Let's dive in!

-- Prints the directories in the directory specified in
-- the path variable.  If the directory doesn't exist it will end the
-- script early.
local dir = "C:\\Temp"
local counter = 0

if directory.Exists(dir) == false then
	ui.Write("The directory '" .. dir .. "' does not exist")
	do return end
end

-- If you wanted to search by a specific file type or pattern you can
-- uncomment the following line.
-- local files = directory.GetDirectories(dir, "*Avalon*")

local files = directory.GetDirectories(dir)

for k, v in pairs(files) do
    counter = counter + 1
	console.WriteToBuffer(v)	
	console.WriteToBuffer("\r\n")
end

console.WriteToBuffer("\r\n" .. counter .. " directories found.")
console.Flush()

In this code snippet, we demonstrate how to list filenames in a directory using Lua Automation IDE. We start by checking if the directory exists and displaying an error message if it doesn't. Then, we use the directory.GetFiles() function to retrieve the files in the directory. Optionally, you can customize the search by specifying file type filters. Finally, we iterate over the files and display the results. Lua Automation IDE simplifies file handling and empowers your automation scripts.