batch-file Creating Files using Batch

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Insert
> Step 2: And Like the video. BONUS: You can also share it!

Introduction

One useful feature of batch files is being able to create files with them. This section shows how to create files using batch code.

Syntax

  • echo (type here whatever you want in the to be) >> (filename)
  • echo (variable name) >> (filename)

Remarks

If a file exists, > will overwrite the file and >> will append to the end of the file. If a file does not exist, both will create a new file.

Also, the echo command automatically adds a newline after your string.

So

echo 1 > num.txt
echo 1 > num.txt 
echo 2 >> num.txt 

will create the following file:

1
2

Not this:

1 1 2

or

1 2

Furthermore, you cannot just modify a single line in a text file. You have to read the whole file, modify it in your code and then write to the whole file again.



Got any batch-file Question?