Alex
Alex

Reputation: 15333

Run all Lua files in a directory

Lets say I have the file main.lua, and in a sub-directory I have a series of Lua scripts which I would like to run. Is there a way to run all the scripts in the subdirectory in plain vanilla Lua - that is, without needing to load any external modules or packages? The require and dofile work on only single files as far as I can tell, I would like to be able to do something like require "subdir/*".

Upvotes: 1

Views: 3282

Answers (2)

sylvanaar
sylvanaar

Reputation: 8216

Do you have access to os.execute? Can you maintain a list of the modules you want to load?

If you have a full Lua interpreter it is pretty easy to do what you want. (Here's an example for Windows)

 local f = io.popen("dir /b") for mod in f:lines() do require(mod) end

Upvotes: 4

Nicol Bolas
Nicol Bolas

Reputation: 473302

Is there a way to run all the scripts in the subdirectory in plain vanilla Lua - that is, without needing to load any external modules or packages?

No. Lua is designed to be an embedded language. As such, "vanilla Lua" is very small. It has few filesystem-based features; iterating through a directory and pattern-matching files is not possible.

If you are serious about using Lua as a shell-scripting language, then you need to get used to using Lua modules to get things done.

Upvotes: 4

Related Questions