Why does this not work

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Why does this not work

#1 Post by Cleptography » 07 Jun 2011 23:22

set str=operation
if "%str%"==str echo.%str%

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Why does this not work

#2 Post by Ed Dyreen » 07 Jun 2011 23:25

Euhhh
set str=operation
if "%str%"=="str" echo.%str%

renzlo
Posts: 116
Joined: 03 May 2011 19:06

Re: Why does this not work

#3 Post by renzlo » 08 Jun 2011 01:02

Try this:

setlocal enabledelayedexpansion
set str=operation
if '!str!'=='str' echo. %str%

im no pro on this

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Why does this not work

#4 Post by dbenham » 08 Jun 2011 05:34

Cleptography wrote:set str=operation
if "%str%"==str echo.%str%

What is not working :?: Nothing happens as expected.

"operation" <> str so echo does not fire.

Dave Benham

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Why does this not work

#5 Post by orange_batch » 10 Jun 2011 06:26

lol 3 people missed the obvious error.

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Why does this not work

#6 Post by Ed Dyreen » 10 Jun 2011 06:33


Which error did we miss ?

Code: Select all

@echo off

echo.errorlevel=%errorlevel%

set str=operation
if "%str%"==str echo.%str%

echo.errorlevel=%errorlevel%

pause
exit

Code: Select all

errorlevel=0
errorlevel=0
Druk op een toets om door te gaan. . .

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Why does this not work

#7 Post by orange_batch » 10 Jun 2011 16:02

Not error levels. Bad syntax.

Batch is interpreted as text, there's no literal or non-literal working with variables with the exception of set /a expressions. Quotations exist as regular characters too, the only difference being they toggle DOS's interpretation of special characters.

Code: Select all

set str=operation
if "%str%"==str echo.%str%

Is the same as:
if "operation"==str

Code: Select all

set str=operation
if "%str%"==%str% echo.%str%

Is the same as:
if "operation"==operation

Code: Select all

set str=operation
if "%str%"=="operation" echo.%str%

Works, as would "%str%" obviously. This is what dbenham is talking about.
Last edited by orange_batch on 10 Jun 2011 22:13, edited 1 time in total.

Cleptography
Posts: 287
Joined: 16 Mar 2011 19:17
Location: scriptingpros.com
Contact:

Re: Why does this not work

#8 Post by Cleptography » 10 Jun 2011 18:39

@orange_batch
Well I'll be god damned. Good observation
That is a first.

Post Reply