目前我找到的文件夹的搜索工具,最多可以完成把搜索到的单行的内容,进行输出出来,或者进行一些简单的处理,但是不够灵活。因此就用lua自己写了个,可以完成自己定义搜索处理函数,进行一些数据的处理,省去了将搜索结果放到excel中再处理的过程。
1 -- search_log.lua 2 3 tbResult = {}; 4 tbCmdResult = {}; 5 6 local szTmpFolderPath = os.getenv("temp"); 7 if not szTmpFolderPath then 8 os.execute("md c:\\temp") 9 szTmpFolderPath = "c:\\temp"; 10 end 11 12 local tbSpecialWorld = 13 { 14 ["("] = "%(", [")"] = "%)", ["."] = "%.", ["%"] = "%%", 15 ["+"] = "%+", ["-"] = "%-", ["*"] = "%*", ["?"] = "%?", 16 ["["] = "%[", ["]"] = "%]", ["^"] = "%^", ["$"] = "%$", 17 }; 18 function FormatCmd(szCmd) 19 return string.gsub(szCmd, ".", function(s) return tbSpecialWorld[s] or s; end) 20 end 21 22 function FormatPath(szPath) 23 string.gsub(szPath, "[\\/]$", ""); 24 return string.gsub(szPath, "/", "\\"); 25 end 26 27 function CheckFile(szFilePath) 28 local file = io.open(szFilePath, "rb"); 29 if not file then 30 return; 31 end 32 file:close(); 33 return true; 34 end 35 36 function OpenFile(szFilePath) 37 if not CheckFile(szFilePath) then 38 return; 39 end 40 41 local tbFile = {}; 42 for line in io.lines(szFilePath) do 43 table.insert(tbFile, line); 44 end 45 46 return tbFile; 47 end 48 49 function SearchFile(szFilePath, szCmd, fnCmd2Line, fnFileName) 50 local tbFile = OpenFile(szFilePath); 51 if not tbFile then 52 return; 53 end 54 55 tbResult[szFilePath] = tbResult[szFilePath] or {}; 56 local szCmdResult = ""; 57 for nLine, szLine in ipairs(tbFile) do 58 if string.match(szLine, szCmd) then 59 szCmdResult = fnCmd2Line(szLine); 60 if szCmdResult and szCmdResult ~= "" then 61 table.insert(tbCmdResult, szCmdResult); 62 end 63 table.insert(tbResult[szFilePath], nLine .. ":" .. szLine); 64 end 65 end 66 67 return 1; 68 end 69 70 function Cmd2Line(szLine) 71 return; 72 end 73 74 function CheckName(szFileName) 75 return true; 76 end 77 78 function SearchDir(szFolderPath, szCmd, fnCmd2Line, fnCheckName, nIdx) 79 if not szCmd or szCmd == "" then 80 return; 81 end 82 83 local fnCmd2Line = fnCmd2Line or Cmd2Line; 84 local fnCheckName = fnCheckName or CheckName; 85 local nIdx = nIdx or 0; 86 87 local szTmpFileName = szTmpFolderPath .. "\\SearchDirTemp" .. nIdx .. ".tmp"; 88 os.execute("dir /b ".. szFolderPath .." >" .. szTmpFileName); 89 90 local tbFile = OpenFile(szTmpFileName); 91 if not tbFile or #tbFile == 0 then 92 return; 93 end 94 95 local szPath = ""; 96 for _, szFileName in ipairs(tbFile) do 97 szPath = szFolderPath .. "\\" .. szFileName; 98 if not CheckFile(szPath) then 99 SearchDir(szPath, szCmd, fnCmd2Line, nIdx + 1); 100 else 101 if CheckName(szFileName) then 102 SearchFile(szPath, szCmd, fnCmd2Line); 103 end 104 end 105 end 106 end 107 108 function Write2File(szInfo, szFilePath) 109 local file = io.open(szFilePath, "w"); 110 if not file then 111 print(szInfo); 112 print("Write2File ERR ?? not file " .. szFilePath); 113 return; 114 end 115 116 file:write(szInfo); 117 file:close(); 118 end 119 120 function DoSearchDir(szFolderPath, szCmd, tbParam) 121 if not szFolderPath or szFolderPath == "" or not szCmd or szCmd == "" then 122 return; 123 end 124 125 tbParam = tbParam or {}; 126 127 szFolderPath = FormatPath(szFolderPath); 128 if tbParam.bIsMatch then 129 szCmd = FormatCmd(szCmd); 130 end 131 local nTime = os.time(); 132 SearchDir(szFolderPath, szCmd, tbParam.fnCmd2Line or Cmd2Line, tbParam.fnCheckName or CheckName, 0); 133 nTime = os.time() - nTime; 134 print("搜索用时:" .. nTime); 135 136 local szResultPath = tbParam.szResultPath or (szTmpFolderPath .. "\\result.tab.tmp"); 137 local szResult = ""; 138 for szFilePath, tbInfo in pairs(tbResult) do 139 szResult = szResult .. szFilePath .. "\n"; 140 for _, szLine in pairs(tbInfo) do 141 szResult = szResult .. szLine .. "\n"; 142 end 143 end 144 Write2File(szResult, szResultPath); 145 146 local szCmdResult = ""; 147 for _, szLine in pairs(tbCmdResult) do 148 szCmdResult = szCmdResult .. szLine .. "\n"; 149 end 150 Write2File(szCmdResult, tbParam.szCmdResultPath or (szTmpFolderPath .. "\\cmd_result.tab.tmp")); 151 end 152 153 --tbParam = 154 --{ 155 -- bIsMatch = false; -- 是否使用正则方式搜索 156 -- fnCmd2Line = function () end; -- 自定义搜索行内容处理函数 157 -- fnCheckName = function () end; -- 文件名限定函数 158 -- szResultPath = "e:\\result.tab"; -- 文件搜索内容输出路径 159 -- szCmdResultPath = "e:\\cmd_result.tab"; -- 自定义处理函数返回内容储存路径 160 --}
使用代码可以如下(貌似支持网络路径的):
1 dofile("e:\\search_log.lua"); 2 3 tbTmpInfo = {}; 4 function CheckInfo(szLine) 5 local szPlayerName, nPlayerId, nCount = string.match(szLine, "^.*szType = final\t[^\t]+\t%d+\t([^\t]+)\t(%d+)\t(%d+).*$"); 6 nPlayerId = tonumber(nPlayerId); 7 nCount = tonumber(nCount); 8 if nCount > tbTmpInfo[nPlayerId] then 9 tbTmpInfo[nPlayerId] = nCount; 10 return "" .. nPlayerId .. "\t" .. nCount; 11 end 12 13 return; 14 end 15 16 tbParam = 17 { 18 bIsMatch = false; 19 fnCmd2Line = CheckInfo; 20 fnCheckName = function () return true; end; 21 szResultPath = "e:\\result.tab"; 22 szCmdResultPath = "e:\\cmd_result.tab"; 23 } 24 25 DoSearchDir("d:\\logs", "szType = final", tbParam); 26 27 for _, szInfo in pairs(tbTmpInfo) do 28 print(szInfo); 29 end
唯一不满意的地方貌似是搜索速度有点慢,以后有空再调整吧,现在这个暂时够用了,至少比原来方便多了~~