Active27 days ago
There are two types of variables in batch files. One is for parameters which can be passed when the batch file is called and the other is done via the set. May 18, 2015 - 2015,1,Jan,1000 I would like to write a windows batch script. Will parse Jan from a text file called data.txt and set it to a variable. This will read the lines of data.txt and set month equal to the fourth token found.
I'm trying to save user input variables from a batch file into a text file. So that some javascript can read the file to use the variables.
This is what I have so far:
This just writes 'ECHO is off' into the
variables.txt
:How would I get the contents of the variables into the file?
Indoors
IndoorsIndoors
1 Answer
I would assume your %GAMEPIN% or %NICKNAME% variables are empty.
When you use 'echo' with nothing by it's side, or an empty variable, you get a response whether the echo is on or off.
As seen here, I declare an empty variable, and when I echo it, since it's empty, it just tells me if echo is on or off.
Thus, make sure your variables are properly declared.
P. KtinosP. Ktinos
Not the answer you're looking for? Browse other questions tagged windowsbatch-fileecho or ask your own question.
Active3 years, 5 months ago
Im trying to see if its possible to read lines from a file into the command window and then write them back out to a file. The tricky part is that anywhere in the file where there is text resembling a variable I would like it to be expanded.
The idea is to have a master batch file where I set the variables then read the default file in and replace the variables with values from the master.
File Contents
My output for the INSTALL_USER_NAME line would beINSTALL_USER_NAME=USER
The problem is that the variables don't seem to be expanded when they are read in and echo'd out to the file.
UPDATE:Solution listed below
RMeyers
RMeyersRMeyers
2 Answers
call
can be used as a kind of eval statement.Try something like
(Untested)
thsths
The order of expansion is roughly as follows:
- Parameters, for example
%1
, or%*
- Normal variable expansion, for example
%var%
- FOR variables, for example
%%V
- Delayed expansion, for example
!var!
- CALL with another round of normal expansion
You can use the CALL trick to expand variables after the FOR variable expansion, but that is relatively slow. Better to use delayed expansion.
You were on the right track when you enabled delayed expansion, but you also need to substitute
dbenhamdbenham!
for %
in your template file. So INSTALL_USER_NAME=%INSTALL_USER_NAME%
should be changed to INSTALL_USER_NAME=!INSTALL_USER_NAME!
. Then everything will work, and it will be faster.8,32744 gold badges2020 silver badges3333 bronze badges