Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I write the value of a variable to a dos text file?

Tags:

batch-file

cmd

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!

like image 305
FITZ Avatar asked Apr 08 '14 02:04

FITZ


People also ask

How do you set a variable in DOS?

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.

What does %% do in DOS scripting?

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.

How do I comment in a batch file?

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.

What is P in batch file?

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.


2 Answers

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%
like image 150
foxidrive Avatar answered Sep 28 '22 16:09

foxidrive


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
like image 35
merlin2011 Avatar answered Sep 28 '22 15:09

merlin2011