36

I am wondering if there is a way of getting the path to the currently executing lua script file?

This is specifically not the current working directory, which could be entirely different. I know luafilesystem will let me get the current working directory, but it doesn't seem to be able to tell the current executing script file.

Thanks

EDIT: I'm not running from the standard command line interpreter, I am executing the scripts from a C++ binary via luabind.

11 Answers 11

37

This is a more elegant way:

function script_path()
   local str = debug.getinfo(2, "S").source:sub(2)
   return str:match("(.*/)")
end

print(script_path())
3
  • 1
    If the script is run without a path, such as lua myscript (in which lua assumes this means ./myscript), this function returns nil. I suggest using return str:match("(.*/)") or "."
    – Hisham H M
    Jun 1, 2016 at 17:50
  • 10
    To make this work on Windows replace str:match("(.*/)") with str:match("(.*[/\\])")
    – tversteeg
    Dec 12, 2017 at 14:05
  • Adding to HishamHM's comment: You should return or "./" so the function always returns a path ending with /.
    – idbrii
    Dec 2, 2022 at 20:51
15

If the Lua script is being run by the standard command line interpreter, then try arg[0].

6
  • If I did dofile(subfile) within the original, would this method work for getting the path of the subfile?
    – radman
    Jun 17, 2011 at 3:20
  • 3
    @radman: no. Moreover, dofile uses whatever path it gets and so relative paths are relative to the current working directory of the process. If you want to track files given to dofile, you can redefine it.
    – lhf
    Jun 17, 2011 at 3:24
  • and user could also use loadfile and then run the loaded file as a function call, you may have to redefine loadfile as well Mar 10, 2014 at 6:48
  • Unfortunately, this doesn't work in Lua 5.3. The ... of the main chunk only returns the equivalent of arg[1] onwards.
    – Hisham H M
    Jun 1, 2016 at 18:25
  • @HishamHM, who said anything about ...? arg[0] does work with Lua 5.3.
    – lhf
    Jun 1, 2016 at 18:46
8

Shortest form which I have found looks like this:

debug.getinfo(1).source:match("@?(.*/)")

