DOS Batch IF statement problems

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jinkskgpt
Posts: 2
Joined: 12 Sep 2011 11:26

DOS Batch IF statement problems

#1 Post by jinkskgpt » 12 Sep 2011 11:46

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!

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: DOS Batch IF statement problems

#2 Post by trebor68 » 12 Sep 2011 15:16

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
)

nitt
Posts: 218
Joined: 22 Apr 2011 02:43

Re: DOS Batch IF statement problems

#3 Post by nitt » 12 Sep 2011 16:35

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

trebor68
Posts: 146
Joined: 01 Jul 2011 08:47

Re: DOS Batch IF statement problems

#4 Post by trebor68 » 13 Sep 2011 02:39

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

Post Reply