Handling filenames without extensions
Posted: 11 Oct 2018 14:34
I'm writing a plugin that needs (among other things) to split a file name into path, filename and extension.
I'm trying to use the following code snippet:
but am finding it fails when the file does not have an extension.
For example, fed "C://example.com/path/image_without_extension"
it returns
whereas "C://example.com/path/image_without_extension.tif"
returns
Ideally I want the function to return:
the complete path
the filename without trailing . or extension
the extension
so e.g.
or
Anyone suggest how I can modify it -- or point me to a utility for creating Lua patterns (I have one for all flavours of regex, but can't find one for patterns)
I'm trying to use the following code snippet:
Code: Select all
function SplitFilename(strFilename)
-- Returns the Path, Filename, and Extension as 3 values
if lfs.attributes(strFilename,"mode") == "directory" then
local strPath = strFilename:gsub("[\\/]$","")
return strPath.."\\","",""
end
return strFilename:match("(.-)([^\\/]-([^\\/%.]+))$")
end
For example, fed "C://example.com/path/image_without_extension"
it returns
Code: Select all
C://example.com/path/
image_without_extension
image_without_extension
returns
Code: Select all
C://example.com/path/
image_without_extension.tif
tif
the complete path
the filename without trailing . or extension
the extension
so e.g.
Code: Select all
C://example.com/path/
image_without_extension
tif
Code: Select all
C://example.com/path/
image_without_extension
nil
Anyone suggest how I can modify it -- or point me to a utility for creating Lua patterns (I have one for all flavours of regex, but can't find one for patterns)