How do I write a variable value to a text file within a dos batch script?
(Y, JJJ, SERL, and SERIAL are variables in the batch file)
I tried the following:
set SERIAL=%Y%+%JJJ%+%SERL%
%SERIAL% > VAR_1.TXT
%T% > VAR_2.TXT
%A% > VAR_3.TXT
%SERL% > VAR_4.TXT
The files VAR_!.txt, VAR_2.txt, VAR_3.txt, VAR_4.txt are created, but they are empty (0 bytes).
I know this has a simple solution, but it has been 20+ years since I played with batch files (VERY rusty!)
THANKS!
Syntax SET variable SET variable=string SET "variable=string" SET "variable=" SET /A "variable=expression" SET /P variable=[promptString] SET " Key variable : A new or existing environment variable name e.g. _num string : A text string to assign to the variable.
Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.
A batch file can be commented using either two colons :: or a REM command. The main difference is that the lines commented out using the REM command will be displayed during execution of the batch file (can be avoided by setting @echo off ) while the lines commented out using :: , won't be printed.
The /P switch allows you to set the value of a variable to a line of input entered by the user. Displays the specified promptString before reading the line of input. The promptString can be empty.
This method allows long filenames and also stops the trailing spaces from being included in the files (the spaces are to stop other problems, but this method is preferable).
The (
characters are a good practice to avoid another bug in echo
.
>"VAR_1.TXT" echo(%SERIAL%
>"VAR_2.TXT" echo(%T%
>"VAR_3.TXT" echo(%A%
>"VAR_4.TXT" echo(%SERL%
Try using echo
to get the value out?
echo %SERIAL% > VAR_1.TXT
echo %T% > VAR_2.TXT
echo %A% > VAR_3.TXT
echo %SERL% > VAR_4.TXT
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With