logo

Difference Between '>' and '>>' in Batch Command 📂Programing

Difference Between '>' and '>>' in Batch Command

Overview

In batch commands, > overwrites a string, while >> appends a string.

Code

For instance, suppose there is a file named example.bat written as follows.

@echo off

echo 1 > A.txt
echo 2 >> A.txt


echo 3 >> B.txt
echo 4 > B.txt

The contents of the resulting A.txt and B.txt upon execution are as follows.

alt text alt text

Trick to Empty a File

echo. > file.txt

alt text

The echo. with a period at the end of the echo command outputs an empty string1. By applying this command in conjunction with the overwriting feature, you can create an empty file.

Environment

  • OS: Windows 11