I\'m new to batch file writing and I\'m writing a script that randomly opens one of three web pages and loops after a delay. I constantly get a syntax error when I run it, b
Put the else
on the same line as the if
. For example:
if 'true'=='true' (
echo true
) else (
echo false
)
Here is code according to good advices of MC ND and Magoo:
@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
And here is the batch code following advice of 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
See Single line with multiple commands using Windows batch file for an explanation of code set "location=..." & goto OpenSite
.
This extra information is for rudicangiotti because of his comment below answer written by Middas.
It is not necessary for if
and else
with just a single command to use parentheses.
Therefore the code block
if "%num%"=="0" (
set "location=yahoo.com"
) else if "%num%"=="1" (
set "location=msn.com"
) else (
set "location=google.com"
)
is parsed like
if "%num%"=="0" (
set "location=yahoo.com"
) else (
if "%num%"=="1" (
set "location=msn.com"
) else (
set "location=google.com"
)
)
Perhaps this is easier to understand with C/C++ which does not require that else
is on same line as command if
or the closing parenthesis which belongs to the matching if
.
A complete compilable C/C++ example code:
#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;
}
Also in C/C++ there is no keyword for an else if
statement as the ElseIf
keyword in Visual Basic or #elif
directive of the preprocessor.
Therefore the above condition block could be written also as:
/* 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";
}
}
This code block could be written in C/C++ with a lot more variants taking the different styles for brackets and indents into account.
Really interesting for batch file coding are the variants 1 to 3 whereby variant 2 shows how variant 1 should really look like if it would be possible in batch files. But nobody brackets and indents else if
blocks as shown in variant 3 as with several more else if
blocks inserted the last if
and else
would be positioned thus far to right.