get current working directory in Lua

40,707

Solution 1

Lua by default doesn't have a "native" way of supporting the concept of "current directory", or, in fact, the concept of "directory".

The proper way to get the current directory is using a library that provides folder support. There are several - I recommend luafilesystem.

Once it is installed, you can get the current directory by executing:

lfs.currentdir()

This will work on windows, linux and mac.

Beware though that these external libraries usually involve some binary packages. Depending on your setup, you might have to compile it before being able to use it.

EDIT:

Note that when a file is included via require, then the expression {...}[1] returns the path used by the require directive. It is not exactly the path because:

  • It uses dots to separate directories and supresses the .lua at the end of the file.
  • It is relative to the path from which the lua process was initialized
  • It depends on the configuration of package.path

But if all you need is the "require-like path" of the file (maybe to require files next to it) then you can get it by doing this at the very beginning of the file:

local PATH = (...):match("(.+)%.[^%.]+$") or (...)

If a file called baz.lua is required with require 'foo.bar.baz', then PATH will be foo.bar.

Solution 2

maybe some ugly hack like

current_dir=io.popen"cd":read'*l'

Solution 3

You should be able to get the currently-running lua file path with:

debug.getinfo(1).short_src;

or

debug.getinfo(1).source;

and then the current directory with a regex:

string.gsub(debug.getinfo(1).short_src, "^(.+\\)[^\\]+$", "%1");

Edit: actually that only works if you're running your lua with the full path. e.g.: "lua.exe C:\test\test.lua" and NOT "lua.exe test.lua"

Solution 4

I haven't had time to test this, but have you tried os.getenv to read windows environment variables?

Windows has an environment variable for current directory: %CD%

os.getenv("CD")

Edit: Tested on Windows 7 and while other environment variables work (ie. %USERNAME% or %PROGRAMFILES%) the CD var returns nil

Share:
40,707

Related videos on Youtube

Admin
Author by

Admin

Updated on October 16, 2021

Comments

  • Admin
    Admin about 1 year

    What's the Lua to get the current working directory on Windows XP SP3 (or to get the directory of the currently-running Lua file)? I prefer not to use LuaFileSystem.

    I can't use os.execute("cd") because os.execute always starts from my home directory (and thus always yields C:\Documents and Settings\username).

  • Admin
    Admin over 11 years
    This seems to have the same effect as os.execute("cd").
  • jpjacobs
    jpjacobs over 11 years
    nope, os.execute'cd' only prints the output, while the io.popen thing captures it, and puts it in the current_dir variable
  • cib
    cib over 10 years
    Thanks, this is just what I needed.
  • Arun R over 9 years
    Have to close the "popen" - maybe store the popen in a local and close it after the read. Otherwise this will leak handles.
  • Gus E
    Gus E over 8 years
    In Linux I had to use debug.getinfo(1).source and change the regex to string.gsub(debug.getinfo(1).source, "^@(.+/)[^/]+$", "%1").
  • Craig Barnes
    Craig Barnes almost 8 years
    @ArunR It will only "leak" handles temporarily until they're garbage collected. Try print(io.popen("ls").__gc).

Related