• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • New resources must be posted under Resources tab. A discussion thread will be created automatically, you can't open threads manually anymore.

[Lua] Get Current Working Directory Linux/Windows

Joined
May 5, 2017
Messages
71
Solutions
5
Reaction score
15
If you write scripts offline and have files in the same directory as the one you are working and would like to get the current directory or load another file in the current directory you can use this function to that since dofile requires an absolute path.

LUA:
function cwd(file)
    local chr = os.tmpname():sub(1,1)
    if chr == "/" then
      -- linux
      chr = "/[^/]*$"
    else
      -- windows
      chr = "\\[^\\]*$"
    end
    return arg[0]:sub(1, arg[0]:find(chr))..(file or '')
end

If file is omitted then it just returns the current directory. I have not had a chance to test it on windows but passing it a string like structure of a windows path did yield the expected outcome.
 
Back
Top