As described in this post :
"To get the value of a substring when changing the index inside FOR / IF encloses a substring of two percent and precedes the command with the call. For example:
for /l %%a in (1,1,!string_length!) do ( call set reverse_string=%%string:~!back!,1%%!reverse_string! set /a back-=1 )
Another way to achieve the previous process is to use the extra FOR command to change the slow index extension to an equivalent plug-in parameter, and then use the delayed extension for the substring. This method is faster than the previous CALL:
for /l %%a in (1,1,!string_length!) do ( for %%b in (!back!) do ( set reverse_string=!string:~%%b,1!!reverse_string! ) set /a back-=1 )
"
However, it is inefficient to scroll the line first to count the characters, and then loop again to undo them. I think the method below should be the fastest:
@echo off setlocal EnableDelayedExpansion set maxLength=80 set string=hello set "reverse=" for /L %%i in (1,1,%maxLength%) do ( set "reverse=!string:~0,1!!reverse!" set "string=!string:~1!" if not defined string goto break ) :break echo %reverse%
source share