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

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.