|
Answer» 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 advanceWe 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.
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 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
|