我的批处理脚本:-
@echo off
:alpha
rasdial "connection_name" connection_id connection_pass> tmpFile
set /p myvar= < tmpFile
del tmpFile
echo %myvar%
if "%myvar%" == "You are already connected to Broadband Connection." (exit)
goto alpha
该脚本应运行命令radial ...
,然后将命令的输出存储在临时文件tempFile
中。然后,它将在该文件中存储的命令radial
的输出分配给变量,并删除该文件。变量又用于检查命令是否成功执行(如果命令成功执行,则将输出You are already connected to Broadband Connection.
)。
但是执行此批处理文件后,我得到输出:-
Connecting to Broadband Connection...
The syntax of the command is incorrect.
,然后脚本中断。无需再次遍历它,直到成功执行get命令为止。
打开回声后的输出:-
rasdial "Broadband Connection" uname pass 1>tmpFile
set /p myvar= 0<tmpFile
del tmpFile
echo You are already connected to Broadband Connection.
You are already connected to Broadband Connection.
The syntax of the command is incorrect.
if "You are already connected to Broadband Connection.
Command completed successfully." == "You are already connected to Broadband Connection." (exit)
答案 0 :(得分:3)
变量myvar
似乎包含换行符(是的,这确实很意外),破坏了if语法。
您应该切换到另一种方法(由@Marged建议)
:alpha
rasdial "Broadband Connection" uname pass | find "You are already connected to Broadband Connection." && exit /b
timeout 1 >nul
goto :alpha
这会将rasdial
的输出直接传递到find
命令。 &&
的工作方式为“如果find
成功”(找到字符串),然后执行exit
命令。
提示:在没有空闲时间的情况下(请在此处使用timeout
来建立无限循环),以防止CPU以100%的速度运行。