`
lua
  • 浏览: 74435 次
  • 性别: Icon_minigender_1
  • 来自: 大连
文章分类
社区版块
存档分类
最新评论

Windows下Lua进行目录文件查找

阅读更多

from http://sunxiunan.com/?p=1285

 

目的:使用lua进行某个目录下特定类型或指定文件名的查找,并给出一个查找使用核心对象的函数实例。

require"lfs"

function findindir (path, wefind, r_table, intofolder)
    for file in lfs.dir(path) do
        if file ~= "." and file ~= ".." then
            local f = path..'\\'..file
            --print ("\t "..f)
            if string.find(f, wefind) ~= nil then
                --print("\t "..f)
                table.insert(r_table, f)
            end
            local attr = lfs.attributes (f)
            assert (type(attr) == "table")
            if attr.mode == "directory" and intofolder then
                findindir (f, wefind, r_table, intofolder)
            else
                --for name, value in pairs(attr) do
                --    print (name, value)
                --end
            end
        end
    end
end

要注意的是,这段代码使用了LuaFileSystem模块,最好使用Lua for windows这个统合安装包,里面已经包含了。

使用方法很简单,比如我想查找c盘下temp目录的所有cpp文件:

local currentFolder = [[C:\temp]]
-------------------------------------
local input_table = {}
findindir(currentFolder, "%.cpp", input_table, true)

查找到的结果放在input_table中。另外可以使用第四个参数控制是否查找子目录。

给出一个实际的例子,想查找那些函数可能使用kernal object,通过它可以帮助查找是否可能存在handle泄露的情况。当我们找到文件,会在里面查找这些函数名,然后打印出行号。

参考文档是http://msdn.microsoft.com/en-us/library/ms724485%28VS.85%29.aspx

local currentFolder = [[C:\temp]]

local input_table = {}
findindir(currentFolder, "%.cpp", input_table, true)
findindir(currentFolder, "%.h", input_table, true)

    local tbl_found1 = {"CreateRestrictedToken",
                    "DuplicateToken",
                    "DuplicateTokenEx",
                    "OpenProcessToken",
                    "OpenThreadToken",
                    "FindFirstChangeNotification",
                    "CreateFile",
                    "GetThreadDesktop",
                    "OpenProcessToken",
                    "CreateEvent",
                    "CreateEventEx",
                    "OpenEvent",
                    "OpenEventLog",
                    "RegisterEventSource",
                    "OpenBackupEventLog",
                    "CreateFile",
                    "CreateFileMapping",
                    "OpenFileMapping",
                    "FindFirstFile",
                    "HeapCreate",
                    "CreateIoCompletionPort",
                    "CreateJobObject",
                    "CreateMailslot",
                    "CreateMemoryResourceNotification",
                    "LoadLibrary",
                    "GetModuleHandle",
                    "CreateMutex",
                    "CreateMutexEx",
                    "OpenMutex",
                    "CreateNamedPipe",
                    "CreatePipe",
                    "CreateProcess",
                    "OpenProcess",
                    "GetCurrentProcess",
                    "CreateSemaphore",
                    "CreateSemaphoreEx",
                    "OpenSemaphore",
                    "socket",
                    "accept",
                    "CreateThread",
                    "CreateRemoteThread",
                    "GetCurrentThread",
                    "CreateWaitableTimer",
                    "CreateWaitableTimerEx",
                    "OpenWaitableTimer",
                    "BeginUpdateResource",
                    "GetProcessWindowStation",}

        local tbl_found2 = {"CloseHandle",
                    "FindCloseChangeNotification",
                    "CloseEventLog",
                    "DeleteFile",
                    "FindClose",
                    "HeapDestroy",
                    "FreeLibrary",
                    "TerminateProcess",
                    "closesocket",
                    "EndUpdateResource",
                    "DeregisterEventSource",}

        local tbl_result1 = {}
        local tbl_result2 = {}

for _, foundname in ipairs(input_table) do
    local f = assert(io.open(foundname, "rb"))
    local linenum = 0
    while true do
        local buffer = f:read("*l")
        linenum = linenum + 1
        if not buffer then
            break
        end

        for ie = 1, table.maxn(tbl_found1) do
            local start1, end1 = string.find(buffer, tbl_found1[ie])
            local start2, end2 = string.find(buffer, "//")
            if start1 then
                if start2 and start2 < start1 then
                else
                    print("----------------------------------")
                    print(buffer, "\nline: " .. linenum, "\nfile: " .. foundname)
                    print("")
                    if tbl_result1[tbl_found1[ie]] == nil then
                        tbl_result1[tbl_found1[ie]] = 1
                    else
                        tbl_result1[tbl_found1[ie]] = tbl_result1[tbl_found1[ie]] + 1
                    end
                end
            end
        end

        for i2 = 1, table.maxn(tbl_found2) do
            local start1, end1 = string.find(buffer, tbl_found2[i2])
            local start2, end2 = string.find(buffer, "//")
            if start1 then
                if start2 and start2 < start1 then
                else
                    print("----------------------------------")
                    print(buffer, "\nline: " .. linenum, "\nfile: " .. foundname)
                    print("")

                    if tbl_result2[tbl_found2[i2]] == nil then
                        tbl_result2[tbl_found2[i2]] = 1
                    else
                        tbl_result2[tbl_found2[i2]] = tbl_result2[tbl_found2[i2]] + 1
                    end
                end
            end
        end
    end
end

print("result table1 -------------------")
print_table(tbl_result1)

print("")
print("result table2 -------------------")
print_table(tbl_result2)

 

分享到:
评论
1 楼 ray_linn 2009-08-24  
1. dir *.cpp /s 或者 for /r . %%i (*.cpp) do echo %%i
2. for /r . %%i (*.cpp) do call findstr /F:token.lst %%i

相关推荐

    LuaForWindows_v5.1.4-45-last.exe Lua开发工具

    lua for windows其实是一整套Lua的开发环境.Lua for Windows 为 Windows 系统下提供了 Lua 脚本语言的开发和运行环境。Lua 是一个小巧的脚本语言。作者是巴西人。该语言的设计目的是为了嵌入应用程序中,从而为应用...

    luape:一个简单的Windows可执行文件,使用Lua进行了乳癌处理以创建可移植脚本

    一个简单的Windows可执行文件,可以用Lua进行科学怪人来创建可移植的脚本。 它是如何工作的? 您将有2个文件: fuser.exe和luastub.bin 。 luastub.bin是一个简单的Windows可执行文件,带有Lua运行时静态链接到它。...

    LuaBind 源码 (Lua增强库)

    把生成的DLL和lua.exe/lua51.dll放在同一个目录下. Lua 5.1.2 Copyright (C) 1994-2007 Lua.org, PUC-Rio &gt; require "luabind_test" &gt; greet() Hello world! &gt; 6 作用域 注册到Lua里面的所有东西要不注册于一个名...

    vlc-prev-next:Vlc Player扩展程序,可播放同一目录中的下一个和上一个文件

    该扩展名将查找当前文件所在的目录,并且如果找到其他媒体文件,则会将上一个和下一个文件添加到播放列表中。 使用文件名按字母顺序选择文件。 类似于Media Player经典家庭影院的功能。安装在Vlc 2.2.2、3.0.3 ...

    Windows下的文本编辑器-flexedit

    FlexEdit Windows下的文本/十六进制编辑器,为编程人员提供方便强大的文本/十六进制编辑器。FlexEdit为公益软件,免费使用,没有任何功能限制。 基于Scitilla的强大的编辑功能 能够高亮显示的语言列表有:ada, asm,...

    MySQLOO:MySQLOO

    笔记如果不确定服务器的操作系统和体系结构,请在服务器的控制台中键入lua_run print(jit.os, jit.arch)进行查找。 输出将类似于Windows x86 (x86是32位,x64是64位)。 如果您的服务器使用的是Windows,则需要...

    LAN-XI-Open-API-Tools:用于使用LAN-XI OpenAPI协议的工具

    Wireshark解剖器wirehark / openapi.lua文件是用于LAN-XI支持的B&K OpenAPI流协议的Wireshark剖析器。安装将openapi.lua复制到Wireshark插件文件夹。 plugins文件夹的位置可能是: Windows:C:\ Users \(用户)\ ...

    自己写的dll的简介

    可执行文件的运行速度:前者快(直接在EXE模块的内存中查找符号),后者慢(需要在DLL模块的内存中查找,在另一个模块的内存中查找自然较慢) 可共享性:前者不可共享,也就是说如果两个EXE使用了同一个静态库,那么...

    TraktForVLC:自动trakt.tv您在VLC上观看的内容

    TraktForVLC 2.x通过结合使用lua VLC模块和Python帮助程序脚本来工作,以便在您正在观看的媒体上查找信息。 与TraktForVLC的早期版本相反,VLC模块允许直接绑定媒体活动(播放,暂停,停止),从而对帐户立即采取...

    mpv.net::film_frames:mpv.net是适用于Windows的现代媒体播放器,其作用类似于mpv

    mpv.net是基于流行的播放器的Windows现代媒体播放器。 图形用户界面 具有可自定义颜色主题的现代GUI。 命令行界面 mpv.net支持基于mpvs属性的命令行开关。 高品质视频输出 视频输出具有发烧友喜欢的许多功能,例如...

    Bounce:运行NodeMCU的Esp8266可视编程系统

    观看此视频以获取有关它的介绍: 要将其与esp8266 / NodeMCU配合使用,请执行以下操作: 首先,您需要使用串行/ USB驱动程序将esp连接到Windows-连接ESP8266,Windows会提示您这样做。 然后使用NodeMCUFlasher软件...

Global site tag (gtag.js) - Google Analytics