How to Create a Batch File That Overwrites a File

By Fred Larrey

Windows Batch is a Windows programming language that lets you write DOS shell commands in a file, and run all the commands at once. One handy DOS shell command is "xcopy." Given two file names as arguments, it copies the first file into the second. It also has a number of command suffixes, including the "/Y" suffix, which suppresses the confirmation prompt that asks if you'd like to overwrite a file, thus allowing you to overwrite any file you'd like.

Step 1

Open Windows Notepad by going to "Start," "Programs," "Accessories," and "Notepad."

Step 2

Type or copy and paste the following lines of Batch code into Notepad (replacing the your_filename's with the actual names of your files):xcopy /Y your_filename1 your_filename2PAUSE CLSEXIT

Step 3

Go to "File," then "Save As," and navigate to the folder where your files are located. Type the name "COPIER.bat" into the file name text box and click "Save." The ".bat" extension lets Windows know that the file you just created is not a text file, but a Batch file.

Step 4

Navigate to the folder where you saved your Batch file and double-click it. A prompt Window will then give you a message that your file was copied. Because of the "/Y" suffix, your file was automatically overwritten. Hit any key to exit the Batch window.