Nesting Variables in batch - batch-file

I have been having a real issue getting something that seems simple, but I can not seem to get a working answer.
Here is what I am doing:
Setting a variable with a specific name: ex. set "select=computer1"
I do a test and give that variable a value based on the result after using an if statement : ex set "%select%=1" This sets the variable %computer1% to 1
I want to see what the value of %computer1% is however all I know is %select%.
Is there any way I can get the value of %computer1% when only knowing %select%? %select% knows the name of the new variable but I need to know its value without being able to directly call it by hard coded name.
I thought a pipeline might work but I can not seem to figure this out.
Thanks all, I appreciate your help.

You need another layer of parsing. You can do it:
with delayed expansion:
setlocal enabledelayedexpansion
echo !%select%!
without delayed expansion:
call echo %%select%%

Related

escaping the special characters without knowing the position batch

I have a requirement to execute a batch file with the arguments. the problem occuring here is I am not sure what special characters and at what place it can occur. I have gone through possibly all the answers here but still unable to fix the issue. Any help is appreciated.
Command that i am trying to run within the batch file
CALL E:\myscript.bat -run e:\param -password %PASSWORD%
where PASSWORD is the variable storing the password and it can be having any special characters at any position.
Maybe I understood your question, it would be:
You want to identify, without having to have specific argument
positions, what each argument represents.
Or:
You want to find a solution to handle the arguments so that they do
not result in errors because they contain special characters
Anyway, I will answer both.
You have some alternatives for doing so, some may be more ideal than others depending on how you are handling your code and its purpose.
In case you identify the arguments and what they represent, you can:
set args=%*
Some things may help you make your code stable while using an wildcard without its effect. Like:
set "args=%args:&=^&"
But the most important thing is to keep the code without quotes. In fact, the use of CALL is not necessary if you are not doing it within a script.
set "args=%args:"=%
The next thing to do is to identify the parameters. We can do this both in a crude way and in a simpler way, which would be defining each argument with its own variable, such as: arg1=%1, arg2=%2...
You can now pass all parameters into a treatment and reconstruction session.
Assuming you want to identify -password:
---during a FOR loop to catch and make the same processes at every arg---
(which I can only guess because you didn’t show the code or anything like that)
if "!arg[%%x]:~0,1!" equ "-" (
if "!arg[%%x]:-=!" equ "password" set /a x=%%x+1&set "password=!arg[%x%]!"
if "!arg[%%x]:-=!" equ "run" set /a x=%%x+1&set "run=!arg[%x%]!"
...
)
Obviously there are some things that can improve in this code, but you have shown almost nothing for us to use as a reference to answer, so that's all I can do.
Hope this helps,
K.

Removing a variable number of characters from front of a string in batch

I am trying to remove a changing number of characters from a string right now I have it as
set /A VAR=%VAR:~%Num%%
is there any way you can think to make this work.
I am currently trying to it with a goto loop so I can still reference the variable
If you want to look at my code in context you can look at it on github at thumbdown.camojackson.com
Well without seeing all your code I can give you some options.
For starters you do not use the /A option to do string manipulation.
1) Use delayed expansion.
set VAR=!VAR:~%Num%!
2) Use the CALL command to get an extra phase of expansion
CALL set VAR=%%VAR:~%Num%%%

Windows Batch Variable inside variable

I'm not very good at code, but I'm trying to get something to work, which would make my life easier.
I need to get a variable to run from within a variable, if possible, but can't get it to work out.
set MAP1=Esseker
set %MAP1%MODS=#exile
-mod=%%MAP1%mods%
This is not the full code, but it's just a section showing what it is I am trying to do. -mod= is correct as it's part of a startup parameter.
You can do this by enabling delayed expansion.
Here's an example script:
#echo off
setlocal EnableDelayedExpansion
set MAP1=Esseker
set %MAP1%MODS=#exile
echo !%MAP1%MODS!
-mod=!%MAP1%MODS!
#echo on
Not sure if that -mod line should be a set, but I've copied what you had above, making the assumption that it's correct.

