HOWTO · Batch

How to Use Batch File to Write to a Text File

Learn how to create or overwrite a text file with a Batch script, append lines, write variables, and save command output.

On this page

Use the ECHO command with > to create a text file or replace its contents, and use >> to add text while keeping what is already there. These operators work in Windows Command Prompt and in .bat scripts. The examples below show the commands and the text they write to each file.

Creating a New Text File

This script writes one line to myfile.txt in the current working directory:

@echo off
echo This is my first line of text > myfile.txt

The file contains This is my first line of text. If myfile.txt already exists, > replaces its contents. To write somewhere else, use a full destination path, such as C:\Users\YourName\Documents\myfile.txt; ensure that the destination directory already exists. @echo off keeps batch commands from being displayed as the script runs.

Appending to an Existing Text File

Use >> when you want to keep the current contents and add a line at the end. This standalone example first creates a known starting file, then appends a second line:

@echo off
echo First line> myfile.txt
echo Added line>> myfile.txt

After the script runs, the file has First line followed by Added line on the next line. Each later run adds another Added line; it does not replace the earlier text. The initial command uses > to set up the example, so remove it when adapting the script for a file that already contains data.

Writing Multiple Lines to a Text File

Group several ECHO commands in parentheses and redirect the group once:

@echo off
(
echo First line
echo Second line
echo Third line
) > myfile.txt

Each command contributes a separate line. The group is redirected with >, so running it replaces the target file with these three lines. Change > to >> to add the group after existing contents.

Using Variables in Text Files

Define a value with SET, then expand it with percent signs when writing the line:

@echo off
set "myVariable=Hello from Batch"
echo %myVariable%> myfile.txt

The file receives Hello from Batch. The quoted SET form keeps accidental trailing spaces out of the stored value; the quotes are not part of the value. Use >> instead if the variable’s value should be added to an existing file.

Writing Command Output to a Text File

Redirect a command such as DIR to save its listing:

@echo off
dir > directory_list.txt

This creates or replaces directory_list.txt with the listing for the current directory. The entries depend on the computer and working directory, so the file’s contents will vary. Use dir >> directory_list.txt to add a new listing after existing text.

Updating an Existing Text File

Choose the operator based on what “update” means for your task: >> adds new text and preserves existing lines, while > replaces the file’s contents with the text produced by that command. For example, echo Status: complete >> myfile.txt adds a status line. If you need to change a particular line while retaining the rest, first use a file-editing tool designed for that operation. A FOR /F loop that reads and rewrites a file is not a general safe replacement method: it can alter blank lines and text containing Batch special characters.

FAQ

  1. How do I create a text file? Use ECHO with >; the file is created if needed, and existing contents are replaced.
  2. How do I append without replacing existing text? Use >>.
  3. How can I write several lines? Put multiple ECHO commands in a parenthesized block and redirect the block.
  4. Can a line include a variable? Yes. Define it with SET and expand it as %variableName% in an ECHO command.
  5. How do I save a command’s output? Place > or >> after the command to replace or append to the destination file.