Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How batch script get substring by variable index

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%

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading