封装的一个文件操作函数

发布于 2019-06-28 23:06:44
File = {
    Remove = (function(path)
        return exec("rm -rf "..path);
    end),
    List = (function(path)
        return exec("ls "..path);
    end),
    Find = (function(path)
        return exec("find "..path.." -prune");
    end),
    Move = (function(path,to)
        return exec("mv "..path.." "..to);
    end),
    Copy = (function(path,to)
        return exec("cp -rf "..path.." "..to);
    end),
    NewFolder = (function(path)
        return exec("mkdir "..path);
    end),
    NewFile = (function(path)
        return exec("touch "..path);
    end),
    Info = (function(path)
        local _ = {};
        function _:chmod(mode)
            if mode then
                return exec("chmod "..mode.." "..path);
            else
                return string.sub(exec("ls -l "..path),2,10);
            end
        end
        function _:chown(mode)
            if mode then
                return exec("chown "..mode.." "..path);
            else
                return string.match(exec("ls -l "..path),string.rep("[^ ]+[ ]+",2).."([^ ]+)");
            end
        end
        function _:size()
            return string.match(exec("ls -l "..path),string.rep("[^ ]+[ ]+",4).."([^ ]+)");
        end
        function _:time()
            return string.match(exec("ls -l "..path),string.rep("[^ ]+[ ]+",5).."("..string.rep("[^ ]+[ ]+",3)..")");
        end
        return _
    end),
    Read = (function(path)
        local f = io.open(path,"r");
        if f then
            local ret = f:read("*all");
            f:close();
            return ret;
        else
            return "";
        end
    end),
    ReadToTable = (function(path)
        local t = {};
        local f = io.open(path,"r");
        if f then
            for l in f:lines() do
                l = string.gsub(l, "\r", "")
                l = string.gsub(l, "\n", "")
                table.insert(t,l)
            end
            f:close();
        end
        return t;
    end),
    Write = (function(path,info)
        info = type(info) == "table" and table.concat(info,"\r\n") or info
        local f = io.open(path,"w+")
        if f then
            f:write(info)
            f:close()
        end
    end)

}
function exec(command)
    local res = io.popen(command);
    if res then
        local ret = res:read("*a");
        res:close();
        return ret;
    else
        return -1;
    end
end
function main()
    --删除文件或文件夹
    File.Remove("/var/touchelf/log/log.txt");
    --遍历文件
    res = File.List("/var/touchelf/scripts/");
    logDebug(res);
    --查找文件,支持*通配符
    res = File.Find("/var/touchelf/scripts/*.lua");
    logDebug(res);
    --移动文件
    File.Move("/var/touchelf/scripts/1.lua","/var/touchelf/res/1.lua");
    --复制文件
    File.Copy("/var/touchelf/scripts/1.lua","/var/touchelf/res/1.lua");
    --创建文件夹
    File.NewFolder("/var/touchelf/test");
    --创建文件
    File.NewFile("/var/touchelf/scripts/test.lua");
    --查看&修改文件信息
    file = File.Info("/var/touchelf/scripts/1.lua");
    file:chmod(666)--修改文件权限
    file:chown('root')--修改文件所有者
    logDebug(file:chmod())--显示文件权限
    logDebug(file:chown())--显示文件所有者
    logDebug(file:size())--显示文件大小
    logDebug(file:time())--显示文件修改
    --读取文件
    ret = File.Read("/var/touchelf/log/log.txt")
    logDebug(ret)
    --读取文件,转成table
    ret = File.ReadToTable("/var/touchelf/log/log.txt")
    logDebug(ret[1])
    --写入文件
    table.remove(ret,1)--移除数组中第一个元素
    File.Write("/var/touchelf/log/log.txt",ret)
end
3 条评论

发布
问题