246

I need to make a script that can write one line of text to a text file in the same directory as the batch file.

1
  • Quick and easy one-liner. echo "some text here" >> myfile.txt
    – undefined
    Commented Jun 13, 2022 at 7:35

8 Answers 8

397

You can use echo, and redirect the output to a text file (see notes below):

rem Saved in D:\Temp\WriteText.bat
@echo off
echo This is a test> test.txt
echo 123>> test.txt
echo 245.67>> test.txt

Output:

D:\Temp>WriteText

D:\Temp>type test.txt
This is a test
123
245.67

D:\Temp>

Notes:

  • @echo off turns off printing of each command to the console
  • @ at the beginning of the remaining lines stops printing of the echo command itself, but does not suppress echo output. (It allows the rest of the line after @echo to display.
  • Unless you give it a specific path name, redirection with > or >> will write to the current directory (the directory the code is being run in).
  • The @echo This is a test > test.txt uses one > to overwrite any file that already exists with new content.
  • The remaining @echo statements use two >> characters to append to the text file (add to), instead of overwriting it.
  • The type test.txt simply types the file output to the command window.
0
159

It's easier to use only one code block, then you only need one redirection.

(
  echo Line1
  echo Line2
  ...
  echo Last Line
) > filename.txt
9
  • 12
    OMG so simple & it makes it half-way human which is impressive since scripting is like the intro to the jibberish world of programming
    – gregg
    Commented Jul 8, 2015 at 21:50
  • This didn't work for me, but a variation with the redirection in front of the block ` > filename.txt ( ` did work.
    – willw
    Commented Jun 20, 2019 at 10:46
  • 2
    @willw I suppose, there is an additional closing parenthesis somewhere in your code block.Else it must work, regardless if you put the redirection in front or after the block
    – jeb
    Commented Jun 20, 2019 at 14:49
  • 1
    @jeb Possibly - I no longer have my original test file, so can't be sure. Certainly I have subsequently had to escape closing ) characters as ^), which would tend to support your hypothesis. Still, at least our discussion alerts others to a possible trap.
    – willw
    Commented Jun 21, 2019 at 15:08
  • Change ... to echo ... to make it executable. I don't understand why it doesn't append to an existing file.
    – flywire
    Commented May 31, 2022 at 8:00
33

echo "blahblah"> txt.txt will erase the txt and put blahblah in it's place

echo "blahblah">> txt.txt will write blahblah on a new line in the txt

I think that both will create a new txt if none exists (I know that the first one does)

Where "txt.txt" is written above, a file path can be inserted if wanted. e.g. C:\Users\<username>\desktop, which will put it on their desktop.

3
  • 2
    yes, it's true for both. Sadly your answer doesn't add additional information to the existing answers.(mentioning '%~dp0 would...)
    – Stephan
    Commented Jul 15, 2015 at 7:37
  • to write to a txt in the same folder? Commented Jul 15, 2015 at 21:46
  • to write into the folder, where the batchfile resides (independent of any cd or pushd commands)
    – Stephan
    Commented Jul 16, 2015 at 6:09
16
    @echo off

    (echo this is in the first line) > xy.txt
    (echo this is in the second line) >> xy.txt

    exit

The two >> means that the second line will be appended to the file (i.e. second line will start after the last line of xy.txt).

this is how the xy.txt looks like:

this is in the first line
this is in the second line
5

@echo off Title Writing using Batch Files color 0a

echo Example Text > Filename.txt echo Additional Text >> Filename.txt

@ECHO OFF
Title Writing Using Batch Files
color 0a

echo Example Text > Filename.txt
echo Additional Text >> Filename.txt
3
  • You can use copy con to write a long text
  • Example:

    C:\COPY CON [drive:][path][File name]

    .... Content

    F6

    1 file(s) is copied

2
  • 1
    In a batch file, copy con prompts the user.
    – heringer
    Commented Sep 22, 2017 at 20:48
  • copy con is useless in a batch file. It also requires user input, whether at the terminal prompt or in a batch file, so it does not do anything the question asked.
    – Ken White
    Commented Mar 21, 2022 at 2:04
1
@echo off

echo Type your text here.

:top

set /p boompanes=

pause

echo %boompanes%> practice.txt

hope this helps. you should change the string names(IDK what its called) and the file name

0

My solution is similar to jeb's brilliant answer, but it requires just one additional line to be added to an existing text file so there's no need to add echo in front of each line.

(for /F "tokens=* skip=1" %%F in ('type %~0') do (echo %%F >> %~n0.txt)) & exit
Line1
Line2
Last Line

When saved as "sometext.bat" this will create a file "sometext.txt" with the content:

Line1
Line2
Last Line

Not the answer you're looking for? Browse other questions tagged or ask your own question.