0

I know we could get substring like this in batch script:

SET a=abcdefgh
ECHO %a:~3,2%

But how can I get letter by a variable index? Kind like:

SET index=3
ECHO %a:~%index%,1%
aschipfl
  • 33,626
  • 12
  • 54
  • 99
lufy
  • 33
  • 5
  • Either `call echo %%a:~%index%,1%%` (only in a batch file), or `echo !a:~%index%,1!` (in a batch file as well as in Command Prompt), which applies [delayed variable expansion](https://ss64.com/nt/delayedexpansion.html), which must first be enabled… – aschipfl Jan 21 '22 at 13:43
  • See [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 not exactly the same, the method used there also apply here – Aacini Jan 21 '22 at 15:34

1 Answers1

-1

Since the substring operation needs a constant it has to be called in a subprocess, that's when CALL comes in handy:

SET _index=3
call set b=%%a:~%index%,1%%
echo (%b%)

See https://ss64.com/nt/syntax-substring.html for more details. SS64/nt is an excellent resource for anything batch related.

Alim Özdemir
  • 2,396
  • 1
  • 24
  • 35