get current working directory in Lua

后端 未结 5 1258
北荒
北荒 2021-01-04 01:37

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 LuaFileSyste

5条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-04 02:40

    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"

提交回复
热议问题