意外的右括号在批处理文件匹配的开始 [英] Unexpected closing parentheses in batch file with matching start
问题描述
我有,目前它所做的尝试,最多可连接到WiFi网络的10倍。一个简单的批处理文件
I have a simple batch file that currently all it does it tries to connect to a wifi network up to 10 times.
我的问题是,我得到我的命令提示符下一个意外的输出,当它跑了。
My trouble is that I am getting an unexpected output in my command prompt when it is ran.
下面是我的批处理脚本
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%"
我相信每一个打开的括号有一个合作伙伴右括号,但我的输出,否则说。
I believe every opening parentheses has a partner closing parentheses, but my output says otherwise.
下面是我的输出(我已禁用@ECHO OFF,看看实际启动错误(O)代表实际输出):
Here is my output (I've disabled @ECHO OFF to see where the error actually starts. (o) stands for actual output):
>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)
> ) || (
然后脚本没有别的结束。
Then the script ends without anything else.
推荐答案
您:
的意见是不是真正的意见。他们的标签。但标签不带括号的code块中的归属。使用 REM
来代替,这应该摆脱你的),在这个时候意外
的问题。
Your ::
comments aren't really comments. They're labels. But labels don't belong inside parenthetical code blocks. Use rem
instead, and that should get rid of your ) was unexpected at this time
problem.
您也可以考虑冷凝以为/ L
循环脚本 - 这样的事情,例如:
You might also consider condensing your script with a for /L
loop -- something like this, for example:
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
)
)
)
这篇关于意外的右括号在批处理文件匹配的开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!