2

I´m trying to use a variable instead of a fixed number here:

set PACK_VERSION=Testing12345
set PACK_VERSION=%PACK_VERSION:~7,100%
echo %PACK_VERSION%

Instead of this number 7, I would like to use a variable, something like this:

set VARNUM=7
set PACK_VERSION=Testing12345
set PACK_VERSION=%PACK_VERSION:~%VARNUM%,100%
echo %PACK_VERSION%

I don´t know how to insert that properly, anyone can help? Thanks!

Daute
  • 23
  • 3
  • 1
    Possible duplicate of [Batch files: How to do substring with variable length in a for loop](https://stackoverflow.com/questions/8913453/batch-files-how-to-do-substring-with-variable-length-in-a-for-loop) – phuclv Dec 29 '17 at 16:42

1 Answers1

2

The "usual" way is using delayed expansion:

setlocal enabledelayedexpansion
set VARNUM=7
set PACK_VERSION=Testing12345
set PACK_VERSION=!PACK_VERSION:~%VARNUM%!
echo %PACK_VERSION%

but there is also a little trick to do it without delayed expansion (you have to parse the line twice, call is a good method to do so):

set VARNUM=7
set PACK_VERSION=Testing12345
call set PACK_VERSION=%%PACK_VERSION:~%VARNUM%%%
echo %PACK_VERSION%
Stephan
  • 53,940
  • 10
  • 58
  • 91