Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Batch How to handle multiple IF conditions  (Read 17421 times)

0 Members and 1 Guest are viewing this topic.

Yogesh123

    Topic Starter


    Beginner

    Batch How to handle multiple IF conditions
    « on: September 24, 2009, 08:08:14 AM »
    How to handle multiple IF conditions in a batch scripting
    Ex:-
    IF %Ti% EQU 2 (                       first if start
    do this
    do this
    do this
    do this
    IF "%clc%" GEQ "800" set /a yy=%yy%+1
    IF "%Day%" GTR "1" set /a yy=%yy%+1
    IF %yy% LEQ 2 (
    taskkill /F /IM calc.exe
    ) ELSE (
    taskkill /F /IM notepad.exe
        )

    ) ELSE (                                      first if end
    echo Bye Bye
        )


    Thanks in advance

    Salmon Trout

    • Guest
    Re: Batch How to handle multiple IF conditions
    « Reply #1 on: September 24, 2009, 10:43:38 AM »
    We do not know what your "do this" code does so cannot give detailed help. However, in a multi line IF structure, you need to use delayed expansion to set and read variables to work properly. You can Google for many pages about this.


    Yogesh123

      Topic Starter


      Beginner

      Re: Batch How to handle multiple IF conditions
      « Reply #2 on: September 25, 2009, 12:25:14 AM »
      Code: [Select]
      [quote author=Salmon Trout link=topic=92501.msg625699#msg625699 date=1253810618]
      We do not know what your "do this" code does so cannot give detailed help. However, in a multi line IF structure, you need to use delayed expansion to set and read variables to work properly. You can Google for many pages about this.


      [/quote]
      Dear Salmon Trout,
      Pls find the code,
      How to handle multiple IF conditions in a batch scripting
      Ex:-

      For /f "tokens=5" %%i in (6.txt) do set ci=%%i
      IF %ci% EQU 2 ( [b] first if start[/b]
      For /f "tokens=3" %%i in (6.txt) do set Tl=%%i
      set Th=%Tl:~0,2%
      set Tm=%Tl:~3,2%
      Set TL=%Th%%Tm%
      For /f "tokens=2" %%j in (4.txt) do set Tj=%%j
      set /A Clc=%Tf%-%TL%
      set /A Day=%dt%-%Tj%
      :del 4.txt
      echo %clc%
      echo %Day%
      set bool=0
      [i]IF "%clc%" GEQ "800" set /a yy=%yy%+1
      IF "%Day%" GTR "1" set /a yy=%yy%+1
      IF %yy% LEQ 2 (
      taskkill /F /IM calc.exe
      ) ELSE (
      taskkill /F /IM notepad.exe
      )
      )[/i] ELSE ( [b]first if end[/b]
      echo Bye Bye
      )

      Pls advice,
      Thanks in advance

      Salmon Trout

      • Guest
      Re: Batch How to handle multiple IF conditions
      « Reply #3 on: September 25, 2009, 12:58:05 AM »
      1. Normally, cmd.exe expands variables ONCE only, at parse time.
      So with variables set and expanded inside parentheses e.g:

      FOR bla bla ba (
          set var=abc
          echo %var%   
          )


      or

      IF bla bla bla (   
          set var=abc
          echo %var%   
          )


      ... the variable %var% is unknown at parse time, so is blank.
      If it was set before the parentheses start, it retains that value.

      Solution:
      -- use delayed expansion, enable it at the start of the batch
      -- use ! exclamation points instead of % percent signs for those
      variables which are both set and expanded inside the parentheses.
      (can use % signs to expand those variables after the parentheses end.)

      REM Put this line near the start of the batch. Only need it once.
      setlocal enabledelayedexpansion
      FOR bla bla ba (
          set var=abc
          echo !var!   
          )

      IF bla bla bla (
          set var=abc
          echo !var!
          )


      2. Each IF can have only 1 ELSE. Therefore redesign logic.

      e.g. use labels and goto

      if "%animal%"=="CAT" goto meow
      if "%animal%"=="DOG" goto grrrr
      goto other

      :meow
      echo animal is a cat
      goto next

      :grrrr
      echo animal is a dog
      goto next

      :other
      echo animal is something else
      goto next

      :next
      echo next part of code




      « Last Edit: September 26, 2009, 01:26:38 AM by Salmon Trout »