具有匹配开始的批处理文件中的意外右括号

Unexpected closing parentheses in batch file with matching start

我有一个简单的批处理文件,目前它尝试连接到 wifi 网络最多 10 次。

我的麻烦是当它是 运行 时,我在命令提示符中得到了意外的输出。

这是我的批处理脚本

setlocal EnableExtensions EnableDelayedExpansion
echo.
echo.

:startSetVariables
set timeout="2"
set PINGS="4"
set pingtimeout="1000"
set endonfail="10"
set wifissid="ROGAR Production"
set /A COUNTER=1

:connectToTheInternet
echo checking internet connection
echo.
:connectToTheInternetRestart
ping -n 1 host | find /i "TTL=" >NUL && (
    echo Internet connection already established, skipping to activation!
    echo.
    goto endNoShutdown
    ::toupd
) || ( 
    ::if failed endonfail times, give up
    if "%COUNTER%"=="%endonfail%" (
        echo Error: Could not connect to network %endonfail% times, stopping script.
        echo.
        goto endNoShutdown
    )

    ::raise waiting time between connections        
    if "%COUNTER%"=="3" (
        echo failed 2 times in a row. Increasing wait time between actions to 4 seconds.
        echo.
        set timeout=4
        set /A COUNTER=COUNTER+1
    ) else if "%COUNTER%"=="5" (
        echo failed 4 times in a row. Increasing wait time between actions to 6 seconds.
        echo.
        set timeout=6
        set /A COUNTER=COUNTER+1
    ) else if "%COUNTER%"=="7" (
        echo failed 6 times in a row. Increasing wait time between actions to 8 seconds.
        echo.
        set timeout=8
        set /A COUNTER=COUNTER+1
    ) else if "%COUNTER%"=="9" (
        echo failed 8 times in a row. Increasing wait time between actions to 10 seconds.
        echo.
        set timeout=10
        set /A COUNTER=COUNTER+1
    ) else (
        echo Wait time is currently %timeout% seconds.
        set /A COUNTER=COUNTER+1
    )

    echo Attempt #%COUNTER%.

    ::disconnect existing network
    netsh wlan disconnect
    netsh wlan delete profile name="%wifissid%"

    timeout /t 1 /nobreak

    echo.
    ::attempt connection
    netsh wlan add profile filename="connection.xml"
    netsh wlan connect name="%wifissid%" ssid="%wifissid%"

    timeout /t %timeout% /nobreak

    ::check connection
    set internet=false
    Ping google.com -n 1 -w %pingtimeout%
    if errorlevel 1 (
        set internet=Failed
    ) else (
        set internet=Connected
    )

    ::check pings
    ping -n 1 host | find /i "TTL=" >NUL && (
        echo Successfully connected! Starting activation check.
        echo.
        goto endNoShutdown
        ::toupd
    ) || ( 
        echo Connection attempt failed. Starting attempt #%COUNTER% in 3 seconds.
        echo.
        timeout /t 3 /nobreak
        cls
        goto connectToTheInternetRestart
    )
)


:endShutdown
echo Releasing wifi, shutting down computer.
netsh wlan delete profile name="%wifissid%"
pause
shutdown /s

:endNoShutdown
echo Releasing wifi, ending without shutdown.
netsh wlan delete profile name="%wifissid%"

我相信每个左括号都有一个伙伴的右括号,但我的输出却另有说法。

这是我的输出(我已禁用 @ECHO OFF 以查看错误实际开始的位置。(o) 代表实际输出):

>set /A COUNTER=1
>echo checking internet connection
checking internet connection (o)

>echo.

Ping google.com -n 1 -w 1000
Ping request could not find host google.com. Please check the name and try again. (o - should return errorlevel 1, meaning the following should resolve to else)
>echo Internet access: Failed
Internet access: Failed (o)

>echo.

) was unexpected at this time. (o)

>     ) || (

然后脚本就结束了。

您的 :: 评论并不是真正的评论。他们是标签。但是标签不属于带括号的代码块。请改用 rem,这样应该可以解决您的 ) was unexpected at this time 问题。

您还可以考虑使用 for /L 循环来压缩您的脚本——类似这样,例如:

for /L %%I in (1,1,10) do (
    ping -n 1 www.google.com | find /i "TTL=" >NUL && (

        echo Successfully connected.
        goto :EOF

    ) || (

        set /a delay = %%I - 1
        echo Starting attempt %%I in !delay! seconds.

        >NUL 2>NUL (
            rem // disconnecting first...
            netsh wlan disconnect
            netsh wlan delete profile name="%wifissid%"

            timeout /t 1 /nobreak

            rem // attempting reconnection...
            netsh wlan add profile filename="connection.xml"
            netsh wlan connect name="%wifissid%" ssid="%wifissid%"

            timeout /t !delay! /nobreak
        )
    )
)