我是批处理文件编写的新手,并且正在编写一个脚本,该脚本会随机打开三个网页之一并在延迟后循环。我在运行它时经常遇到语法错误,但是我无法查明它在哪里。
: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
Run Code Online (Sandbox Code Playgroud)
帮助将不胜感激,并在此先感谢!
@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
Run Code Online (Sandbox Code Playgroud)
以下是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
Run Code Online (Sandbox Code Playgroud)
有关代码的说明,请参见使用Windows批处理文件的单行多命令set "location=..." & goto OpenSite。
这个额外的信息是给rudicangiotti的,因为他在Middas的回答下面发表了评论。
这是没有必要的if,并else只用一个命令就可以使用括号。
因此,代码块
if "%num%"=="0" (
set "location=yahoo.com"
) else if "%num%"=="1" (
set "location=msn.com"
) else (
set "location=google.com"
)
Run Code Online (Sandbox Code Playgroud)
被解析像
if "%num%"=="0" (
set "location=yahoo.com"
) else (
if "%num%"=="1" (
set "location=msn.com"
) else (
set "location=google.com"
)
)
Run Code Online (Sandbox Code Playgroud)
也许这对于C / C ++来说更容易理解,它不需要else与命令位于同一行if或属于匹配项的右括号if。
完整的可编译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;
}
Run Code Online (Sandbox Code Playgroud)
同样在C / C ++中,没有else if语句的ElseIf关键字作为Visual Basic中的关键字或#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";
}
}
Run Code Online (Sandbox Code Playgroud)
可以使用C / C ++编写此代码块,并且可以使用很多其他变体来考虑方括号和缩进的不同样式。
对于批处理文件编码而言,真正令人感兴趣的是变体1到3,其中,变体2显示了如果在批处理文件中可能的话,变体1的外观将是什么样子。但是没有人用方括号else if3所示的方括号和缩进块,而是在else if最后插入了多个块if,else因此将其定位在最右边。
| 归档时间: |
|
| 查看次数: |
3165 次 |
| 最近记录: |