Batch-File Variable Part of Another Variable - batch-file

Okay so let's say I have the following code
set playerlevel=5
set x=player
echo %x%level
I want the output to be "5", is that possible?

dbmitch's answer is good for integers, but if you want to display strings or integers, you can simply use delayed expansion.
#echo off
setlocal enabledelayedexpansion
set playerlevel=5
set x=player
echo !%x%level!
Note that if the code is located inside of a code block (i.e. enclosed in parentheses), the syntax is slightly different.
#echo off
setlocal enabledelayedexpansion
REM This is just an example code block to show off the alternate syntax with the %%s
for /L %%A in (1,1,1) do (
set playerlevel=5
set x=player
call echo %%!x!level%%
)

You can get what you want by using a combination of enabledelayedexpansion and the set /a to emulate an EVAL function
Try this:
#echo off
setlocal enabledelayedexpansion
set playerlevel=5
set x=player
set /a varx = "%x%level"
echo %varx%

Related

"for" loop not working as per need

I have following code:
set b=Hello
set c=1
set d=5
for /l %%x in (1,1,%d%) do (set /a c=c+1 & set "a=%a%%b%%c%")
echo %a%
And for this, I wanted an output as:
Hello1Hello2Hello3Hello4Hello5
But instead, I get result as:
Hello1Hello1Hello1Hello1Hello1
Can anyone give me any idea on how can I do so?
You need:
setlocal enableextensions enabledelayedexpansion
set b=Hello
set c=0
set d=5
for /l %%x in (1,1,%d%) do (set /a c=c+1 & set "a=!a!!b!!c!")
echo %a%
endlocal
The ! variant of % will expand the variables at the time the code is executed whereas % expands when the code is parsed. And, since parsing happens on the entire statement (from for to the closing parenthesis), you'll get the original value only.
Note that you don't need delayed expansion for the invariant b but, once you've decided you need them, you may as well use them everywhere - they act more as you'd expect in most cases.
You'll notice I've also changed the initial value of c to get the output you stated that you wanted. As it was, you would get 2,3,4,5,6 rather than 1,2,3,4,5.
I think this is what you want:
#Echo Off
Set "a="
Set "b=Hello"
Set "c=1"
Set "d=5"
For /L %%A In (%c%,1,%d%) Do Call Set "a=%%a%%%b%%%A"
If Defined a Echo %a%
Pause
Or alternatively:
#Echo Off
Set "a="
Set "b=Hello"
Set "c=1"
Set "d=5"
For /L %%A In (1,%c%,%d%) Do Call Set "a=%%a%%%b%%%A"
If Defined a Echo %a%
Pause

Batch Coding For loop to create files with different names

