163

I have a question about if - else structure in a batch file. Each command runs individually, but I couldn't use "if - else" blocks safely so these parts of my programme doesn't work. How can I do make these parts run? Thank you.

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%"
    )
mavhc
  • 5
  • 3
eponymous
  • 2,090
  • 5
  • 23
  • 29
  • Is my question read clearly? I read it line by line but it seems to me side by side. – eponymous Jun 18 '12 at 11:24
  • 4
    Hi, Welcome to Stack Overflow! In order to create a code block you can highlight the relevant text and click the `{}` button; it helps to make posts more readable and avoids some problems with the mark-up. In answer to your question, it does now! When writing a question there's a preview below so you can see how it will look once posted. – Ben Jun 18 '12 at 11:24
  • 1
    So thank you for your explanation. I am going to use these method. – eponymous Jun 18 '12 at 11:32
  • if anyone want to put `multiple commands` in one `if` can also see this post: https://stackoverflow.com/questions/13692916/unable-to-echo-user-input-values-to-file-in-batch-script – yu yang Jian Apr 28 '21 at 12:29

8 Answers8

130

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

James Hill
  • 60,353
  • 20
  • 145
  • 161
  • 3
    Please correct me if I'm wrong, but I don't think you can use `&&` in multiple `if` conditions, see my alternative syntax? – Bali C Jun 18 '12 at 11:40
  • if my if conditions are like this: "IF %F%==0" run safely but when I added && it doesn't run. How can I use two conditions in an if? – eponymous Jun 18 '12 at 13:00
  • 1
    @aprogrammer, Look at my example. If you have multiple conditions, use `IF condition1 IF condition 2` – James Hill Jun 18 '12 at 13:21
  • I had tried your answer but I couldn't had run it but I tried again and modified to my code ant it run. Last situation of some part of my code is like this: 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 ) ) – eponymous Jun 18 '12 at 13:57
  • I want to explain my code; I added "else block" out of each if block except for first "if block". Thank you for your answer. – eponymous Jun 18 '12 at 13:59
  • 5
    What do you mean you can't you use `ELSE IF`? It works fine under Win7. See example: http://paste2.org/G8tMae92 – bryc Apr 30 '16 at 19:55
  • Not working. Here is simple example related to above, which is not working. @echo off Set p=%~1 if %p%==1 if %p%==2 if %p%==3 ( echo Among first 3 ) if %p%==4 if %p%==5 if %p%==6 if %p%==7 ( echo These are later ones ) echo Ended. – Farrukh Waheed Jun 16 '16 at 04:38
  • Good answer. One note - Using `::` comment syntax as opposed to `REM` syntax inside of loops can cause illogical errors that are difficult to debug - see [here](http://stackoverflow.com/a/12407934/1167442) – dgo Jan 04 '17 at 18:40
64

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
wjandrea
  • 28,235
  • 9
  • 60
  • 81
gtpunch
  • 641
  • 5
  • 3
13

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%"
    )
Bali C
  • 30,582
  • 35
  • 123
  • 152
  • 1
    `if else`, especially in C-like languages is just a nested `if` too. You don't have to nest them with blocks in batch files either. – Joey Jun 18 '12 at 11:31
  • @Joey I don't quite follow. I know the `else if` combo is just a nested `if` but I didn't think you could use those keywords together in batch like you can in C languages. – Bali C Jun 18 '12 at 11:35
  • Hmm I still can't get it to work, but I'll take your word for it, you know what you're talking about, I'll leave this answer as a long version :) – Bali C Jun 18 '12 at 11:50
  • Thank you for your answers but none of them did't run in my code. – eponymous Jun 18 '12 at 12:54
  • another example: if 1==1 (echo 1) else (if 1==1 (echo 2) else (echo 3)) – JohnP2 Mar 29 '18 at 20:21
7

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

Amado Saladino
  • 2,114
  • 23
  • 25
2

I believe you can use something such as

