Batch file error: "cant be processed syntactically at this point"
Batch file error: "cant be processed syntactically at this point"
我的批处理文件程序总是在同一点崩溃。在它崩溃之前总是发生的事情是这样的:
ping -n 6 127.0.0.1 1>nul: 2>nul:
-n cant be processed syntactically at this point.
if ping -n 1 127.0.0.1|find "(0" >nul && goto Loop
然后 window 关闭。
有人可以帮我解决这个问题吗?
这是整个脚本:
@setlocal enableextensions enabledelayedexpansion
:Loop
set ipaddr=127.0.0.1
ping -n 6 %ipaddr% >nul: 2>nul:
if ping -n 1 %ipaddr%|find "(0%" >nul && goto Loop
echo Connection lost
start "" http://www.google.com
else if ping -n 1 %ipaddr%|find "(100%" >nul && goto Loop
echo Connection OK
taskkill /FI "WINDOWTITLE eq google*" goto Loop
endlocal
这是一个高级版本,其中包含两个代码块,这些代码块的执行取决于连接是正常还是丢失。
@setlocal enableextensions enabledelayedexpansion
set ipaddr=127.0.0.1
:Loop
ping -n 6 %ipaddr% >nul: 2>nul:
ping -n 1 %ipaddr%|find "(0%" >nul && (
echo Connection OK
taskkill /FI "WINDOWTITLE eq google*"
) || (
echo Connection lost
tasklist /v /fi "Windowtitle eq google*" || start "" http://www.google.com
)
goto :Loop
这是带有 %errorlevel% 和 if
- else
的 "conventional way"(相同的逻辑,以上只是一种较短的实现方式):
@setlocal enableextensions enabledelayedexpansion
set ipaddr=127.0.0.1
:Loop
ping -n 6 %ipaddr% >nul: 2>nul:
ping -n 1 %ipaddr%|find "(0%" >nul
if %errorlevel%==0 (
echo Connection OK
taskkill /FI "WINDOWTITLE eq google*"
) else (
echo Connection lost
tasklist /v /fi "Windowtitle eq google*" || start "" http://www.google.com
)
goto :Loop
我 set ipaddr...
脱离了循环。无需一次又一次。
(只是想知道 Google 是否可以访问,如果连接丢失...)
编辑以反映。有点增强,所以只有在连接状态发生变化时才会发生任何动作。如果不需要,请删除 "log" 函数,如果需要,也可以将它们重定向到文件。
@echo off
setlocal enabledelayedexpansion
set ipaddr=127.0.0.1
set "status=undefined"
:Loop
ping -n 2 %ipaddr% >nul: 2>nul:
ping -n 1 %ipaddr%|find "(0%" >nul && (
set oldstatus=!status!
set status=online
if !status! neq !oldstatus! (
echo %date% %time% Connection switched from !oldstatus! to !status!
taskkill /FI "WINDOWTITLE eq google*" >nul 2>&1
)
) || (
set oldstatus=!status!
set status=offline
if !status! neq !oldstatus! (
echo %date% %time% Connection switched from !oldstatus! to !status!
start "" http://www.google.com
)
)
goto :Loop
我的批处理文件程序总是在同一点崩溃。在它崩溃之前总是发生的事情是这样的:
ping -n 6 127.0.0.1 1>nul: 2>nul:
-n cant be processed syntactically at this point.
if ping -n 1 127.0.0.1|find "(0" >nul && goto Loop
然后 window 关闭。
有人可以帮我解决这个问题吗? 这是整个脚本:
@setlocal enableextensions enabledelayedexpansion
:Loop
set ipaddr=127.0.0.1
ping -n 6 %ipaddr% >nul: 2>nul:
if ping -n 1 %ipaddr%|find "(0%" >nul && goto Loop
echo Connection lost
start "" http://www.google.com
else if ping -n 1 %ipaddr%|find "(100%" >nul && goto Loop
echo Connection OK
taskkill /FI "WINDOWTITLE eq google*" goto Loop
endlocal
这是一个高级版本,其中包含两个代码块,这些代码块的执行取决于连接是正常还是丢失。
@setlocal enableextensions enabledelayedexpansion
set ipaddr=127.0.0.1
:Loop
ping -n 6 %ipaddr% >nul: 2>nul:
ping -n 1 %ipaddr%|find "(0%" >nul && (
echo Connection OK
taskkill /FI "WINDOWTITLE eq google*"
) || (
echo Connection lost
tasklist /v /fi "Windowtitle eq google*" || start "" http://www.google.com
)
goto :Loop
这是带有 %errorlevel% 和 if
- else
的 "conventional way"(相同的逻辑,以上只是一种较短的实现方式):
@setlocal enableextensions enabledelayedexpansion
set ipaddr=127.0.0.1
:Loop
ping -n 6 %ipaddr% >nul: 2>nul:
ping -n 1 %ipaddr%|find "(0%" >nul
if %errorlevel%==0 (
echo Connection OK
taskkill /FI "WINDOWTITLE eq google*"
) else (
echo Connection lost
tasklist /v /fi "Windowtitle eq google*" || start "" http://www.google.com
)
goto :Loop
我 set ipaddr...
脱离了循环。无需一次又一次。
(只是想知道 Google 是否可以访问,如果连接丢失...)
编辑以反映
@echo off
setlocal enabledelayedexpansion
set ipaddr=127.0.0.1
set "status=undefined"
:Loop
ping -n 2 %ipaddr% >nul: 2>nul:
ping -n 1 %ipaddr%|find "(0%" >nul && (
set oldstatus=!status!
set status=online
if !status! neq !oldstatus! (
echo %date% %time% Connection switched from !oldstatus! to !status!
taskkill /FI "WINDOWTITLE eq google*" >nul 2>&1
)
) || (
set oldstatus=!status!
set status=offline
if !status! neq !oldstatus! (
echo %date% %time% Connection switched from !oldstatus! to !status!
start "" http://www.google.com
)
)
goto :Loop