bambooz 2017-03-15 03:27 采纳率: 0%
浏览 6247
已结题

lua中如何使用os.execute实现io.popen的find功能

function QMPlugin.setarea1()
    local files = find_file("/User/Containers/Data/Application/*/Documents/clientconfig")
    if #files > 0 then
        local clientconfig = files[1]
        if #files > 1 then
            LuaAuxLib.TracePrint("发现"..#files.."个clientconfig文件目录!(自动清理)", 3)
            local latest_file = nil
            for k, f in pairs(files) do
                if latest_file == nil then
                    latest_file = f
                else
                    local r = io.popen("find "..latest_file.." -newer "..f)
                    local old_file = f
                    if not r then 
                        old_file = latest_file
                        latest_file = f
                    end

                    -- 删除旧文件
                    local dir = string.sub(old_file, 1, -23)
                    remove_file(dir)
                end
            end
            clientconfig = latest_file
        end

        local content = read_file(clientconfig)
        return content--按键中decode后返回
    else
        LuaAuxLib.TracePrint("无法找到clientconfig文件目录!")
    end
end
function QMPlugin.setarea2(area_set,config)
        config.areaSetId = area_set.areaSetId
        config.areaSet = area_set.areaSet
        config.quick_help_info = nil
        config.local_all_login_record = nil
        return config--按键中encode
end
function QMPlugin.setarea3(config)
        local files = find_file("/User/Containers/Data/Application/*/Documents/clientconfig")
    if #files > 0 then
        local clientconfig = files[1]
        if #files > 1 then
            LuaAuxLib.TracePrint("发现"..#files.."个clientconfig文件目录!(自动清理)", 3)
            local latest_file = nil
            for k, f in pairs(files) do
                if latest_file == nil then
                    latest_file = f
                else
                    local r = io.popen("find "..latest_file.." -newer "..f)
                    local old_file = f
                    if not r then 
                        old_file = latest_file
                        latest_file = f
                    end

                    -- 删除旧文件
                    local dir = string.sub(old_file, 1, -23)
                    remove_file(dir)
                end
            end
            clientconfig = latest_file
        end

        local content = read_file(clientconfig)
        --return content--按键中decode后返回
    else
        LuaAuxLib.TracePrint("无法找到clientconfig文件目录!")
    end


        write_file(clientconfig,config)
        return true
end

function find_file(path)
    local a = io.popen("find "..path.." -prune")
    local f = {};
    for l in a:lines() do
        table.insert(f,l)
    end
    return f
end
function remove_file(path)
    return os.execute("rm -rf " .. path)
end
function read_file(file_name)
    local f = io.open(file_name, "r")
    if f ~= nil then
        local data = f:read("*a")
        f:close()
        return data
    end
end
function write_file(file_name, data)
    local f = io.open(file_name, "w")
    if f ~= nil then
        f:write(data)
        f:close()
    end
end


一共3处popen,2处重复
1.io.popen("find "..path.." -prune")
2.io.popen("find "..latest_file.." -newer "..f)
怎么使用os.execute才能实现相同操作

  • 写回答

2条回答 默认 最新

  • 枫舞的季节 2017-03-16 01:40
    关注

    os.execute 和 os.popen 都是一个执行器,以来的是内部实现的命令。os.execute 执行后返回错误码,而os.popen执行后返回file对象。
    如果要用os.execute 实现,你可以用 c 语言写一个,然后让 os.execute 执行就可以了。但我猜你应该都用脚本实现。os.execute 不会比os.popen
    更便捷。

    评论

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料