string not working

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Rajnishjc_27
Posts: 17
Joined: 16 Aug 2019 23:35

string not working

#1 Post by Rajnishjc_27 » 26 Aug 2019 08:30

hi

i am using below script which is not working

set res1='YES'

echo !res1!
if "!res1!" EQU "YES" echo "HI"

its not displaying HI , please help

aGerman
Expert
Posts: 3673
Joined: 22 Jan 2010 18:01
Location: Germany

Re: string not working

#2 Post by aGerman » 26 Aug 2019 09:20

You compare "'YES'" with "YES". The first has an additional pair of single quotes as assigned using SET.

Steffen

Rajnishjc_27
Posts: 17
Joined: 16 Aug 2019 23:35

Re: string not working

#3 Post by Rajnishjc_27 » 26 Aug 2019 21:46

Hi Steffen

Then how it should be ?

ShadowThief
Expert
Posts: 906
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: string not working

#4 Post by ShadowThief » 27 Aug 2019 02:55

You've got two options:

Option 1 (recommended) - Use double quotes instead of single quotes in the set statement. Note that if you go this route, you should move the first double quote to the left of the variable name so that you get the benefit of quotes without the quotes actually being appended to the value of the string.

Code: Select all

set "res1=YES"

echo !res1!
if "!res1!" EQU "YES" echo "HI"
Option 2 - Include the single quotes in your if statement

Code: Select all

set res1='YES'

echo !res1!
if "!res1!" EQU "'YES'" echo "HI"
Note that in both situations, your code will print "HI" instead of HI because echo does not need quotes.

Post Reply