I'm new to Batch coding, so please go easy.
Please consider the following code:
FOR /L %%G IN (1,1,20) DO (break>"C:\New folder\%%G+1.txt")
I'm trying to create text files with the above code, but I'm getting 1+1, 2+1, 3+1.. and so on.
Is there a way to not touch the parameters, but to increase the parameter in %%G+1? Instead of outputting as a string, it gives me a number.
Please guide me. thanks
Update: I tried this code below
:MakeTextFiles
setlocal enabledelayedexpansion
set "var=1"
FOR /L %%G IN (1,1,20) DO
(
set /a "var=%var%+1"
break>"C:\New folder\!var!.txt"
)
EXIT /B
But it's not working.
setlocal enabldelayedexpansion
FOR /L %%G IN (1,1,20) DO
(
set /a _new=%%G+1
break>"C:\New folder\!_new!.txt"
)
Please see hundrds of articles on SO about delayedexpansion
Two problems with your latest change:
The ( must be on the same physical line as the do.
set /a var=!var!+1
or
set /a var=var+1
or
set /a var+=1
set /a accesses the run-time value of var, !var! is the run-time value of var, %var% is the parse-time value of var - the value that it has when the for is encountered.
You don't need arithmetic addition here, just change the set you loop over:
FOR /L %%G IN (2,1,21) DO (break>"C:\New folder\%%G.txt")
If you definitely want arithmetic:
setlocal enabledelayedexpansion
FOR /L %%G IN (1,1,20) DO (
set /a var=%%G+1
break>"C:\New folder\!var!.txt")
You need to look here:
calculating the sum of two variables in a batch script
and here
delayed expansion

Echo batch file arrays using a variable for the index?

If I have a batch file and I am setting arrays with an index that is a variable
#echo off
SET x=1
SET myVar[%x%]=happy
How do I echo that to get "happy" ?
I've tried
ECHO %myVar[%x%]%
ECHO %%myVar[%x%]%%
ECHO myVar[%x%]
But none of them work.
It works fine if I use the actual number for the index
ECHO %myVar[1]%
But not if the index number is also a variable
SET x=1
SET myVar[%x%]=happy
call echo %%myvar[%x%]%%
set myvar[%x%]
for /f "tokens=2* delims==" %%v in ('set myvar[%x%]') do #echo %%v
setlocal enableDelayedExpansion
echo !myvar[%x%]!
endlocal
I would recommend you to use
setlocal enableDelayedExpansion
echo !myvar[%x%]!
endlocal
as it is a best performing way
There is a special ! character in batch to deal with your situation. Use echo !myVar[%x%]!, from How to return an element of an array in Batch?. ! means delayed expansion. The variable myVar will not get expanded until after %x% is, yielding the expression you want.
one way you can do this is to use
call echo %%myVar[%x%]%%
call lets you put variables in places where they wouldn't normally work, if you use the double percents

Why won't this batch code work?

Assuming that %Edata% was a variable written like this "A;B;C;1;2;3;" then this code should be able to separate it into a bunch of numbered variables:
set /a c=0
FOR %%A IN (%Edata%) DO (
set /a c=%c%+1
set var%c%=%%A
echo.^>^>^> Set "%%A" to "var%c%"
)
Only the result is setting all parts of the variable to var0 because the %c% variable doesn't count up each time like it's supposed to. Could someone explain why?
This code works:
#echo off
setlocal EnableDelayedExpansion
set /a c=0
FOR %%A IN (%Edata%) DO (
set /a c=!c!+1
set var!c!=%%A
echo.^>^>^> Set "%%A" to "var!c!"
)
The problem in your script is that variables by default are expanded at parse time and not at execution time. In this case c is expanded only once, before entering the loop, that's why its value is always 0 and it never changes.
You have to enable the expansion of variables at execution time with this command:
setlocal EnableDelayedExpansion
and you have to use !c! instead of %c% inside the for loop.

Splitting string using delayed expansion limitation

When I was testing a script I came across this issue when trying to extract characters from a string using batch. I have simplified it into a simple example. t.txt just contains the word hello.
#echo off
setlocal enabledelayedexpansion
set a=0
set b=1
for /f %%a in (t.txt) do (
set x=%%a
echo !x:~!a!,!b!!
set /a x+=1
)
pause >nul
The problem is, the variable x needs to be accessed using delayed expansion, and because I am updating the values of a and b through the loop these also need to be accessed using delayed expansion.
When trying to use the variables a and b to split the string they all need delayed expansion, but the order of the ! marks means that it is not parsed the way I intended!
CMD will expand my command as !x:~!, !,! and !!, instead of expanding the inner ones first. Obviously I can't use %'s either.
The only way I have found to get around this is to call an external function that isn't in the loop, so I can use %'s.
#echo off
setlocal enabledelayedexpansion
set a=0
set b=1
set v=
for /f %%a in (t.txt) do (
set x=%%a
call :RETURN x
set /a x+=1
)
pause >nul
:RETURN
set v=%1
echo %v:~!a!,!b!%
Is there any way of getting cmd to parse my command how I need it to, or this just a limitation I will have to use call for?
Simply transfer variables a and b to FOR variables.
#echo off
setlocal enabledelayedexpansion
set a=0
set b=1
for /f %%a in (t.txt) do (
set "x=%%a"
for /f "tokens=1,2" %%A in ("%a% %b%") do echo !x:~%%A,%%B!
REM this line makes no sense if x=hello: set /a x+=1
)
pause >nul
Mixing delayed and normal expansion will work.
#echo off
setlocal EnableDelayedExpansion
set a=0
set b=1
for /f %%L in (t.txt) do (
set "x=%%L"
echo !x:~%A%,%B%!
)

Resources