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.
Related
I just started coding with Batch a couple weeks ago, a couple hours a day while at work.
I'm writing a script to auto-launch and auto-create dated files. I thought it would be nice to try something "fun".
What I'm trying to do is have my main script save a configuration file with the Batch color code to call upon later/first start and change itself to the previously saved color code. Preferably with a text file.
I've tried using the global variable thing. I think our IT has that disabled as I cannot share variable states between scripts. I haven't tried to enable it. I should also mention we are on Windows 7.
I have since gone the route of creating a colors.bat with the below example code within. I have applied the redirection to my main script to save the file after asking the user for a color code that gets applied to a variable. Right now this Batch file is called upon at a first start in the main script, later I will add a statement to check if the file exists or not.
color 5e
Instead I would prefer a text file that can be called upon or perhaps even a file without a container to prevent backend editing.
However I can't seem to get my script to apply the code inside the text file properly. Below are examples that have failed to apply the color change. Inside the "colors.txt" file is just the Batch compatible color code of "5e" and nothing else.
type colors.txt | color
echo Test
color <colors.txt
echo Test
The above code I have tried with varying placements of the spaces as well.
I've been at this for a few hours now reading up on Batch and Redirection and really anything else of interest. I tried at one point putting the color code as the file name and then trying the Parameter stuff like " %~n1 ". I couldn't figure that out at all.
I hope none of that was confusing. But I'll reiterate, the script as it is right now works. I want to use a .txt container instead of .bat or better yet no container at all. Or even get the global variables enabled, whichever is the best route. I want to call upon this file at script start to apply the color code.
If you have any other questions let me know.
Thanks.
It is fairly straight forward
#echo off
setlocal enabledelayedexpansion
if exist colors.txt (
goto setcolor
) else (
set /p color="Choose color: "
echo !color! > colors.txt
attrib colors.txt +r
endlocal
goto setcolor
)
:setcolor
for /F %%i in (colors.txt) do color %%i
echo Test
pause
The above code check if colors.txt exists, if not it will prompt you for a preferred colour and background colour. Save the code to file and then set it, next time the file will be found and it will just set the colour without re-prompting. We also set attrib +r to make file read-only, if you want to prevent people from editing it, there are ways around it though.
EDIT:
enabledelayedexpansion do cmd /? and you will see
/V:ON
Enable delayed environment variable expansion using ! as the
delimiter. For example, /V:ON would allow !var! to expand the
variable var at execution time. The var syntax expands variables
at input time, which is quite a different thing when inside of a FOR
loop.
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%%
I'm scheduling a Job using a batch file (bat) but I don't know how to set a parameter that the job needs.
hope someone can help me on this!
My Batch file is:
#echo off
set Pentaho_Dir="C:\Transformations&Jobs"
set Pentaho_Job=Interfases_MI.kjb
set path="C:\Program Files\Pentaho\data-integration";%path%
Kitchen.bat /file:%PENTAHO_DIR%\%Pentaho_Job% /log:%PENTAHO_DIR%\Logs\Interfases_MI.log /level:basic
Since you keep which parameter value is failing to yourself, we resort to guesswork.
I'd try
set "Pentaho_Dir=C:\Transformations&Jobs"
set "Pentaho_Job=Interfases_MI.kjb"
set "path=C:\Program Files\Pentaho\data-integration;%path%"
Kitchen.bat /file:"%PENTAHO_DIR%\%Pentaho_Job%" /log:"%PENTAHO_DIR%\Logs\Interfases_MI.log" /level:basic
The set "var=value" syntax ensures that any trailing spaces on the batch line are not included in the value assigned to var. This may not be a problem in the current case, but the resolution of your code would be
Kitchen.bat /file:"C:\Transformations&Jobs"\Interfases_MI.kjb /log:"C:\Transformations&Jobs"\Logs\Interfases_MI.log /level:basic
Whereas cmd would be reasonably happy with the quotes in odd places, kitchen.bat may not be.
In windows you can pass the parameters as
/param="_destination_email=test#test.com"
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
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%!