I have read reference of LuaFileSystem, but it seems have no function to get a file's parent folder. And I also search "file" or "dir" in Lua 5.1 Reference Manual, there are just io operations. How should I do?
The ugly method I have thought is cut strings after the last '/' or '\'. Just like C:\\data\\file.text to C:\\data. But I think there should be a better way to do this.
You are correct about LuaFileSystem not having path/name manipulating functions; it's a library that "offers a portable way to access the underlying directory structure and file attributes".
I don't see much wrong with removing the filename using the method you described.
This function using patterns can do the job:
path = "C:\\data\\file.text"
local function getParentPath(_path)
pattern1 = "^(.+)//"
pattern2 = "^(.+)\\"
if (string.match(path,pattern1) == nil) then
return string.match(path,pattern2)
else
return string.match(path,pattern1)
end
end
print(getParentPath(path))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With