how concatenate two variables in batch script? - batch-file

I want to do something like this in batch script. Please let me know if this is the proper or possible way to do it or any other way?
set var1=A
set var2=B
set AB=hi
set newvar=%var1%%var2%
echo %newvar%
This should produce the value "hi".

Enabling delayed variable expansion solves you problem, the script produces "hi":
setlocal EnableDelayedExpansion
set var1=A
set var2=B
set AB=hi
set newvar=!%var1%%var2%!
echo %newvar%

You can do it without setlocal, because of the setlocal command the variable won't survive an endlocal because it was created in setlocal. In this way the variable will be defined the right way.
To do that use this code:
set var1=A
set var2=B
set AB=hi
call set newvar=%%%var1%%var2%%%
echo %newvar%
Note: You MUST use call before you set the variable or it won't work.

The way is correct, but can be improved a bit with the extended set-syntax.
set "var=xyz"
Sets the var to the content until the last quotation mark, this ensures that no "hidden" spaces are appended.
Your code would look like
set "var1=A"
set "var2=B"
set "AB=hi"
set "newvar=%var1%%var2%"
echo %newvar% is the concat of var1 and var2
echo !%newvar%! is the indirect content of newvar

Related

Is there a way to shrink the long and repeat "set" command into a single command?

So I got a following piece of code which will set the variable as another variable
set c1_page_%page_num%=%c1%
set c2_page_%page_num%=%c2%
set c3_page_%page_num%=%c3%
set c4_page_%page_num%=%c4%
set c5_page_%page_num%=%c5%
set c6_page_%page_num%=%c6%
set c7_page_%page_num%=%c7%
set c8_page_%page_num%=%c8%
set c9_page_%page_num%=%c9%
set c10_page_%page_num%=%c10%
set c11_page_%page_num%=%c11%
set c12_page_%page_num%=%c12%
set c13_page_%page_num%=%c13%
set c14_page_%page_num%=%c14%
set c15_page_%page_num%=%c15%
I need to somehow still able to set all the variable as another variable with only a single line of code or more. Please help me
I am assuming you want a single line that will do the work for you?
#echo off
for /l %%i in (1,1,15) do call set c%%i_page_%page_num%=%%c%%i%%
or using delayedexpansion
#echo off
setlocal enabledelayedexpansion
for /l %%i in (1,1,15) do set c%%i_page_%page_num%=!c%%i!

Concatenate string from an array

I want to concatenate the string in the array inside the for loop. Once it get concatenate it should do some task and then it go to the another variable in the array. Please let me know how to do it. Give me some examples.
Here is the code I am trying to execute.
#echo off
set topic[0]=USB
set topic[1]=hello
set topic[2]=mic
set topic[3]=Operators
set file = C:\Users\User\Android_Studio_Projects
for /l %%n in (0,1,2) do (
set file=%file% CD\!topic[%%n]!
)
pause
You need to be carefull of whitespace before and after = when setting variables, also, to use delayedexpansion, you need to enable it. You are also trending on the line with whitespace in setting variables, always enclose your variable set in double quotes, like I did below. Try this:
#echo off
setlocal enabledelayedexpansion
set "topic[0]=USB"
set "topic[1]=hello"
set "topic[2]=mic"
set "topic[3]=Operators"
set "file=C:\Users\User\Android_Studio_Projects\"
for /l %%n in (0,1,3) do (
echo cd %file%!topic[%%n]!
)
pause
You do not need to set %file% again inside of the loop seeing that nothing changed. This will only display the command to you, to cd to each directory, just remove echo in the loop.

Use a variable on SET in Windows batch file

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!
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%

How to refer to a variable in Batch Scripting?

Imagine a script called batch.bat, invoked like this:
batch.bat Two
The script contains:
set One=1
set Two=2
set Three=3
set Choice=%1
echo %Choice%
But I want to echo 2, not Two.
What elegant way can I employ to do that? I thought about if %Choice%=One, etc. but my actual script is a bit more complex than the example provided.
Edit: I also tried:
set percent=%%
echo %percent%%Choice%%percent%
But it won't treat the resulting expression as a variable reference.
#echo off
setlocal EnableDelayedExpansion
set one=1
set c=%1
echo delayed expansion and variable: !%c%!
echo delayed expansion and paramr: !%1!
call echo using call and variable: %%%c%%%
call echo using call and param: %%%1%%
I personally prefer the delayed expansion method, but sometimes there are reasons to look for an alternative.
OK, I have figured it out:
The key is to use the "set delayed expansion" trick.
There's also a trick using the "call set" instead of the above but I reckon if I use that, the next guy after me won't know what I was trying to do and might get in trouble.
The script can be fixed by adding !! around the reference that we want to force into the variable name, like so:
#echo off
setlocal EnableDelayedExpansion
set One=1
set Two=2
set Three=3
set Choice=!%1!
echo %Choice%

input a variable name and display - BATCH FILE

I wanted the user insert a variable and then be displayed with echo It would be something like %%var%%
A variable inside another variable
set var=value
set x=var
setlocal enableDelayedExpansion
echo !%x%!
...
endlocal
This should outperfrom the call approach.
I hope I got your question right. You need an additional parse-phase. this can be done with call:
set var=value
set x=var
call echo %%%x%%%
REM on commandline: call echo %%x%%
(yes, that's ugly, but it works)

Resources