lua获取window文件的各种路径相关属性

--[[
    for window path
    this function return
    1. file path
    2. file name with extension
    3. file name no extension
    4. file extension
--]] 
local function getFilePathAndName( file )
    local fn_flag = string.find(file, "\\")
    if fn_flag then
        local filepath = string.match(file, "(.+)\\[^\\]*%.%w+$")
        local filename = string.match(file, ".+\\([^\\]*%.%w+)$")
        local name = nil

        local idx = filename:match(".+()%.%w+$")
        if(idx) then
            name = filename:sub(1, idx-1)
        else
            name = filename
        end

        local extension = string.match(filename, ".+%.(%w+)$")

        return filepath, filename, name, extension
    end
end


_file = 'E:\\Test\\hello.lua'
local filepath, filename, name, extension = getFilePathAndName(_file)
print(filepath)
print(filename)
print(name)
print(extension)

输出

E:\Test
hello.lua
hello
lua

 

posted @ 2013-06-14 11:49  *tingliang*  阅读(823)  评论(0编辑  收藏  举报