Page 1 of 1

DOS Batch IF statement problems

Posted: 12 Sep 2011 11:46
by jinkskgpt
I have a really weird problem. It is almost like there is some setting in my cmd prompt window environment that is making the IF command work incorrectly.

here is the cmd file I am trying to run:

Code: Select all

@echo off
set str1="Default Web Site"
set str2="Default Web Site"
echo %str1%
echo %str2%

REM @echo on
IF /I (%str1% EQU %str2%) (
echo %_WebSiteName% Found bsw!
goto :siteFound
)
echo %_WebSiteName% NOT Found bsw!
:siteFound
echo.
pause

Every time I run this the IF command evaluates to FALSE. Now that's NOT supposed to happen: especially when you Hardcode both variable.

Any help will be appreciated!

Re: DOS Batch IF statement problems

Posted: 12 Sep 2011 15:16
by trebor68
Here the code:

Code: Select all

@echo off
set str1="Default Web Site"
set str2="Default Web Site"
echo %str1%
echo %str2%

REM @echo on
IF /I %str1% EQU %str2% (
  echo %_WebSiteName% Found bsw!
) else (
  echo %_WebSiteName% NOT Found bsw!
)
echo.
pause



IF condition commands if true

IF condition (commands if true) ELSE commands if false

IF condition (
commands if true
) ELSE (
commands if false
)

Re: DOS Batch IF statement problems

Posted: 12 Sep 2011 16:35
by nitt
trebor68 wrote:Here the code:

Code: Select all

@echo off
set str1="Default Web Site"
set str2="Default Web Site"
echo %str1%
echo %str2%

REM @echo on
IF /I %str1% EQU %str2% (
  echo %_WebSiteName% Found bsw!
) else (
  echo %_WebSiteName% NOT Found bsw!
)
echo.
pause



IF condition commands if true

IF condition (commands if true) ELSE commands if false

IF condition (
commands if true
) ELSE (
commands if false
)


Yeah, all commands have to be on one line, but everything in between the brackets are read as one line. Such as:

Code: Select all

@echo off
(
echo line1
echo line2
echo line3
) > text.txt

Re: DOS Batch IF statement problems

Posted: 13 Sep 2011 02:39
by trebor68
More as one command is also possible. But for the "end of line" you need the brackets "( )".

(command 1) & (command 2) & command 3

See this code:

Code: Select all

@echo off
(
echo line1
echo line2
echo line3
) > text15.txt
echo.>>text15.txt
((echo line1) & (echo line2) & (echo line3)) >> text15.txt
echo.>>text15.txt
(echo line1>> text15.txt) & (echo line2>> text15.txt) & echo line3>> text15.txt