Display an asked variable in Batch (variable inside a variable) - variables

Is there any way to ask the user which variable wants to be shown and display it? It should be, as a concept, something like this:
#echo off
set "VAR_1=THIS IS VAR_1"
set "VAR_2=THIS IS VAR_2"
set /p "VAR_TO_SHOW=Enter the variable to show: (VAR_1/VAR_2)"
echo %%VAR_TO_SHOW%%
So if we are in "echo %%VAR_TO_SHOW%%" if we entered "VAR_1" I would want to look like this:
echo %VAR_1%
So the output would be "THIS IS VAR_1". Summarizing all what I've said, I would want to make a variable inside a variable. How can I make this?
Another thing I would want to comment is that I've alredy tried "%%!VAR!%%" and "%%VAR%%" without the exclamation marks, but what is displayied is or "%!VAR_TO_SHOW!%" or the same but without (again) the exclamation marks...

#echo off
set "VAR_1=THIS IS VAR_1"
set "VAR_2=THIS IS VAR_2"
set /p "VAR_TO_SHOW=Enter the variable to show: (VAR_1/VAR_2)"
call echo %%%VAR_TO_SHOW%%%
or
#echo off
setlocal enableDelayedExpansion
set "VAR_1=THIS IS VAR_1"
set "VAR_2=THIS IS VAR_2"
set /p "VAR_TO_SHOW=Enter the variable to show: (VAR_1/VAR_2)"
echo !%VAR_TO_SHOW%!
the second way will work faster.

Related

CMD - how does one set a variable using other variables in the variable name

I want to set a variable that uses another variable in its name. I want to do something like:
set a=2
set b=0
set s%a%%b%=Yee
Obviously, this doesn't work, but I want to be able to call the variable by doing:
echo %s20%
So it would echo Yee. This may be something you can't do, but it would make setting lots of variables much easier.
#echo off
set a=2
set b=0
set s%a%%b%=Yee
call echo %%s%a%%b%%%
:: OR ::
setlocal enableDelayedExpansion
echo !s%a%%b%!
endlocal
Better use the way with the delayed expansion as the call hits the performance.

Is it possible to set a dynamic variable name?

Is it possible to do something like this?
set wordNum=1
set word=Test
:: ^
::This stuff is just so that if people want to
::test it, they have all the variables they need.
set word%wordnum%=%word%
set file=!word%wordNum%!
echo %file%
If so, what am I doing wrong?
In context, I'm trying to remind the computer of the last word it recorded.
Almost done.
setlocal enabledelayedexpansion
set wordNum=1
set word=Test
set word%wordnum%=%word%
set file=!word%wordNum%!
echo %file%
To use delayed expansion you need to enable it

Create a database within a bat file

I am trying to create a bat file that is in essence a database. I want to be able to enter information that is tied to a single record. When I enter a record, I want to be able to look up the record by the card number assigned to it. The code I have now doesn't really work due to the fact that the variables are not being stored properly.
This is my code:
Color 5F
#echo off
:start
cls
echo ==========================================
echo Gift Card
echo ==========================================
echo.
echo What would you like to do?
echo.
echo 1 Add Card
echo 2 Check Information
echo 3 Edit Card Balance
echo 4 Delete Card
echo.
set /p choice=Please enter choice:
if /I %choice%==1 goto 1
if /I %choice%==2 goto 2
:1
echo.
set /p var=Enter Card Number:
set /p val=Enter Amount:
set /p fname=Enter First Name:
set /p lname=Enter Last Name:
set /p cbal=Enter Current Balance:
set /p diss=Enter Date issued:
#echo set %var%=%val%=%fname%=%lname%=%cbal%=%diss% > %var%.bat
echo.
echo The data has been stored!
pause
goto start
:2
echo.
set /p var=Please enter card number:
setlocal enabledelayedexpansion
call %var%.bat
echo !%fname%! !%lname%!'s !%var%! card has $!%cbal%! on it as of !%diss%!!
pause > nul
goto start
I have tried to send the variables separately and altogether and none have worked. I am thinking it is because I do not have the delayed expansion sytax correct.
Any help is very appreciated!
Your problem seems to lie where you generate the batch file to fill the variables... You generate only one line, which does not actually assign anything to any of the variables you need.
Try changing
#echo set %var%=%val%=%fname%=%lname%=%cbal%=%diss% > %var%.bat
to be
#echo set var=%var% > %var%.bat
#echo set val=%val% >> %var%.bat
#echo set fname=%fname% >> %var%.bat
#echo set lname=%lname% >> %var%.bat
#echo set cbal=%cbal% >> %var%.bat
#echo set diss=%diss% >> %var%.bat
This should allow your variables to be loaded back properly.
Also, change
echo !%fname%! !%lname%!'s !%var%! card has $!%cbal%! on it as of !%diss%!!
to read
echo %fname% %lname%'s %var% card has $%cbal% on it as of %diss%!
You should never use both ! and % to surround variables in a batch file, only one or the other. % should be used in most cases; ! should be used when you need to read a variable inside a multi-line "code block" (for example, the result of an if statement or the body of a for loop) which is surrounded by parentheses.
Some more advice:
You can put setlocal delayedexpansion just once in the beginning of the file, right after #echo off. However, you are not doing anything in this program (yet, at least) to need delayed expansion. Delayed expansion is used to enable accessing variables with the ! symbol and is only useful inside multi-line statement bodies surrounded by parentheses. Because of that, you should get rid of it completely unless/until you actually need it, as it can cause other problems.
There is no need for the "#" symbol in any command after you call #echo off (but it won't break anything). The # symbol simply suppresses echoing of the command which it precedes, but it is redundant because all commands are silenced by default after you call echo off. For this reason, it is important to only use it on the first line when calling #echo off, so that the user does not see that command echoed.

