的同时,每次调用都将 counts 置为 0 呢?

可以将 counts 作为参数传递给 findFilePath 函数,每次调用时都传入初始值 0。修改后的函数代码如下:

function findFilePath(path, compare, count, counts) counts = counts or 0 -- 如果未传入 counts,则将其默认为 0 if counts > count then return nil end

local rd = ms.os.read_dir(path)
local data
if rd or next(rd) then
    for index, fileInfo in pairs(rd) do
        local destTarget = path .. "/" .. fileInfo.name
        -- print("destTarget : ".. destTarget)
        if fileInfo.is_dir then
            counts = counts + 1
            data = findFilePath(destTarget, compare, count, counts)
            if data ~= nil then
                return data
            end
        elseif fileInfo.is_dir == false then
            if ms.strings.contains(destTarget, compare) then
                if utils.fileExist(destTarget) == true then
                    print("destTarget : " .. destTarget)
                    data = destTarget
                    break
                end
            end
        end
    end
    if data ~= nil then
        return data
    end
end

end

这样,每次调用 findFilePath 时,都会将 counts 置为 0,从而避免了计数器未重置的问题。

local counts = 0function findFilePathpathcomparecountcounts if counts count then return nil end local rd = msosread_dirpath local data if rd or nextrd then for index file

原文地址: https://www.cveoy.top/t/topic/bFPi 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录