Batch File: Output variable to text file

I would like to output a variable to a text file. I have done this in my code, but I have one output within an if statement that I can't get to work.
if not exist "%TuningLog%" (
set Title=Tuning Parameters Saving Log
set LogTitle=--------- %Title% ---------
echo %LogTitle%>> "%TuningLog%"
)
All its supposed to do is check first for the existense of a log file and if it doesn't exist then I want to in the first instance append a title.
But all I get in the log file is " echo is off." If I don't use a variable and just place text there then it works fine.
Can anybody spot the problem? Thanks for reading and for any help!
UPDATE:
I changed my code to. I don't know much about delayed expansion, but I tried this and it happened to work...
if not exist "%TuningLog%" (
setlocal enabledelayedexpansion
set Title=Tuning Parameters Saving Log
set LogTitle=--------- !Title! ---------
echo !LogTitle!>> "!TuningLog!"
endlocal
)
If anyone can provide a reason as to why the first attempt didn't work and this did, then please inform this thread for the purposes of learning. Thank you!
because or early expansion. your variable gets replaced with its value at the moment the block starts. at this time, it is still undefined and thus empty. if you have echo on, you can watch this happening on screen. if you enable delayed expansion, as you did in your second example, it gets only expanded at the moment it is used, so it works as you expect variables to work in "real" programming languages.
EnableDelayedExpansion
causes Variables to be expanded in simple language it causes the system to treat the value of variable and not the variable name itself

Variable in variable batch

Alright, I need help here. I have done this before where you have variable1 (let's say it's eat1=apple), variable2 (this is eat2=orange), and variable3 (appaleorange=apple and orange). I need it to do this:
echo Apple:%eat1%
echo Orange:%eat2%
echo Apple & Orange:%eat1%%eat2%
Now, you can see my problem. That above script wouldn't show the word and, only appleorange. That isn't my script and the reason I need this is because I have multiple variables with numbers in them. I have done this before and I forgot how... I know you can do a call and then multiple %'s.
In this case I want fterm variable to be fterm (not sure how to have it in there and not be a variable) and stermnum as a number that will be changed often on other parts of the script.
My code:
set stermnum=1
call set exsternum=%%fterm%%stermnum%%%
echo Selected term:%stermnum% ^(%exsternum%^)
Does anyone know what to do?
Thanks and sorry it was long :P
~Edit:I found it out... If it helps anyone I did:
call set exsternum=%%fterm%stermnum%%
Sorry for posting this even though I figured it out so fast
The OP appended a solution to the question, but it does not relate to the original question scenario, and it still has a bug.
Here is the OP's solution in terms of the original scenario:
set "eat1=apple"
set "eat2=orange"
set "appleorange=apple and orange"
call echo %%%eat1%%eat2%%%
For the actual code, I believe the OP wants to access an array of variables named fterm1, fterm2, fterm3, etc. And the number suffix is in a variable named stermnum.
call set exsternum=%%fterm%stermnum%%%
If fterm is itself a variable containing the base name of the array, then the solution becomes:
call set exsternum=%%%fterm%%stermnum%%%
But CALL is inefficient - Probably not noticeable with a single CALL, but it becomes painfully slow if executed thousands of times in a loop.
There is a much faster solution using delayed expansion. Delayed expansion must be enabled prior to being used.
Original scenario:
setlocal enableDelayedExpansion
set "eat1=apple"
set "eat2=orange"
set "appleorange=apple and orange"
echo !%eat1%%eat2%!
Actual code, interpretation 1:
setlocal enableDelayedExpansion
REM additonal code ...
set exsternum=!fterm%stermnum%!
Actual code, interpretation 2:
setlocal enableDelayedExpansion
REM additonal code ...
set exsternum=!%fterm%%stermnum%!

Resources