for command to set a variable does not work

I'm running this command and I don't see why it won't work
setlocal EnableDelayedExpansion
for %%a in (harry-boy) do set %%a:-==
echo %harry%
pause
And this is the result I get -
e:\6\1>setlocal EnableDelayedExpansion
e:\6\1>for %a in (harry-boy) do set %a:-==
e:\6\1>set harry-boy:-==
e:\6\1>echo
ECHO is on.
e:\6\1>pause
Press any key to continue . . .
I'm changing the hyphen sign to a equals sign then running the set command on that. I expect to see that the variable "harry" = "boy"??
Here is a simple test -
set file=play=here.mkv
set %file:==-%
echo %file%
pause
and I get this -
set file=play=here.mkv
==-%
was unexpected at this time.
set %file:==-%
I thought I would get the new contents of file = play-here.mkv. Ok, I see that this makes the syntax wrong and the set command stops. So how do I change the = to a hyphen?
The string replacement format:
%var:old-string=new-string%
does NOT work on for replaceable parameters, just in Batch variables. The equivalent way for your example, using a variable instead, would be:
set a=harry-boy
set %a:-==%
echo %harry%
pause
Output:
C:>set a=harry-boy
C:>set harry=boy
C:>echo boy
boy
C:>pause
Press any key to continue . . .
Easy soloution:
for /f "tokens=1,2 delims=-" %%a in ("harry-boy") do set %%a=%%b
Echo %harry%
And that should do your job for you. But it will only work with one - in the quote.
SET will assign the value on the right of the first = to an environment variable named on the left.
Hence you would be assigning a value of = to a variable named harry-boy:- in BOTH cases.
You can verify this by executing
set harr
which will display any variable starting harr

Removing spaces from a variable in batch

I am writing a file to remove spaces from filenames in a folder and then put the result in a .txt file. I just get a result of "Echo is on." over and over.
This is what I have so far:
#echo ON
SET LOCAL EnableDelayedExpansion
For %%# in (*.*) do (
SET var=%%~n#
Set MyVar=%var%
set MyVar=%MyVar: =%
echo %MyVar%>>text.txt
)
Can someone tell me whats wrong?
Removing all spaces (not just leading and trailing) can be done without using setlocal enabledelayedexpansionwith the following line:
set var=%var: =%
This works by replacing all spaces in the string with the empty string.
Source: DOS - String Manipulation
The reason why you are getting ECHO is on. is because delayed expansion was not used, which caused the value of %var% and %MyVar% to be inserted before the for command is run, and since they were not defined at the start, empty variables were inserted in. When the echo %MyVar%>>text.txt was run, it was interpreted as echo >>text.txt. When echo is run without any arguments, it outputs whether echo is on or off, which is what you get in text.txt.
To fix the problem, you have to do two things:
First, there is something wrong with your second line. There is no space between set and local in setlocal. The second line should be SETLOCAL EnableDelayedExpansion.
Second, to use delayed expansion, you have to replace all %s in each variable with !, like !var! instead of %var%.
End result:
#echo ON
SETLOCAL EnableDelayedExpansion
For %%# in (*.*) do (
SET var=%%~n#
Set MyVar=!var!
set MyVar=!MyVar: =!
echo !MyVar!>>text.txt
)
You actually do not need to use a temporary variable in this case, you can just do SET MyVar=%%~n# and skip to set MyVar=!MyVar: =!.
The wrong thing is you've enabled the variable expansion (you wroted it bad...) and also you are not using it, when you use enabledelayedexpansion you need to write the variable names as this: !Variable! instead of this else: %Variable%
But you don't need to use it with this code:
#echo ON
For %%# in (*) do (
SET "var=%%~n#"
Call Set "MyVar=%%var: =%%"
Call echo %%MyVar%%>>text.txt
)
Run the following batch in the folder holding the files to be renamed
#echo off
setlocal enabledelayedexpansion
for %%j in (*.*) do (
set filename=%%~nj
set filename=!filename=.=_!
set filename=!filename= =_!
if not "!filename!"=="%%~nj" ren "%%j" "!filename!%%~xj"
)
you just need to add the print to txt
The set var=%var: =% did not work for me.
So I tried with success for a number the following code:
set /a var-=1 & set /a var+=1