0

The file looks like this:

@echo off

set /a varOne=1
set /a varTwo=2

echo %varOne% %varTwo%>>test.txt

start test.txt

varOne and varTwo can be any number.

My problem is that it doesn't write "1 2" to the test.txt as I'd expect. When I remove varTwo, it looks like this:

@echo off

set /a varOne=1

echo %varOne%>>test.txt

start test.txt

This does do as I'd expect. It writes "1" to test.txt

To allow for both numbers to be written to the file I can just remove the space between both variables. But For my situation, I need a space in between the variables.

How do I write 2 variables with a space in between them to a text file?

Thanks.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Coding Mason
  • 162
  • 1
  • 14

1 Answers1

3

Your script is being read "1 2>>test.txt" so your output is being redirected to stream "2". In batch, in/out streams range from 0-9, so when you use any of those single digits right before a ">" or ">>" then they are being interpreted as in/out streams.

==============
~ Workarounds ~
==============

@echo off

set varOne=1
set varTwo=2

echo ^%varOne% ^%varTwo%>>test.txt
@echo off

set varOne=1
set varTwo=2

(echo %varOne% %varTwo%)>>test.txt

You only need those workarounds for "single digit" values. If you use two or more digits, then you don't need a workaround...

@echo off

set varOne=123
set varTwo=456

echo %varOne% %varTwo%>>test.txt

  • 7
    The safest syntax is to place the redirection at the beginning of the line like so: `>>"test.txt" Echo(%varone% %vartwo%` – T3RR0R Jun 10 '20 at 05:21