How can I use a batch file to write to a text file?

FileBatch FileText

File Problem Overview


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.

File Solutions


Solution 1 - File

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

  • 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.

Solution 2 - File

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

(
  echo Line1
  echo Line2
  ...
  echo Last Line
) > filename.txt

Solution 3 - File

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.

Solution 4 - File

    @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

Solution 5 - File

@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

Solution 6 - File

  • 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

Solution 7 - File

@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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Question09stephenbView Question on Stackoverflow
Solution 1 - FileKen WhiteView Answer on Stackoverflow
Solution 2 - FilejebView Answer on Stackoverflow
Solution 3 - FileDarth TaterView Answer on Stackoverflow
Solution 4 - FileOrganView Answer on Stackoverflow
Solution 5 - FileAceView Answer on Stackoverflow
Solution 6 - Filepc43View Answer on Stackoverflow
Solution 7 - FileJohn Caleb GaronView Answer on Stackoverflow