* Split a Filename Code Snippet

For users to report plugin bugs and request plugin enhancements; and for authors to test new/new versions of plugins, and to discuss plugin development (in the Programming Technicalities sub-forum). If you want advice on choosing or using a plugin, please ask in General Usage or an appropriate sub-forum.
Post Reply
User avatar
tatewise
Megastar
Posts: 28434
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Split a Filename Code Snippet

Post by tatewise »

The plugins:code_snippets:split_filename_in_to_path_filename_and_extension|> Split a Filename into Path, File, and Extension (code snippet) exhibits an interesting effect with an FH Project folder such as ...\FH Projects\xyz\xyz.fh_data.

It returns the following three values:
...\FH Projects\xyz\
xyz.fh_data
fh_data
which is somewhat misleading as xyz.fh_data is a folder and NOT a file, and fh_data is NOT a file type.

If a folder such as ...\FH Projects\xyz is used it returns three nil values.

The following alternative function works rather better:

Code: Select all

-- Return the Path, Filename, and extension as 3 values
function SplitFilename(strFilename)
	if lfs.attributes(strFilename,"mode") == "file" then
		return strFilename:match("(.-)([^\\]-([^\\%.]+))$")
	end
	local strPath = strFilename:gsub("[\\/]$","")
	return strPath.."\\","",""
end -- function SplitFilename
In the above cases it returns the values:
...\FH Projects\xyz\xyz.fh_data\ or ...\FH Projects\xyz\
plus two empty strings.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
User avatar
Jane
Site Admin
Posts: 8518
Joined: 01 Nov 2002 15:00
Family Historian: V7
Location: Somerset, England
Contact:

Re: Split a Filename Code Snippet

Post by Jane »

Good idea Mike, I would be tempted to put both the in the snippets, as the original one is the one found on many other Lua sites.
Jane
My Family History : My Photography "Knowledge is knowing that a tomato is a fruit. Wisdom is not putting it in a fruit salad."
User avatar
tatewise
Megastar
Posts: 28434
Joined: 25 May 2010 11:00
Family Historian: V7
Location: Torbay, Devon, UK
Contact:

Re: Split a Filename Code Snippet

Post by tatewise »

I have updated plugins:code_snippets:split_filename_in_to_path_filename_and_extension|> Split a Filename into Path, File, and Extension (code snippet) as suggested.
Mike Tate ~ researching the Tate and Scott family history ~ tatewise ancestry
Post Reply