1

Code Excerpt of my batch file:

set stringOne=ABCDEF    
echo %stringOne:~2,3%  

This output is CDE

How can I dynamically echo the output for my start index and desired output length?

set stringOne=ABCDEF  
set start=2  
set len=3
double-beep
  • 5,031
  • 17
  • 33
  • 41
arkeyar
  • 13
  • 3
  • This type of management is described 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... – Aacini Nov 26 '18 at 03:08

1 Answers1

1

you need two layers of variable expansion. That can be done by delayed expansion or by call:

@echo off
setlocal enabledelayedexpansion
set "string=ABCDEFGH"
set "start=2"
set "len=3"

echo A with delayed expansion: !string:~%start%,%len%!
call echo A with using 'call': %%string:~%start%,%len%%%

FOR /F %%G IN ('dir /b "%~f0"') DO ( 
  set /A "newStart=!Start!+2" 
  call echo B with 'call' and delayed : %%string:~!newStart!,!len!%%
  call call echo B with double-'call': %%%%string:~%%newStart%%,%len%%%%% 
)

FOR /F %%G IN ('dir /b "%~f0"') DO call :output
goto :eof
:output
  set /A "newStart=Start+2" 
  echo C with subroutine and delayed expansion: !string:~%newStart%,%len%! 
  call echo C with subroutine andusing 'call': %%string:~%newStart%,%len%%% 
goto :eof

EDITED to match your comment. You need a third layer of expansion. I expanded the code with some different methods.
(btw: please don't post code in comments, it's nearly impossible to read. And if your question changes, better ask a follow-up question next time)

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • with delayed expansion: CDE with using 'call': CDE But, setlocal enabledelayedexpansion set "string=ABCDEFG" set "start=2" set "len=3" FOR /F %%G IN ('dir /b *.txt') DO ( set "fileName=%%G" set /A "newStart=!Start!+2" echo !newStart! echo with delayed expansion: !string:~%newStart%,%len%! call echo with using 'call': %%string:~%newStart%,%len%%% ) O/P 4 with delayed expansion: ABC (instead of EFG) with using 'call': ABC (instead of EFG) How I will be able to get the output EFG ? – arkeyar Nov 25 '18 at 16:15