lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Tue, May 26, 2009 at 7:57 PM, KHMan <keinhong@gmail.com> wrote:
> It's probably not practical to do all at one go, specially the filespec
> part. I think it would fail if the file has multiple dots, or if the file
> starts with a dot.

Here is a more careful function:

function split(path)
   -- do we have an extension? If so, remove it
    local ie = path:find('%.[^%.]+$') or #path+1
    local ext = path:sub(ie)
    path = path:sub(1,ie-1)
   -- find the filename
    local inm = path:find('[/\\][^/\\]+$')  or #path+1
    -- is there a drive letter?
    local id1,id2 = path:find('^%a:')
    if not id1 then
        id1,id2 = 0,0
    end
    return path:sub(id1,id2),path:sub(id2+1,inm-1),path:sub(inm+1),ext
end

print(split [[c:\bonzo\dog\cat.txt]])
print(split [[\here\we\go\again]])
print(split [[\here\we\go\again.dog.txt]])
print(split [[/here/we/go\again]])
print(split [[/here/we/go\again\]])

c:	\bonzo\dog	cat	.txt
	\here\we\go	again	
	\here\we\go	again.dog	.txt
	/here/we/go	again	
        /here/we/go\again\

Altho, to be honest, it's getting to the point where a loop would be
more understandable ;)

steve d.