How can I write "exit" word to txt from bat file?

120 views Asked by At

I want to write to txt from bat file

I write this code:

@echo off
cd C:\test_folder
set st1=cd  newfolder/
set st2=mget xxxx*.csv
set st3=exit 0
echo %st1%> test2.bat
echo %st2%>>test2.bat
echo %st3%>>test2.bat

Bu the output is (test2.bat) :

cd  newfolder/
mget xxxx*.csv

and the "exit" word is missed.

How can I do this?

thanks in advance

1

There are 1 answers

0
Stephan On BEST ANSWER

because your resulting line is echo exit 0>>test2.bat - where 0>>test2.bat tries to redirect the INPUT (STDIN) of echo exit to the file. This results in an empty output to the file and the word echo on the screen.
echo exit 1>>test2.bat would redirect STDOUT ("normal" output) of echo exit to the file (which is just the word exit).
Even worse with exit 2: 2 means ErrorStream, which is empty here, so your file would receive nothing at all, but STDOUT still goes to screen.

Whenever your line may end in a number, better use a different syntax:

>>test2.bat echo exit 0

or

(echo exit 0)>>test2.bat

If you have to write several lines, you can even write all of them in one go (instead of opening the file for each file separately, write the line, close the file before opening it again for writing the next line)

>test2.bat (
  echo %st1%
  echo %st2%
  echo %st3%
)