Windows OS

Batch File Copy: A Guide to Copying Files Using Batch Scripts

When it comes to managing files in the digital world, one task that we often find ourselves doing is copying the individual files. It might be to create backups, share files with others, or simply organize our digital assets. There’s a powerful tool at your disposal to automate the file copying tasks: Batch scripts.

The Windows Command Prompt or PowerShell may run a set of commands that are contained in text files known as Batch scripts, also referred to as Batch files. This guide delves into how to create, customize, and use the Batch scripts to copy the files effortlessly.

Syntax:

The basic syntax for copying a file from one folder to another using the Batch script “copy” command is as follows:

copy sourcefile destinationfolder

The “sourcefile” is the path and name of the file that we want to copy. Furthermore, the “destinationfolder” specifies the location of the folder to which we wish to copy the file.

Depending on your unique requirements, you can additionally define other choices and parameters.

Creating a Batch File

To start, open a text editor like Notepad, Notepad++, or any other plain text editor of your choice. Then, write your Batch script by entering a series of commands, one per line, that you want the script to execute. These commands can range from simple file operations like copying or moving the files to more complex tasks involving system settings or running the programs. Once your script is ready, save the file with a “.bat” extension. This extension indicates to Windows that the file is a Batch script. After saving, you can simply double-click the Batch file to run it or execute it from the Command Prompt or PowerShell by navigating to its location and entering the file name.

To run a Batch file, simply double-click it. This action opens a Command Prompt window and runs the script.

Now that we covered the basics, let’s move on to the specifics of copying single files using Batch scripts.

Copying a Single File

Copying a single file using a Batch script is a straightforward process. Batch scripts offer numerous advantages when it comes to copying single files. They make it simple for us to copy specific files from one place to another.

Open a text editor on your Windows PC, such as Notepad or Notepad++, to get started. Now, let’s consider a scenario where we have a file named “important.docx” in our “Documents” folder and we want to create a backup of it in a folder named “Backup”.

We can copy this file by creating a Batch script and writing the following code:

copy "C:\Users\Administrator\Documents\important.docx" "C:\Backup"

“Copy” is the command to copy the files in a Batch script. The “C:\Users\Administrator\Documents\important.docx”: is the source file that we want to copy. It’s located at the specified path and has the name “important.docx.”

Last but not least, “C:Backup” is the folder to which we like to copy the file. The backslash at the end indicates that “important.docx” should be copied into the “Backup” folder.

So, when we execute this command, “important.docx” will be duplicated from its original location in the “Documents” folder to the “Backup” folder on our C drive.

Copying Multiple Files

To employ a Batch script, you may choose a collection of files with related names or extensions by utilizing the wildcard characters. Here’s the syntax to copy multiple files:

copy "source\*.extension" "destination"

Here, “source\*.extension” is the source path and the file specification using a wildcard. The wildcard character (*), which is an asterisk, matches any file with the given extension. The “destination\” is the destination folder where we want to copy the selected files.

For instance, if we want to copy all “.docx” files from a source folder to a destination folder, our Batch script command looks like this:

copy "C:\Users\Administrator\Documents\*.docx" "C:\Backup"

The provided Batch script command which is “copy “C:\Users\Administrator\Documents*.docx” “C:\Backup” copies all files with the “.docx” extension from the “Documents” folder of the “Administrator” user’s directory to the “Backup” folder. This script employs the wildcard character (*) to match any file in the source directory with a “.docx” extension which allows for the efficient copying of multiple files at once.

When we execute this script by double-clicking on the file, the selected “.docx” files will be duplicated into the “Backup” folder.

Also, we can copy the entire folder into another folder using the Batch script command. The “xcopy” command can be used to copy a directory together with any subdirectories:

xcopy "SourceFolder" "DestinationFolder" /E /I

Here, the “/E” switch ensures that all subdirectories are copied, and the “/I” switch assumes that the destination is a folder.

Copying Files with Different Names

When we’re working with Batch scripts, there are scenarios where we might need to copy the files while giving them different names in the destination folder. This could be useful for versioning, archiving, or organizing the files more meaningfully.

Here’s how we can accomplish this task using Batch scripts:

copy "C:\Users\Administrator\Documents\important.docx" "C:\Backup\MyData.docx"

In this script, we used the “copy” command to duplicate the “important.docx” file from the source folder to the destination folder, but we also specified a new name which is “MyData.docx” for the copied file in the destination folder.

This approach allows us to maintain the original file intact while creating a copy with a distinct name in the specified location. It’s a practical way to manage the files with customized naming conventions to suit our needs.

The following image shows that the file is copied to the destination folder with the specified name:

Using the Batch script “copy” command, you can also perform many other file-copying related tasks.

Conclusion

Batch scripting is a valuable skill for anyone who wants to automate the file management tasks in Windows. With the knowledge gained from this guide, you can create Batch scripts to copy the files, directories, and even the entire folder structures. You can efficiently copy the files and directories with the “copy” and “xcopy” commands along with wildcards. Also, the method of copying a file with a different name in the destination folder is explained in this guide.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content