我是批处理文件写作的新手,我正在编写一个脚本,该脚本会在延迟后随机打开三个网页中的一个并循环播放。我在运行时经常出现语法错误,但我无法确定它的位置。
:main
@echo on
set location=""
set /A num=%random% %% 10
if /A"%num%"=="0"
(
set location="yahoo.com"
)
else if /A"%num%"=="1"
(
set location="msn.com"
)
else
(
set location="google.com"
)
start "Chrome" chrome --new-window %location%
timeout /t 30 /nobreak >NUL
goto main
非常感谢帮助,并提前感谢!
答案 0 :(得分:4)
@echo off
:main
set location=
set /A num=%random% %% 10
if "%num%"=="0" (
set "location=yahoo.com"
) else if "%num%"=="1" (
set "location=msn.com"
) else (
set "location=google.com"
)
start "Chrome" chrome.exe --new-window "%location%"
timeout /t 30 /nobreak >NUL
goto main
以下是遵循Ed Heal建议的批处理代码:
@echo off
:main
set location=
set /A num=%random% %% 10
if "%num%"=="0" set "location=yahoo.com" & goto OpenSite
if "%num%"=="1" set "location=msn.com" & goto OpenSite
set "location=google.com"
:OpenSite
start "Chrome" chrome.exe --new-window "%location%"
timeout /t 30 /nobreak >NUL
goto main
有关代码set "location=..." & goto OpenSite
的解释,请参阅Single line with multiple commands using Windows batch file。
此额外信息适用于rudicangiotti,因为他的评论低于Middas撰写的答案。
if
和else
只需一个命令即可使用括号。
因此代码块
if "%num%"=="0" (
set "location=yahoo.com"
) else if "%num%"=="1" (
set "location=msn.com"
) else (
set "location=google.com"
)
被解析为
if "%num%"=="0" (
set "location=yahoo.com"
) else (
if "%num%"=="1" (
set "location=msn.com"
) else (
set "location=google.com"
)
)
使用C / C ++可能更容易理解,这不需要else
与命令if
位于同一行或属于匹配if
的右括号。< / p>
完整的可编译C / C ++示例代码:
#ifdef __cplusplus
#include <cstdio> /* printf */
#include <cstdlib> /* rand */
#include <cstring> /* strcmp */
#else
#include <stdio.h> /* printf */
#include <stdlib.h> /* rand */
#include <string.h> /* strcmp */
#endif
int main (int argc, char* argv[])
{
const char* sLocation;
int iNum = rand() % 10;
if(iNum == 0) sLocation = "yahoo.com";
else if(iNum == 1) sLocation = "msn.com";
else sLocation = "google.com";
printf("Number is %d and location is \"%s\".\n",iNum,sLocation);
/* Some not really useful code to avoid warnings. */
if(argc > 1)
{
if(!strcmp(argv[1],"/?"))
{
printf("There is no help for this small demo application.\n");
}
}
return 0;
}
同样在C / C ++中,else if
语句没有关键字作为Visual Basic中的ElseIf
关键字或预处理器的#elif
指令。
因此,上述条件块也可以写成:
/* Variant 1: Same usage of brackets and indents like in first batch example. */
if(iNum == 0) {
sLocation = "yahoo.com";
} else if(iNum == 1) {
sLocation = "msn.com";
} else {
sLocation = "google.com";
}
/* Variant 2: Same usage of brackets like in first batch example,
but this time with indents as it would be 100% correct according
to processing. It is not possible to use this syntax in batch
files because the second if must be on same line as first else. */
if(iNum == 0) {
sLocation = "yahoo.com";
} else
if(iNum == 1) {
sLocation = "msn.com";
} else {
sLocation = "google.com";
}
/* Variant 3: Same usage of brackets like in second batch example,
but without omitting not necessary brackets for first else block. */
if(iNum == 0) {
sLocation = "yahoo.com";
} else {
if(iNum == 1) {
sLocation = "msn.com";
} else {
sLocation = "google.com";
}
}
/* Variant 4: One more variant not possible in batch file,
but using a very common style for C/C++ programmers. */
if(iNum == 0)
{
sLocation = "yahoo.com";
}
else if(iNum == 1)
{
sLocation = "msn.com";
}
else
{
sLocation = "google.com";
}
/* Variant 5: This is variant 3 in coding style of variant 4. */
if(iNum == 0)
{
sLocation = "yahoo.com";
}
else
{
if(iNum == 1)
{
sLocation = "msn.com";
}
else
{
sLocation = "google.com";
}
}
这个代码块可以用C / C ++编写,有更多的变体考虑到括号和缩进的不同样式。
批处理文件编码真的很有意思的是变体1到3,其中变体2显示了变体1在批处理文件中是否真的如此。但是,如变体3中所示,没有任何括号和缩进else if
块与最后else if
和if
插入的更多else
块一样,这些块将被定位到右到右。
答案 1 :(得分:0)
将else
放在与if
相同的行上。例如:
if 'true'=='true' (
echo true
) else (
echo false
)