Page 1 of 1

insert variable into onother variable

Posted: 07 Oct 2013 04:46
by pumi
Hello,

I try insert a variable into an another variable
I tried this:

Code: Select all

@echo off
set fullstr=asdfghjkl
set /a offset=3
set /a chcount=5
rem set substr=%fullstr:~3,5%
set substr=%fullstr:~%offset%,%chcount%% <- this does't work :-(
echo %substr%

How can I insert the variable into an another variable?
Any idea?

thanks in advance, pumi

Re: insert variable into onother variable

Posted: 07 Oct 2013 05:24
by dwngrt
Hello, i am not sure and didn't tested this but i think it should work this way:

set substr=%fullstr:~%%offset%,%chcount%

Re: insert variable into onother variable

Posted: 07 Oct 2013 05:36
by jeb
You need two expansion phases here.

First for the numbers and then for the main variable expansion.

There are multiple ways to solve it, here is one

Code: Select all

setlocal enableDelayedExpansion
set /a offset=3
set /a chcount=5
rem set substr=%fullstr:~3,5%
set substr=!fullstr:~%offset%,%chcount%!

Re: insert variable into onother variable

Posted: 07 Oct 2013 07:04
by pumi
thanks for your help, it works!