Jump to content
  • Sky
  • Blueberry
  • Slate
  • Blackcurrant
  • Watermelon
  • Strawberry
  • Orange
  • Banana
  • Apple
  • Emerald
  • Chocolate
  • Charcoal
  • 0
Henness

require() all files in a folder?

Question

I have a file structure for a program I'm working on that looks like this:

Main.lua
Addons folder
    -API.lua
    -Addon A.lua
    -Addon B.lua
    -Addon Z.lua

My problem is all the information I can find about listing files in a directory is inconsistent, and I don't quite under stand file path format in lua. Some people say its easier with lua filesystem but is that even available in open computers? The way I have been thinking about doing it would be something like this:

for dir in io.popen([[dir "PATHHERESOMEHOW" /b]]):lines() do require(dir) end

First will something like this even work is io.popen even available to use in open computers? Seconded what whould I have to put in the path area to make it work? And Third what does the modifiers after the path mean I saw some use /b and some use /a /b?

I have another question that isn't really related to the above. If I require() the A B and Z files in the API file would the A B and Z files be usable in the Main file if I requre() the API file?

 

Thanks in advance,

Henness

Link to post
Share on other sites

2 answers to this question

Recommended Posts

  • 0
  • Solution

OpenOS has the filesystem API. There's the page about it on the wiki. You'll see the filesystem.list function there. It's exactly what you need.

local fs = require("filesystem")

for node in fs.list("/path") do
  print(node)
end

This code prints all files and directories in /path.

You should also check if the node is a file or a directory. This can be done using the filesystem.isDirectory function.

local fs = require("filesystem")

for file in fs.list("/path") do
  if not fs.isDirectory(file) then
    print(file)
  end
end

You shouldn't use require to load your addon scripts. It just won't work. Use dofile instead.

local fs = require("filesystem")

for file in fs.list("/path") do
  if fs.isDirectory(file) do
    dofile(fs.concat("/path", file))
  end
end

dir "PATH" /b is a Windows command. It won't work in OpenOS.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Answer this question...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use and Privacy Policy.