批处理脚本禁用互联网 - 尽管我没有要求它这样做,但系统却显示“系统找不到指定的驱动器”

批处理脚本禁用互联网 - 尽管我没有要求它这样做,但系统却显示“系统找不到指定的驱动器”

我有一个批处理脚本(通过右键单击“发送到”菜单将其作为 .cmd 文件运行),该脚本首先禁用用户的 Internet 适配器,然后安全地解压缩 .zip 存档。

对于大多数人来说,它运行良好,但我有几个用户收到“系统找不到指定的驱动器”错误,但其中没有任何内容正在寻找驱动器。

netsh 接口设置接口名称=“本地连接”admin=disabled

if "%errorlevel%" == "0" (

    echo Network Interface disabled
    ::netsh interface set interface name="Wireless Network Connection" admin=disabled
    ::if "%errorlevel%" == "1" (
    ::echo Network Interface still active, exiting
    ::pause
    ::exit \b
    ::)
    )   ELSE (

    echo Network Interface still active
    pause
    exit \b
    )

如果用户只有一个活动的 Internet 连接,则有一些注释掉的部分。这两个用户得到的输出是:

Network Interface disabled
The system cannot find the drive specified.
The system cannot find the drive specified.
The system cannot find the drive specified.
Network Interface still active

因此,即使撇开我没有寻找驱动器的事实,它也不应该能够同时回显“网络接口已禁用”和“网络接口仍处于活动状态”,对吗?


编辑:我很确定这是因为我在 IF 块内有 :: 注释。显然,这对于 Windows 批处理脚本来说并不好。

http://www.robvanderwoude.com/comments.php

答案1

您收到这些错误是因为 IF ELSE 结构使用不正确。在批处理脚本中,使用带括号的 IF ELSE 不起作用,但在大多数编程语言中却可以。

ELSE 子句必须与 IF 后的命令位于同一行。要么将其移至同一行,要么使用 GOTO +labels。

供参考:

答案2

也许是带反斜杠的“exit \b”?我想你的意思是“exit /b”。(谁知道呢,也许它将“\b”解释为路径或类似的东西。)

我有时也使用“::”作为注释字符。我不认为这是你的问题。

相关内容