由于 assoc 无法正确管理 %ERRORLEVEL% ,因此我使用以下方法作为解决方法:

assoc .myext=myext >NUL 2>NUL
ftype myext="%~dp0mybatch.bat" "%%1" >NUL 2>NUL
assoc .myext | find "myext" >NUL 2>NUL
if %errorlevel% NEQ 0 (
    echo Association failed
    goto err
)
ftype otrkey | find "otrmenu" > NUL
if %errorlevel% NEQ 0 (
    echo Association failed
    goto err
)
exit /b 0
:err
exit /b 1

我想保持输出清洁,因为用户不需要来自assoc和ftype命令的实际输出,这就是为什么stdout和stderr被重定向到NUL . 该脚本运行正常,但该关联并不真正起作用 . 如果我尝试打开.myext文件,我会得到一个未知文件扩展的对话窗口 .

如果我从批处理中删除输出重定向,则关联起作用 . 如何解决这个问题?

update :准确地说,我可以让所有重定向都在哪里,但需要从第一行中删除两个重定向 - assoc .myext=myext - 才能使其正常工作 .