为什么这个lua脚本无法打开Windows子目录? [英] Why is this lua script unable to open a Windows subdirectory?

查看:144
本文介绍了为什么这个lua脚本无法打开Windows子目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图确定lua脚本中是否存在多个目录之一.它可以在OSX上运行,但不能在Windows上运行(Linux目前尚未经过测试,但是我希望它可以运行).当以下代码运行时,出现错误:

I'm trying to determine if one of several directories are present from within a lua script. It works on OSX, but not on Windows (linux is currently untested, but I expect that one to work). When the following code runs, I get an error:

与此C:\ Program Files(x86)\ VideoLAN \ VLC \ lua \ playlist \失败:没有这样的文件或目录

我可以确认该目录存在.我逃脱了斜线,我不确定还有什么问题.

I can confirm that that directory exists. I've escaped the slashes, I'm not sure what else could be the issue.

local oses = { "/Applications/VLC.app/Contents/MacOS/share/lua/playlist/"; "C:\\Program Files\\VideoLAN\\VLC\\lua\\playlist\\"; "C:\\Program Files (x86)\\VideoLAN\\VLC\\lua\\playlist\\"; "/usr/lib/vlc/lua/playlist" }

-- Determine which OS this is (and where to find share/lua).
local f,err = io.open( oses[1], "r")
if not err then
    opsys = "OSX"
    scriptpath = oses[1] .. script
    f:close()
else
    f,err = io.open( oses[2], "r")
    if not err then
        opsys = "Win32"
        scriptpath = oses[2] .. script
        f:close()
    else
        f,err = io.open( oses[3], "r")
        vlc.msg.dbg( dhead .. 'failed with this ' .. err .. dtail ) 
        if not err then
            opsys = "Win64"
            scriptpath = oses[3] .. script
            f:close()
        else
            f,err = io.open( oses[4], "r")
            if not err then
                opsys = "Linux/Unix"
                scriptpath = oses[4] .. script
                f:close()
            else
                return false
            end
        end
    end 
end

推荐答案

文件"C:\ Program Files \ VideoLAN \ VLC \ lua \ playlist \"不存在.如果要删除结尾的斜杠,则将尝试打开目录,并且可能会出现权限错误.无论哪种方式都行不通.如果要使用这种确定操作系统的方法,则应尝试打开文件.

The file "C:\Program Files\VideoLAN\VLC\lua\playlist\" does not exist. If you were to remove the trailing slash, you'd be trying to open a directory and probably get a permissions error. It's not going to work either way. If you're going to use this method of determining OS, you should be trying to open files.

例如,构建您的脚本路径,尝试打开该文件,然后使用 that 确定通过/失败.

For instance, build your script path, try to open that file, and use that to determine pass/fail.

请注意,您的代码结构可能会大大改善.每当您有一堆重复的代码因索引而异时,都应该使用循环.例如,我们可以用以下代码替换您的代码:

Side note, the structure of your code could be vastly improved. Any time you have a bunch of duplicate code that differs by an index, you should be using a loop. For instance, we can replace your code with this:

local oses = {
    ["OSX"]        = "/Applications/VLC.app/Contents/MacOS/share/lua/playlist/",
    ["Win32"]      = "C:\\Program Files\\VideoLAN\\VLC\\lua\\playlist\\",
    ["Win64"]      = "C:\\Program Files (x86)\\VideoLAN\\VLC\\lua\\playlist\\",
    ["Linux/Unix"] = "/usr/lib/vlc/lua/playlist",
}
for osname, directory in pairs(oses) do
    local scriptpath = directory..script
    local f,err = io.open( scriptpath, "r")
    if not err then
        f:close()
        return scriptpath, osname
    end
end

这篇关于为什么这个lua脚本无法打开Windows子目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