如果批处理文件中的ELSE语法错误?

时间:2014-08-24 04:18:20

标签: batch-file

我是批处理文件写作的新手,我正在编写一个脚本,该脚本会在延迟后随机打开三个网页中的一个并循环播放。我在运行时经常出现语法错误,但我无法确定它的位置。

: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

非常感谢帮助,并提前感谢!

2 个答案:

答案 0 :(得分:4)

根据MC NDMagoo的好建议,以下是代码:

@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撰写的答案。

ifelse只需一个命令即可使用括号。

因此代码块

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 ifif插入的更多else块一样,这些块将被定位到右到右。

答案 1 :(得分:0)

else放在与if相同的行上。例如:

if 'true'=='true' (
    echo true
) else (
    echo false
)