Batch File: Output variable to text file - batch-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

Related

Redirect and apply color from text file

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.

Nesting Variables in batch

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

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.

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

How to get a the directory path from a variable in a cmd batch file

Within my batch file I have a variable that contains a file path:
SET VAR1=C:\Folder1\Folder2\File.txt
I would like to extract on the directory structure and retreive:
C:\Folder1\Folder2\
I have read threads like this where I need to use %~dp0 where 0 I believe is passed as a parameter. I have tried %~dpVAR1 but that doesn't work. How can I get the output I'm looking for, but with a variable containing the file path?
Also, to make matters difficult, I have to perform all of this within an IF condition which means that once the variable is declared, I will need to refer to it with ! instead of % (I have declared setlocal enableextensions enabledelayedexpansion at the beginning of my script to allow for this).
Any help is much appreciated!
Thanks!
Andrew
You are attempting to use parameter expansion syntax on an environment variable - that cannot work. But it is relatively easy to do what you want.
Using a CALL (relatively slow):
(...
call :getPath "!var!" var
...
)
exit /b
:getPath
set "%2=%~dp1"
exit /b
Using FOR, assuming the variable does not contain any wildcards (fast)
(...
for %%F in ("!var!") do set "var=%%~dpF"
...
)
Using FOR, if the variable may contain wildcards (also fast)
(...
for /f "delims=" %%F in ("!var!") do set "var=%%~dpF"
...
)
Note 1: If the variable does not contain the full path, then all the solutions will attempt to resolve the name into an absolute path and will return the full absolute path. For example, if var contains foobar\test.txt, then the solutions will include the full path to the current directory, even if the file is not found. Something like c:\pathToCurrentDirectory\foobar\.
Note 2: All solutions above will remove all quotes from the path.
Note 3: A path could include the ! character, which will cause problems when expanding %~dp1 or %%~dpF because you have delayed expansion enabled. The delayed expansion will corrupt both ^ and ! if the value contains !. There is a solution that involves protecting both ! and ^. Here is a demonstration applied to the last solution above. The protection requires normal expansion, and since you are within a code block, it requires at least one CALL. It could be done without a subroutine, but it is easier with a subroutine. The subroutine assumes the variable is named var.
(...
call :getPath
...
)
exit /b
:getPath
set "var=!var:"=!"
set "var=!var:^=^^^^!"
set "var=%var:!=^^^!%" !
for /f "delims=" %%F in ("!var!") do set "var=%%~dpF" !
exit /b
I do believe (once again) many questions are on the same topic (string constraints, or splitting strings).
Instead of giving you the whole code, I'm going to give you a template and explain why %~dpVAR! didn't work.
Firstly, why %~dpVAR! did't work.
Before I get into modifiers, let's discuss parameters. You may know that batch files can parse parameters to each other. These parameters can be called by using a single percent sign (%) in front of the numbers 0-9. As far as I'm aware (someone might have made a way for more to be parsed), only 9 parameters can be parsed. You may think that is wrong (there's 10 parameters right?). Parameters 1-9 are parsed to the batch file (or function within one), %0 is the file path of the batch file (or function name). If you look, %~dp0 shares some (not really) resemblance to %0. This will be discussed below.
Secondly, the term %~dp0 has modifiers in it. Modifiers are things that modify variables (only in the case of parameters and those declared in for loops, you know the ones with double percent signs like %%i) and parameters. The modifier d expands the parameter to a drive letter only while p expands the parameter to a path only. You may think that these would contradict themselves, but parameters can be combined to create extremely wacky formats.
So, as you can see, you attempt at replacing 0 with your variable name failed because it's not specified for those sort of things.
Now, on to the template.
You can constrain variables (and put them into other variables) like this:
set variable=!variable:~offset,amount!
Don't worry if that seems confusing, I'm about to explain the components.
Firstly, notice that there is no /a switch. This is because this is not a mathematical function (don't really know why I added this). So, before I explain it, here's an example of what it would do to a variable name numbers that has the value of 0123456789.
set numbers=!numbers:~5,1!
By using that line of code, numbers would now equal 5. This is because it is recreating the variable with a smaller version of the original value (gee this is hard to explain). As you can see, there is a 5 where offset was on the template above. This is because it is skipping the first 5 characters and setting the variable as the next amount, or 1 character (I really hope you're getting this).
So basically, it sets a variable as a shorter value of a different (or the same) variable determined by the offset and the amount of characters to contain in it.
I really hope this helps because I probably wouldn't understand a word of this.
Can someone redirect this poor guy to a link explaining this better (I tried, ok!)?
Complete example of extracting paths from variable:
#echo off
set /p Fullpath="Specify full path: "
call :getPath %Fullpath% filename folder
echo %filename%
echo %folder%
pause
exit /b
:getPath
set "%2=%~nx1"
set "%3=%~dp1"
exit /b
Would this work:
SET VAR1=C:\Folder1\Folder2\File.txt
echo %var1%
Where Echo is the name of your exe.
%CD% may work as well: Echo %CD%

Resources