Skip to content

Instantly share code, notes, and snippets.

@Miqueas
Last active March 8, 2022 15:59
Show Gist options
  • Save Miqueas/53cf4344575ccedbf264010442a21dcc to your computer and use it in GitHub Desktop.
Save Miqueas/53cf4344575ccedbf264010442a21dcc to your computer and use it in GitHub Desktop.
[Lua] Small set of some OS and filesystem utilities
-- Determines if this script is running on Windows
os.is_win = package.config:sub(1, 1) == "\\"
-- Return the path to the temp dir
function os.get_temp_dir()
if os.is_win then
-- Windows. Same as:
-- os.getenv("TEMP")
-- os.getenv("TMP")
return os.getenv("UserProfile") .. "/AppData/Local/Temp"
else
-- Unix
return os.getenv("TMPDIR") or "/tmp"
end
end
-- Check if the given file exists
function io.exists(path)
local ok, _, code = os.rename(path, path)
if code == 13 then
-- Permission denied, but it exists
return true
end
return ok ~= nil
end
-- Check if the given path is a folder
function io.is_dir(path)
if os.is_win then
return io.exists(path .. "/")
end
return (io.open(path .. "/") == nil) and false or true
end
-- Returns the path to the user "home" dir
function os.get_home_dir( ... )
if os.is_win then
return os.getenv("UserProfile")
else
return os.getenv("HOME")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment