目前我找到的文件夹的搜索工具,最多可以完成把搜索到的单行的内容,进行输出出来,或者进行一些简单的处理,但是不够灵活。
因此就用lua自己写了个,可以完成自己定义搜索处理函数,进行一些数据的处理,省去了将搜索结果放到excel中再处理的过程。
-- search_log.lua tbresult = {}; tbcmdresult = {}; local sztmpfolderpath = os.getenv("temp"); if not sztmpfolderpath then os.execute("md c:\\temp") sztmpfolderpath = "c:\\temp"; end local tbspecialworld = { ["("] = "%(", [")"] = "%)", ["."] = "%.", ["%"] = "%%", ["+"] = "%+", ["-"] = "%-", ["*"] = "%*", ["?"] = "%?", ["["] = "%[", ["]"] = "%]", ["^"] = "%^", ["$"] = "%$", }; function formatcmd(szcmd) return string.gsub(szcmd, ".", function(s) return tbspecialworld[s] or s; end) end function formatpath(szpath) string.gsub(szpath, "[\\/]$", ""); return string.gsub(szpath, "/", "\\"); end function checkfile(szfilepath) local file = io.open(szfilepath, "rb"); if not file then return; end file:close(); return true; end function openfile(szfilepath) if not checkfile(szfilepath) then return; end local tbfile = {}; for line in io.lines(szfilepath) do table.insert(tbfile, line); end return tbfile; end function searchfile(szfilepath, szcmd, fncmd2line, fnfilename) local tbfile = openfile(szfilepath); if not tbfile then return; end tbresult[szfilepath] = tbresult[szfilepath] or {}; local szcmdresult = ""; for nline, szline in ipairs(tbfile) do if string.match(szline, szcmd) then szcmdresult = fncmd2line(szline); if szcmdresult and szcmdresult ~= "" then table.insert(tbcmdresult, szcmdresult); end table.insert(tbresult[szfilepath], nline .. ":" .. szline); end end return 1; end function cmd2line(szline) return; end function checkname(szfilename) return true; end function searchdir(szfolderpath, szcmd, fncmd2line, fncheckname, nidx) if not szcmd or szcmd == "" then return; end local fncmd2line = fncmd2line or cmd2line; local fncheckname = fncheckname or checkname; local nidx = nidx or 0; local sztmpfilename = sztmpfolderpath .. "\\searchdirtemp" .. nidx .. ".tmp"; os.execute("dir /b ".. szfolderpath .." >" .. sztmpfilename); local tbfile = openfile(sztmpfilename); if not tbfile or #tbfile == 0 then return; end local szpath = ""; for _, szfilename in ipairs(tbfile) do szpath = szfolderpath .. "\\" .. szfilename; if not checkfile(szpath) then searchdir(szpath, szcmd, fncmd2line, nidx + 1); else if checkname(szfilename) then searchfile(szpath, szcmd, fncmd2line); end end end end function write2file(szinfo, szfilepath) local file = io.open(szfilepath, "w"); if not file then print(szinfo); print("write2file err ?? not file " .. szfilepath); return; end file:write(szinfo); file:close(); end function dosearchdir(szfolderpath, szcmd, tbparam) if not szfolderpath or szfolderpath == "" or not szcmd or szcmd == "" then return; end tbparam = tbparam or {}; szfolderpath = formatpath(szfolderpath); if tbparam.bismatch then szcmd = formatcmd(szcmd); end local ntime = os.time(); searchdir(szfolderpath, szcmd, tbparam.fncmd2line or cmd2line, tbparam.fncheckname or checkname, 0); ntime = os.time() - ntime; print("搜索用时:" .. ntime); local szresultpath = tbparam.szresultpath or (sztmpfolderpath .. "\\result.tab.tmp"); local szresult = ""; for szfilepath, tbinfo in pairs(tbresult) do szresult = szresult .. szfilepath .. "\n"; for _, szline in pairs(tbinfo) do szresult = szresult .. szline .. "\n"; end end write2file(szresult, szresultpath); local szcmdresult = ""; for _, szline in pairs(tbcmdresult) do szcmdresult = szcmdresult .. szline .. "\n"; end write2file(szcmdresult, tbparam.szcmdresultpath or (sztmpfolderpath .. "\\cmd_result.tab.tmp")); end --tbparam = --{ -- bismatch = false; -- 是否使用正则方式搜索 -- fncmd2line = function () end; -- 自定义搜索行内容处理函数 -- fncheckname = function () end; -- 文件名限定函数 -- szresultpath = "e:\\result.tab"; -- 文件搜索内容输出路径 -- szcmdresultpath = "e:\\cmd_result.tab"; -- 自定义处理函数返回内容储存路径 --}
使用代码可以如下(貌似支持网络路径的):
dofile("e:\\search_log.lua"); tbtmpinfo = {}; function checkinfo(szline) local szplayername, nplayerid, ncount = string.match(szline, "^.*sztype = final\t[^\t]+\t%d+\t([^\t]+)\t(%d+)\t(%d+).*$"); nplayerid = tonumber(nplayerid); ncount = tonumber(ncount); if ncount > tbtmpinfo[nplayerid] then tbtmpinfo[nplayerid] = ncount; return "" .. nplayerid .. "\t" .. ncount; end return; end tbparam = { bismatch = false; fncmd2line = checkinfo; fncheckname = function () return true; end; szresultpath = "e:\\result.tab"; szcmdresultpath = "e:\\cmd_result.tab"; } dosearchdir("d:\\logs", "sztype = final", tbparam); for _, szinfo in pairs(tbtmpinfo) do print(szinfo); end
唯一不满意的地方貌似是搜索速度有点慢,以后有空再调整吧,现在这个暂时够用了,至少比原来方便多了~~
您可能感兴趣的文章:
如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!
网友评论