This question already has answers here:
windows batch command nested variable
(2 answers)
Arrays, linked lists and other data structures in cmd.exe (batch) script
(11 answers)
Closed last year.
CALL SET %PROJECT%_VERSION=1_7_0
echo %%PROJECT%_VERSION%
The result will be %PROJECT.
How can I let it to 1_7_0
Using delayedexpansion makes life easier for these types of scenarios.. You do not need to get the number of % 100% either side of the variables and no need to use call:
#echo off
setlocal enabledelayedexpansion
set "PROJECT=Projectname"
SET "%PROJECT%_VERSION=1_7_0"
echo !%PROJECT%_VERSION!
Related
This question already has answers here:
Arrays, linked lists and other data structures in cmd.exe (batch) script
(11 answers)
Batch Script - How to get variable value using a different variable that has the variable name stored
(1 answer)
using a DelayedExpansion index variable for an array within an IF statement fails
(2 answers)
Closed 3 years ago.
I have a series variable named Installer1 to Installer18
I am trying to install all installer 18 using a loop, so far I have this code to check if I am getting the value but the real value of variable is not reflecting, I believe there is missing on my code but I have tried my best but it is not enough. Thank you in advance
Here is what I have so far
I've got this result, I want to echo the value of the variable i declared on top of my loop but I got the variable name instead of value. I am not sure how to concatenate string and script will look it as a variable name.
I tried below code:
And I got this result:
Use array , Please try this.
#echo off
setlocal enabledelayedexpansion
SET var[0]=A
SET var[1]=foo bar
SET var[2]=123
for %%a in (0,1,2) do (
echo !var[%%a]!
)
echo Get one of the variables directly: %var[1]%
This question already has an answer here:
Variables are not behaving as expected
(1 answer)
Closed 3 years ago.
A very simple batch file. I'm trying to search for file extensions that are not .txt. There will be one .txt, but the rest will be like .txt_20190607.
for %%I in (\\01mtsdv130\Myapp\Log\*.*) do (
set var1=%%~xI
echo %var1
if %var1%==".txt" (
echo Matches
) else (
echo does not match
)
)
I have files in that folder both .txt and those with the extra date info in the extension. What do I have wrong?
There are two problems in the code.
The first one is that %-based expansion of normal variables is rather "static", in that it happens the first time code is parsed/executed and is fixed since then. That means that in iterations of the loop after the first, the result of %var1% will not change. You'd have to use !var1! (along with setting EnableDelayedExpansion) to get the behaviour you want.
An easier alternative is to get rid of var1 altogether and just use %%~xI.
The other problem is that CMD treats quotes (almost) as any other character. Most notably, the strings a and "a" are not considered equal. Therefore, the if should look like this:
if "%%~xI"==".txt" (
This question already has answers here:
Nested environment variables
(2 answers)
Closed 5 years ago.
I want to use one variable value as another variable name in command line script.
e.g:
set VAR1=%1
call set VAR3=JAVA%VAR1%_HOME
echo %VAR3%
This should print the value of system environment variable called JAVA8_HOME (C:\program Files\Java\jdk1.8.0_121)
How can I do it?
call set VAR3=%%JAVA%VAR1%_HOME%%
should solve the problem, assuming % and hence var has a value 8.
call set VAR3=%%JAVA%1_HOME%%
should also work in this instance.
This question already has answers here:
How can I pass arguments to a batch file?
(20 answers)
Closed 6 years ago.
I want to create a cmd command by writing a batchfile and put it into "system32" to call it in the cmd console by its name. Is there a way to expect parameters in the batch file:
Write in cmd:
fake-command Test
And then work with the string "Test" in the batch file?
Use %1 to access the first argument, %2 for the second, etc.
This question already has answers here:
Assign command output to variable in batch file [duplicate]
(5 answers)
Closed 6 years ago.
For my batch file I need to do a few different things...I have completed steps 1-3
1) Perform a dir search and save the result of that search in a variable
2) Set the destination path as a variable
3) Copy the source file to the destination path
My code so far :
#echo off
cls
cd /d D:\Downloads\Videos
set "flshvid=&dir *flash*.mkv /s /b"
set "flshdir=O:\Aayush\Videos\TV Shows\The Flash\Season 3"
xcopy %flshvid% %flshdir%
Why doesn't this code work?
Any help is appreciated. Thanks in advance!
Environment variables may not start with a numeric because %n where n is 0..9 refers to the serial parameter number provided to the routine.
replace 1 and 2 with variable names that do not start with a numeric