1

How would I do to use the variable %number% in this case?

Set Test=%strToMeasure:~-%Number%%

Whenever I use a variable the result comes out like this:

No variable only the number:

Set Test=%strToMeasure:~-3%

enter image description here

With variable:

Set Test=%strToMeasure:~-%Number%%

enter image description here

Full Code

@echo off

Set "strToMeasure=This is a string"
call :strLen strToMeasure strlen
echo.String is %strlen% characters long
Set /A number = %strlen% - 13

Set Test=%strToMeasure:~-%Number%%

Echo %strToMeasure%
Echo %Test%

pause


exit /b

:strLen
setlocal enabledelayedexpansion
:strLen_Loop
  if not "!%1:~%len%!"=="" set /A len+=1 & goto :strLen_Loop
(endlocal & set %2=%len%)
goto :eof
Humberto Freitas
  • 461
  • 1
  • 6
  • 21

1 Answers1

1

You have two options in order to correctly execute this line:

Set Test=%strToMeasure:~-%Number%%

1- Doubling the percent signs for the second expansion and using call command:

Call Set Test=%%strToMeasure:~-%Number%%%

2- Using Delayed Expansion and enclosing the second expansion in exclamation marks:

setlocal EnableDelayedExpansion

. . .

Set Test=!strToMeasure:~-%Number%!

The second method is the usual way to solve this problem and it run faster than the former. You may review further details of this behavior at this post.

Community
  • 1
  • 1
Aacini
  • 65,180
  • 12
  • 72
  • 108