Index 1, 2- other - depends on which function in call stack you want to query. 1 is last called function (where you're in). If you're running in global context, then probably 2 is more appropriate (haven't tested by myself)

1
  • arg[0]:match("@?(.*/)") is work for me in run from console.
    – Tim
    Jan 11, 2017 at 9:16
6

As lhf says:

~ e$ echo "print(arg[0])" > test.lua
~ e$ lua test.lua
test.lua
~ e$ cd /
/ e$ lua ~/test.lua
/Users/e/test.lua
/ e$ 

Here's the same info using the debug.getinfo mechanism

~ e$ echo "function foo () print(debug.getinfo(1).source) end; foo()" > test.lua
~ e$ lua test.lua
@test.lua
~ e$ cd /
/ e$ lua ~/test.lua
@/Users/e/test.lua
/ e$ 

This is available from the C API lua_getinfo

4

The only reliable way to get what you want is to replace dofile with your own version of this function. Even the debug.getinfo method won't work, because it will only return the string passed to dofile. If that was a relative path, it has no idea how it was converted to an absolute path.

The overriding code would look something like this:

local function CreateDoFile()
    local orgDoFile = dofile;

    return function(filename)
        if(filename) then --can be called with nil.
            local pathToFile = extractFilePath(filename);
            if(isRelativePath(pathToFile)) then
                pathToFile = currentDir() .. "/" .. pathToFile;
            end

            --Store the path in a global, overwriting the previous value.
            path = pathToFile; 
        end
        return orgDoFile(filename); --proper tail call.
    end
end

dofile = CreateDoFile(); //Override the old.

The functions extractFilePath, isRelativePath, and currentDir are not Lua functions; you will have to write them yourself. The extractFilePath function pulls a path string out of a filename. isRelativePath takes a path and returns whether the given path is a relative pathname. currentDir simply returns the current directory. Also, you will need to use "\" instead of "/" on Windows machines.

This function stores the path in a global called path. You can change that to whatever you like.

2
  • 1
    fyi, you can perfectly use / for windows paths too.
    – jpjacobs
    Jun 17, 2011 at 7:49
  • also loadfile should be overridded Jan 23, 2013 at 8:51
4

I have written a function getScriptDir which uses the debug information like a few other people have suggested, but this one is going to work everytime (at least in Windows). But the thing is there are quite a few lines of code as it uses another function string.cut which i have created, which separates a string every given pattern, and puts it into a table.

function string.cut(s,pattern)
  if pattern == nil then pattern = " " end
  local cutstring = {}
  local i1 = 0
  repeat
    i2 = nil
    local i2 = string.find(s,pattern,i1+1)
    if i2 == nil then i2 = string.len(s)+1 end
    table.insert(cutstring,string.sub(s,i1+1,i2-1))
    i1 = i2
  until i2 == string.len(s)+1
  return cutstring
end

function getScriptDir(source)
  if source == nil then
    source = debug.getinfo(1).source
  end
  local pwd1 = (io.popen("echo %cd%"):read("*l")):gsub("\\","/")
  local pwd2 = source:sub(2):gsub("\\","/")
  local pwd = ""
  if pwd2:sub(2,3) == ":/" then
    pwd = pwd2:sub(1,pwd2:find("[^/]*%.lua")-1)
  else
    local path1 = string.cut(pwd1:sub(4),"/")
    local path2 = string.cut(pwd2,"/")
    for i = 1,#path2-1 do
      if path2[i] == ".." then
        table.remove(path1)
      else
        table.insert(path1,path2[i])
      end
    end
    pwd = pwd1:sub(1,3)
    for i = 1,#path1 do
      pwd = pwd..path1[i].."/"
    end
  end
  return pwd
end

Note: if you want to use this function in another OS than Windows, you have to change the io.popen("echo %cd%") in the line 15 to whatever command gives you present working directory in your OS, e.g. io.popen("pwd") for Linux, and the pwd2:sub(2,3) == ":/" in the line 18 to whatever represents the root directory in your OS, e.g. pwd2:sub(1,1) == "/" for Linux.

Note2: if you don't provide the source variable to the function via debug.getinfo(1).source when calling it, then it will return the path to the directory of the file containing this function. Therefore, if you want to get the directory of a file which you called via dofile or loadfile, you will have to give it the source, like this: getScriptDir(debug.getinfo(1).source).

3

This version of anthonygore's answer is cross-platform (handles Windows backslash paths) and works when the script is run without a path (returns relative path).

local function script_path()
    local str = debug.getinfo(2, "S").source:sub(2)
    return str:match("(.*[/\\])") or "./"
end
2

Have a look at the Lua debug library, which is part of the standard Lua distribution. You can use debug.getinfo to find the current file, or the file up N frames on the call stack:

http://www.lua.org/manual/5.1/manual.html#5.9

Note that this is probably fairly slow, so it is not something you want to do on the fast path if you are worried about such things.

1
  • thanks for the idea but I think that is far too much of a filthy hack for my liking ;)
    – radman
    Jun 17, 2011 at 3:18
2

if you want the actual path :

path.dirname(path.abspath(debug.getinfo(1).short_src))

else use this for full file path :

path.abspath(debug.getinfo(1).short_src)
2

If you want the real path including the filename, just use the following

pathWithFilename=io.popen("cd"):read'*all'
print(pathWithFilename)

Tested on Windows.

Explanation:

io.popen - Sends commands to the command line, and returns the output.

"cd" - when you input this in cmd you get the current path as output.

:read'*all' - as io.popen returns a file-like object you can read it with the same kind of commands. This reads the whole output.


If someone requires the UNC path:

function GetUNCPath(path,filename)
local DriveLetter=io.popen("cd "..path.." && echo %CD:~0,2%"):read'*l'
local NetPath=io.popen("net use "..DriveLetter):read'*all'
local NetRoot=NetPath:match("[^\n]*[\n]%a*%s*([%a*%p*]*)")
local PathTMP=io.popen("cd "..path.." && cd"):read'*l'
PathTMP=PathTMP:sub(3,-1)
UNCPath=NetRoot..PathTMP.."\\"..filename
return UNCPath
end
1
  • This returns the working directory of the script, but that may not match the script directory (lua path/to/script/s.lua).
    – idbrii
    Dec 2, 2022 at 21:19
0
arg[0]:match('.*\\')

If it returns nil try changing the .\*\\\ with .*/ and arg[0] with debug.getinfo(1).short_src.

But I find this to be the best and shortest way to get the current directory.

You can of course append the file you are looking for with the .. operator. It will look something like this:

arg[0]:match('.*\\')..'file.lua'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.