1. 程式人生 > >[Lua] 搜尋資料夾下包含特定字串的檔名稱

[Lua] 搜尋資料夾下包含特定字串的檔名稱

        給自己寫的一個小工具,為了便於在程式碼資料夾中,搜尋包含特定字串的檔名稱,方便debug時查詢一些功能。

        之所以用Lua是因為電腦上只有Lua,懶得安裝別的了。

        在cmd中,和search.lua同一路徑下,輸入lua search.lua "你想要查詢的字串",資料夾名字可在程式碼中修改。

local lfs = require "lfs"
local io = require "io"
local string = require "string"

function SearchforStringInWhichFile (path,finddata)
    for file in lfs.dir(path) do
        if file ~= "." and file ~= ".." and file ~=".svn" then
            local f = path..'\\'..file
            local attr = lfs.attributes (f)
            assert (type(attr) == "table")
            if attr.mode == "directory" then
                SearchforStringInWhichFile(f,finddata)
			elseif attr.mode == "file" then
			    local file = io.open(f,"r")
				local data = file:read("*a")
				file:close()
				if string.find(data,finddata) ~= nil then
				    print(f)
				end
			end
        end
    end
	
end

for i, v in ipairs(arg) do
    if i == 1 then
	    find = v
	end
end

SearchforStringInWhichFile("D:\\code",find)