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
Excallypurr
  • 319
  • 1
  • 16
  • 3
    Read the explanations at [this answer](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990). Although the topic is different, the solution is the same... – Aacini Jul 03 '18 at 19:41
  • You essentially asked this same type of [question](https://stackoverflow.com/questions/49410637/why-does-my-batch-file-echo-the-array-name-and-number-instead-of-the-string) before. – Squashman Jul 03 '18 at 19:55

1 Answers1

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!

Excallypurr
  • 319
  • 1
  • 16
  • Yes. Same concept as one of your [previous questions](https://stackoverflow.com/questions/49410637/why-does-my-batch-file-echo-the-array-name-and-number-instead-of-the-string). `set operator=!operator[%operator%]!` – Squashman Jul 03 '18 at 20:03
  • Thanks, am I mistaken in asking another question about a specific function of a different bit of code? – Excallypurr Jul 03 '18 at 20:16
  • May I ask you to upvote the [other answer](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990)? – Aacini Jul 03 '18 at 20:38