Using a Variable in String Manipulation

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
alleypuppy
Posts: 82
Joined: 24 Apr 2011 19:20

Using a Variable in String Manipulation

#1 Post by alleypuppy » 06 Dec 2011 23:30

Hello,

I have a batch file that loops n amount of times until an IF statement is true. Each time it loops, it sets a variable to a new value (set /a num+=1). I want to use this variable (num) as part of a string manipulation number:

Code: Select all

IF /I %VAR:~%num%,1%=="A" [do commands]
This, however, does not seem to work.
I've also tried:

Code: Select all

SETLCOAL ENABLEDELAYEDEXPANSION
IF /I !VAR:~%num%,1!=="A" [do commands]
This does not seem to work either. Perhaps I am using DelayedExpansion incorrectly, but I don't know because I'm still not all that familiar with how DelayedExpansion works.

Thanks for any help!

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

Re: Using a Variable in String Manipulation

#2 Post by orange_batch » 07 Dec 2011 01:17

You're on the right track.

The first case doesn't work because, command prompt reads from % to % to determine variables:
IF /I %VAR:~%num%,1%=="A" [do commands]

With delayed expansion, it reads as:
IF /I !VAR:~%num%,1!=="A" [do commands]

This should work, except the problem is there is a syntax error. You cannot expose a comma to command prompt in the comparison. So either enclose both sides of the comparison in quotation marks (disables special character handling) or escape the problematic character using a caret ^.

IF /I "!VAR:~%num%,1!"=="A" [do commands]

or

IF /I !VAR:~%num%^,1!==A [do commands]

Remember that the quotation marks are included in the comparison, so they need to be present on both sides appropriately.

By the way, you mis-typed SETLOCAL as SETLCOAL.

Lastly, it's only needed for commas or special characters like &|<>, so quotation marks aren't needed in this example:

Code: Select all

setlocal enabledelayedexpansion
set var=ABCDE
if !var:~1!==BCDE echo The characters following the first one in var are BCDE.
if !var:~-1!==E echo Last character of var is E.

alleypuppy
Posts: 82
Joined: 24 Apr 2011 19:20

Re: Using a Variable in String Manipulation

#3 Post by alleypuppy » 07 Dec 2011 13:48

Great! Thanks!
By the way, you mis-typed SETLOCAL as SETLCOAL.
Whoops! :shock:
The code that I posted was just an example, however, it isn't an actual part of my batch file, so it wouldn't have made a difference either way, but I'm glad you pointed that out!

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

Re: Using a Variable in String Manipulation

#4 Post by dbenham » 09 Dec 2011 11:23

orange_batch wrote:Lastly, it's only needed for commas or special characters like &|<>, so quotation marks aren't needed in this example:

It is also needed for = when doing search and replace with delayed expansion. I try to always use the quotes when doing any comparison using delayed expansion just so I don't get caught.

Dave Benham

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

Re: Using a Variable in String Manipulation

#5 Post by orange_batch » 09 Dec 2011 18:11

Yep that too, that's what I meant by "like" :) I never have a full list of problematic characters in my head. :o

Post Reply