This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
EveryOS's profile picture

Get directory a file called with loadfile is in

Started by EveryOS, 10 October 2017 - 02:56 PM
EveryOS #1
Posted 10 October 2017 - 04:56 PM
SO basically, I would like to make a multi-file program that can be put into any directory.

Thing is, it is called with loadfile so the following will not work:


fs.getDir(shell.getRunningProgram())

SO let's say I have a script like this:


--IN test.lua
write('DIR: ')
dir = read()
loadfile(fs.combine(dir, 'test.lua'), _G)()

--IN <somedir>/test.lua
dir =
  ...
print(dir)

How could I get the dir of the running file's directory instead of the shell's current path?
KingofGamesYami #2
Posted 10 October 2017 - 05:23 PM
You can't.

loadfile completely separates the file path (including the directory) from the executable code because it creates an anonymous function.

Obviously you could pass the directory as an argument, as the program which is calling loadfile certainly knows where that is.
EveryOS #3
Posted 10 October 2017 - 05:27 PM
Ok. That sounds good I guess.
Lupus590 #4
Posted 10 October 2017 - 08:55 PM
You might be able to ask the shell for the current directory, this may not be the directory of the file though.