However, you can't use else if in batch scripting. Instead, simply add a series of if statements: if %x%==5 if %y%==5 (echo "Both x and y equal 5.")
Checking Command Line ArgumentsAnother common use of the 'if' statement is used to check for the values of the command line arguments which are passed to the batch files. The following example shows how the 'if' statement can be used to check for the values of the command line arguments.
Use if to specify a block of code to be executed, if a specified condition is true. Use else to specify a block of code to be executed, if the same condition is false. Use else if to specify a new condition to test, if the first condition is false. Use switch to specify many alternative blocks of code to be executed.
Your syntax is incorrect. You can't use ELSE IF
. It appears that you don't really need it anyway. Simply use multiple IF
statements:
IF %F%==1 IF %C%==1 (
::copying the file c to d
copy "%sourceFile%" "%destinationFile%"
)
IF %F%==1 IF %C%==0 (
::moving the file c to d
move "%sourceFile%" "%destinationFile%"
)
IF %F%==0 IF %C%==1 (
::copying a directory c from d, /s: boş olanlar hariç, /e:boş olanlar dahil
xcopy "%sourceCopyDirectory%" "%destinationCopyDirectory%" /s/e
)
IF %F%==0 IF %C%==0 (
::moving a directory
xcopy /E "%sourceMoveDirectory%" "%destinationMoveDirectory%"
rd /s /q "%sourceMoveDirectory%"
)
Great batch file reference: http://ss64.com/nt/if.html
I think in the question and in some of the answers there is a bit of confusion about the meaning of this pseudocode in DOS: IF A IF B X ELSE Y. It does not mean IF(A and B) THEN X ELSE Y, but in fact means IF A( IF B THEN X ELSE Y). If the test of A fails, then he whole of the inner if-else will be ignored.
As one of the answers mentioned, in this case only one of the tests can succeed so the 'else' is not needed, but of course that only works in this example, it isn't a general solution for doing if-else.
There are lots of ways around this. Here is a few ideas, all are quite ugly but hey, this is (or at least was) DOS!
@echo off
set one=1
set two=2
REM Example 1
IF %one%_%two%==1_1 (
echo Example 1 fails
) ELSE IF %one%_%two%==1_2 (
echo Example 1 works correctly
) ELSE (
echo Example 1 fails
)
REM Example 2
set test1result=0
set test2result=0
if %one%==1 if %two%==1 set test1result=1
if %one%==1 if %two%==2 set test2result=1
IF %test1result%==1 (
echo Example 2 fails
) ELSE IF %test2result%==1 (
echo Example 2 works correctly
) ELSE (
echo Example 2 fails
)
REM Example 3
if %one%==1 if %two%==1 (
echo Example 3 fails
goto :endoftests
)
if %one%==1 if %two%==2 (
echo Example 3 works correctly
goto :endoftests
)
echo Example 3 fails
)
:endoftests
AFAIK you can't do an if else
in batch like you can in other languages, it has to be nested if
's.
Using nested if
's your batch would look like
IF %F%==1 IF %C%==1(
::copying the file c to d
copy "%sourceFile%" "%destinationFile%"
) ELSE (
IF %F%==1 IF %C%==0(
::moving the file c to d
move "%sourceFile%" "%destinationFile%"
) ELSE (
IF %F%==0 IF %C%==1(
::copying a directory c from d, /s: boş olanlar hariç, /e:boş olanlar dahil
xcopy "%sourceCopyDirectory%" "%destinationCopyDirectory%" /s/e
) ELSE (
IF %F%==0 IF %C%==0(
::moving a directory
xcopy /E "%sourceMoveDirectory%" "%destinationMoveDirectory%"
rd /s /q "%sourceMoveDirectory%"
)
)
)
)
or as James suggested, chain your if
's, however I think the proper syntax is
IF %F%==1 IF %C%==1(
::copying the file c to d
copy "%sourceFile%" "%destinationFile%"
)
here is how I handled if else if situation
if %env%==dev (
echo "dev env selected selected"
) else (
if %env%==prod (
echo "prod env selected"
)
)
Note it is not the same as if-elseif block as the other programming languages like C++ or Java but it will do what you need to do
I believe you can use something such as
if ___ (
do this
) else if ___ (
do this
)
A little bit late and perhaps still good for complex if-conditions, because I would like to add a "done" parameter to keep a if-then-else structure:
set done=0
if %F%==1 if %C%==0 (set done=1 & echo found F=1 and C=0: %F% + %C%)
if %F%==2 if %C%==0 (set done=1 & echo found F=2 and C=0: %F% + %C%)
if %F%==3 if %C%==0 (set done=1 & echo found F=3 and C=0: %F% + %C%)
if %done%==0 (echo do something)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With