Home > Enterprise >  Want to print single digit value in txt file using Batch script
Want to print single digit value in txt file using Batch script

Time:01-19

I am trying to write a single digit value in a txt file using Batch script. Here is my sample code:

set no=1
set flag=7

echo FlagStatus=%flag%>>Output.txt
echo[>>Output.txt
echo Number=%no%>>Output.txt

But this is not working properly. The line with value 1 prints without value and if I use any value other than 1, it doesn't even prints the line Here is the output of this code:


Number=

I am excepting following output:

FlagStatus=1

Number=7

I will be grateful for your help. thanks

CodePudding user response:

With your numbers directly in front of the redirection > you give the command to use a specific output stream, instead of stdout.

echo FlagStatus=1>>Output.txt

is executed as

echo FlagStatus=   1>>Output.txt

This means, redirect the stdout of the echo command to Output.txt.
In case of flag=2 it becomes redirect the stderr of the echo command to Output.txt, but echo does output nothing to stderr at all.

A simple solution is to use a block instead, it's faster, more readable and avoids problems with stream handles.

set no=1
set flag=7

(
  echo FlagStatus=%flag%
  echo(
  echo Number=%no%
) >>Output.txt
  • Related