Windows 10

Batch File To Write To a Text File

In this tutorial, you’ll learn how to write text to a file in a Windows batch file using the ECHO command and output redirection operators. This technique is ideal for generating logs, creating reports, or writing custom data into a .txt file.

Batch files execute a series of DOS (Disk Operating System) commands, and redirection makes it easy to write data to files from within your script.
 
 

💡 Batch File to Write to a Text File

The following example writes two lines of text into a file named file.txt:

@echo off
echo Hello, Welcome To StackHowTo > file.txt
echo The First eLearning Platform. >> file.txt

📁 Output:
 

 

🔍 Explanation

@echo off ➤ Prevents command echoing in the terminal for cleaner output.

echo Hello, Welcome To StackHowTo> file.txt ➤ Writes the first line to file.txt, overwriting any existing content.

echo The First eLearning Platform.>> file.txt ➤ Appends the second line to file.txt without deleting the first line.
 

📌 Redirection Operators Recap
  • > – Overwrites the file with new content.
  • >> – Appends to the file without deleting existing data.

 

📚 Summary

Using batch scripts to write to text files is a powerful way to automate documentation, reporting, or system logging tasks. The combination of ECHO and redirection makes it simple and effective, even for beginners.
 

Leave a Reply

Your email address will not be published. Required fields are marked *