feenyman99
feenyman99

Reputation: 165

DOS Substring where Length is a Variable

I'm trying to extract a substring, from a variable, in a DOS batch script. As I understand it, one does this with a statement like the following:

set substring=%original_string:~2,3%  

Where 2 would be a zero-based offset, and 3 would be the length of the substring to be extracted. So far, so good, right?

But, I need my length to be a variable, not a explicit number. I'm almost sure this can be done, but the proper syntax is eluding me :-(

What I have tried is...

set string=string

set length=3

set substring=%string:~0,%length%%

echo substring is: %substring%

What I get is...

substring is: length%

I've tried removing 1 set of percent signs, but... no good.

Does anyone know if and how I can do what I want???

thx!

Upvotes: 3

Views: 4921

Answers (1)

DogLimbo
DogLimbo

Reputation: 446

Change it to this:

call set substring=%%string:~0,%length%%%

Upvotes: 1

Related Questions