if ___ (

do this

) else if ___ (

do this

)
Ophir Yoktan
  • 8,149
  • 7
  • 58
  • 106
  • 32
    Shouldn't the statement in the second condition be "do that"? – bvj Nov 07 '14 at 01:08
  • 9
    @bvj No, no, you got it all wrong. It should be `do foo` and `do bar`. – yyny Apr 03 '16 at 19:41
  • When you write `IF (test) (command) ELSE IF (test) (command)`, you are implying `IF (test) (command) ELSE (IF (test) (command))`. This may work sometimes, but if you believe it is an actual programming structure acceptable in DOS then it's going to be a PITA to troubleshoot when it fails. – Tim Aug 08 '16 at 15:50
1

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)
Stefane
  • 11
  • 2
0

IF...ELSE IF constructs work very well in batch files, in particular when you use only one conditional expression on each IF line:

IF %F%==1 (
    ::copying the file c to d
    copy "%sourceFile%1" "%destinationFile1%"
) ELSE IF %F%==0 (
    ::moving the file e to f
    move "%sourceFile2%" "%destinationFile2%" )

In your example you use IF...AND...IF type construct, where 2 conditions must be met simultaneously. In this case you can still use IF...ELSE IF construct, but with extra parentheses to avoid uncertainty for the next ELSE condition:

IF %F%==1 (IF %C%==1 (
    ::copying the file c to d
    copy "%sourceFile1%" "%destinationFile1%" )
) ELSE IF %F%==1 (IF %C%==0 (
    ::moving the file e to f
    move "%sourceFile2%" "%destinationFile2%"))

The above construct is equivalent to:

IF %F%==1 (
    IF %C%==1 (
    ::copying the file c to d
    copy "%sourceFile1%" "%destinationFile1%"
    ) ELSE IF %C%==0 (
    ::moving the file e to f
    move "%sourceFile2%" "%destinationFile2%"))

Processing sequence of batch commands depends on CMD.exe parsing order. Just make sure your construct follows that logical order, and as a rule it will work. If your batch script is processed by Cmd.exe without errors, it means this is the correct (i.e. supported by your OS Cmd.exe version) construct, even if someone said otherwise.

Community
  • 1
  • 1
sambul35
  • 1,058
  • 14
  • 22
0

Here's my code Example for if..else..if
which do the following

Prompt user for Process Name

If the process name is invalid
Then it's write to user

Error : The Processor above doesn't seem to be exist 

if the process name is services
Then it's write to user

Error : You can't kill the Processor above 

if the process name is valid and not services
Then it's write to user

the process has been killed via taskill

so i called it Process killer.bat
Here's my Code:

@echo off

:Start
Rem preparing the batch  
cls
Title Processor Killer
Color 0B
Echo Type Processor name to kill It (Without ".exe")
set /p ProcessorTokill=%=%  

:tasklist
tasklist|find /i "%ProcessorTokill%.exe">nul & if errorlevel 1 (
REM check if the process name is invalid 
Cls 
Title %ProcessorTokill% Not Found
Color 0A
echo %ProcessorTokill%
echo Error : The Processor above doesn't seem to be exist    

) else if %ProcessorTokill%==services (
REM check if the process name is services and doesn't kill it
Cls 
Color 0c
Title Permission denied 
echo "%ProcessorTokill%.exe"
echo Error : You can't kill the Processor above 

) else (
REM if the process name is valid and not services
Cls 
Title %ProcessorTokill% Found
Color 0e
echo %ProcessorTokill% Found
ping localhost -n 2 -w 1000>nul
echo Killing %ProcessorTokill% ...
taskkill /f /im %ProcessorTokill%.exe /t>nul
echo %ProcessorTokill% Killed...
)

pause>nul



REM If else if Template
REM if thing1 (
REM Command here 2 ! 
REM ) else if thing2 (
REM command here 2 !
REM ) else (
REM command here 3 !
REM )
Oimar Daif
  • 13
  • 5