0

So I want to extract 2 and 3 from the test string by a variable that is set separately, number. But when I try to do this it outputs: 2 where it should output 23. Any advice helps, thank you!

SET test=123
SET number=2

SET two=%test:~1,%number%%

ECHO two
2
  • 3
    Read the explanations at this answer. Although the topic is different, the solution is the same... Commented Jul 3, 2018 at 19:41
  • You essentially asked this same type of question before. Commented Jul 3, 2018 at 19:55

1 Answer 1

1

To solve this issue you must use SETLOCAL EnableDelayedExpansion and ! instead of % for the SET statement like so,

SET t=!test:~1,%number%!

If I understand this correctly, the ! makes the interpreter read test:~1, %number% instead of before where it was reading test:~1 and number separately due to the interpreter thinking the expression was closed because of using two % symbols. In another way, it thought you meant two expressions because of the paired % symbols. Thanks to Aacini for linking this answer which helped me find the solution!

Sign up to request clarification or add additional context in comments.

3 Comments

Yes. Same concept as one of your previous questions. set operator=!operator[%operator%]!
Thanks, am I mistaken in asking another question about a specific function of a different bit of code?
May I ask you to upvote the other answer?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.