How to Copy and Paste With a Batch File

Image Credit: Tom Werner/DigitalVision/GettyImages

You can easily copy and paste files and data using the Windows clipboard manually using a keyboard or mouse, but you can also automate copying and pasting using the Windows command line, sometimes called "cmd." If you're writing a batch file to copy files, you can use the traditional "copy" command or a newer tool called robocopy. There are also tools to let you store text to the Windows clipboard from a batch file or PowerShell script.

Understanding Batch Files

A Windows batch file is a special type of script that can be run by the Windows command line tool, known as cmd. It's essentially a list of commands that you could type into the command prompt all combined into one file in order to automate a particular task.

Batch files usually end with the extension .bat. The files have been around since the days of MS-DOS, Microsoft's predecessor to Windows.

For some purposes, it makes more sense to use another, more powerful scripting language instead of using a batch file. You can use the newer Windows PowerShell environment, which has access to Microsoft's .NET programming framework, or a third-party, cross-platform scripting tool such as Python or Node.

Batch File to Copy Files

If you want to copy files from one place to another in a batch file, you can have cmd copy and paste the file. Use the command simply called "Copy." Generally, put the word copy on one line, followed by the original file and where you want it copied, such as "copy C:\Example\Example.txt C:\Example2\Example2.txt."

Note that the folder you are copying to must already exist or you will get an error message. Use the built-in command "mkdir" or "md" to create a folder if you need to do so.

Copy takes some special arguments, such as "/y" to skip prompts to verify you want to overwrite an existing file. Check the documentation for the version of copy on your computer by typing "copy /?" at the command line.

Robocopy in a Batch File

You can also use a program built in to Windows called Robocopy to transfer files. It works similarly to the copy command but has more features, including to copy attributes of the file like timestamps or to copy entire folders and their contents.

For example, you can type "robocopy C:\Example1\ C:\Example2 /e /copyall" to copy Example1 and its subfolders to Example2 while preserving all file attributes. Check the robocopy documentation from Microsoft to see the full list of options.

Batch File to Copy Data

If you want to copy actual data to the Windows clipboard, you can use the command "clip."

To copy data from a file, type "clip < filename" in your batch file, where "filename" is path of the file. To send output from another command to clip, type "command | clip." This will store the data on the Windows clipboard, overwriting any other data that's there.

You can't directly paste from the Windows clipboard using a batch file, but you can use a PowerShell script and the function "Get-Clipboard" to access the clipboard contents. The corresponding command "Set-Clipboard" lets you overwrite what's already there, similar to clip.

references