Windows 10

Batch File To Write Variable To a Text File

In this tutorial, we are going to see how to write variable to a text file by using ECHO. The batch file contains a series of DOS (Disk Operating System) instructions. It allows triggering the execution of commands found in this file.
 

Batch File To Write Variable To a Text File

The following example write two variables “Name” and “Address” to “file.txt”:
 

 

@echo off

SET Name="Alex"
echo %Name% > file.txt

SET Address="Boston"
echo %Address% >> file.txt

Output:
 

  • The > operator is used to overwrite any file that already exists with new content.
  • The >> operator is used to append to the text file (add to), instead of overwriting it.

 

One thought on “Batch File To Write Variable To a Text File

  • Please enlighten me with the simple batch code to open myText.txt on drive A (root), make a change or add something and save it to myTextBackup.txt on drive B and then save it back to myText.txt on drive A, full circle i.e. the two files get updated.

    Reply

Leave a Reply

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