You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Lua 5.2中用os.execute调用CMD打开含emoji文件夹可行吗?

解决Lua脚本访问含Emoji路径的跨平台问题

平台适配方案(推荐)

Windows CMD对Unicode字符(包括Emoji)支持有限,但PowerShell完全支持;Unix类系统(Linux/macOS)的终端原生支持Emoji,因此可以通过判断系统调用对应命令:

Windows 平台

替换CMD为PowerShell执行打开操作:

local base_path = "你的基础路径"
local target_dir = base_path .. "\\scripts\\menu\\📄 My Scripts"
-- 用PowerShell打开目标文件夹
os.execute('powershell -Command "Start-Process \'' .. target_dir .. '\'"')

简化写法:

os.execute('powershell start "' .. target_dir .. '"')

Unix 类平台

直接使用系统原生打开命令:

local base_path = "你的基础路径"
local target_dir = base_path .. "/scripts/menu/📄 My Scripts"
local is_windows = package.config:sub(1,1) == "\\"

if not is_windows then
    local uname_handle = io.popen("uname -s")
    local sys_name = uname_handle:read("*a")
    uname_handle:close()
    
    if sys_name:match("Darwin") then
        -- macOS 使用open命令
        os.execute('open "' .. target_dir .. '"')
    else
        -- Linux 使用xdg-open命令
        os.execute('xdg-open "' .. target_dir .. '"')
    end
end

通配符匹配方案(备选)

已知Emoji是文件夹名称首字符,可使用*通配符替代Emoji部分,避免直接写入Emoji:

local base_path = "你的基础路径"
-- 用*替换首字符的Emoji
local wildcard_dir = base_path .. "\\scripts\\menu\\* My Scripts" -- Windows路径分隔符
-- 或Unix路径:base_path .. "/scripts/menu/* My Scripts"

-- Windows下用CMD打开(需确保无重名匹配项)
os.execute('start "" "' .. wildcard_dir .. '"')
-- Unix下对应替换为open/xdg-open命令

注意:需保证该目录下仅有一个以 My Scripts结尾的文件夹,否则会打开第一个匹配项。

不推荐的编码转换方案

尝试将UTF-8路径转换为Windows GBK编码需要依赖第三方库(如iconv),步骤繁琐且跨平台兼容性差,远不如前两种方案实用。

内容的提问来源于stack exchange,提问作者Rai

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 22:35:56