-
Method 1: Using the
copy
Command -
Method 2: Using the
xcopy
Command -
Method 3: Using the
robocopy
Command -
Conclusion
-
FAQ
Copying files using a Batch Script can be a straightforward yet powerful task, especially for those looking to automate their workflows. Whether you want to back up important files, organize your documents, or simply move items around, understanding how to use Batch Scripts for file copying is essential.
In this tutorial, we’ll delve into the various methods available to copy files to a new directory using Batch Script. By the end of this guide, you’ll be equipped with the knowledge to streamline your file management tasks efficiently. Let’s jump right in and explore how you can harness the power of Batch Scripts to copy files with ease.
Method 1: Using the copy
Command
The simplest way to copy a file in Batch Script is by using the copy
command. This command allows you to specify the source file and the destination directory where you want the file to be copied. Here’s how you can do it:
@echo off
copy "C:\path\to\your\file.txt" "D:\new\directory\"
Output:
In this example, the script begins with @echo off
, which prevents the commands from being displayed in the command prompt. The copy
command is then used to specify the source file located at C:\path\to\your\file.txt
and the destination directory D:\new\directory\
. When executed, this script will copy the specified file into the new directory. If the operation is successful, you’ll see a message indicating that one file has been copied. This method is particularly effective for straightforward file copying tasks.
Method 2: Using the xcopy
Command
For more complex copying needs, the xcopy
command is a robust alternative. It provides additional options, such as copying entire directories and subdirectories. Here’s how you can use xcopy
to copy files:
@echo off
xcopy "C:\path\to\your\file.txt" "D:\new\directory\" /Y
Output:
In this script, the xcopy
command is employed to copy file.txt
from the source path to the destination. The /Y
switch is included to suppress prompts that ask for confirmation before overwriting files. This is particularly useful when you’re copying files to a directory that may already contain files with the same name. The command will execute and confirm that the file has been copied without requiring any additional input from you, making it a seamless process.
Method 3: Using the robocopy
Command
For users who require even more control over their file copying tasks, robocopy
(Robust File Copy) is an excellent choice. This command is designed for reliable copying and can handle large volumes of data efficiently. Here’s an example of how to use robocopy
:
@echo off
robocopy "C:\path\to\your" "D:\new\directory" "file.txt"
Output:
--------------------------------------------------------------------
Total Copied Skipped Mismatch FAILED Extras
Dirs : 0 0 0 0 0
Files : 1 1 0 0 0
Bytes : 1,234 1,234 0 0 0
Times : 0:00:01 0:00:01 0:00:00
Ended : 12/31/2023 12:00:00 AM
In this script, robocopy
is used to copy file.txt
from the specified source directory to the destination directory. Unlike the previous commands, robocopy
provides detailed output, including the number of directories and files copied, skipped, and any failures that may have occurred during the process. This level of detail is particularly beneficial for troubleshooting and ensuring that your file operations are carried out as intended. The command is efficient and can handle interruptions, making it ideal for copying large files or directories.
Conclusion
Copying files with Batch Script is a valuable skill that can help you automate various tasks and improve your productivity. Whether you choose the simple copy
command, the more versatile xcopy
, or the robust robocopy
, each method has its strengths and can be tailored to meet your specific needs. With this guide, you now have the tools to manage your files effectively and streamline your workflow. So go ahead, give these commands a try, and experience the convenience of Batch Scripts for yourself!
FAQ
-
What is a Batch Script?
A Batch Script is a text file containing a series of commands that the Windows command line interpreter can execute sequentially. -
Can I copy multiple files at once using Batch Script?
Yes, you can use wildcards (like*
and?
) in the copy command to copy multiple files at once. -
Is there a way to copy entire directories using Batch Script?
Yes, usingxcopy
orrobocopy
, you can copy entire directories along with their subdirectories and files. -
What happens if the destination file already exists?
If the destination file exists, thecopy
command will prompt you for confirmation unless you use the/Y
switch withxcopy
. -
Are there any limitations to using Batch Scripts for copying files?
While Batch Scripts are powerful, they may not handle very large files or complex operations as efficiently as dedicated file management software.
Enjoying our tutorials? Subscribe to DelftStack on YouTube to support us in creating more high-quality video guides. Subscribe
The Xcopy tool, which stands for extended copy, is a popular command-line utility for copying massive amounts of data in Windows. Compared to the traditional copy
command, Xcopy has more features and switches that give you better control when copying or moving files and folders.
Not a reader? Watch this related video tutorial!
Not seeing the video? Make sure your ad blocker is disabled.
In this guide, you will learn how to use Xcopy for various file copy scenarios. From copying a single file, multiple directories, applying filters or exclusions, and more. By the end, you will have gained the knowledge to use Xcopy in your day-to-day file copying tasks.
Let’s begin!
Prerequisites
To follow along with the examples in this guide, you’ll need the following.
- A Windows (server or client) computer. Xcopy comes built-in to Windows, and you don’t need to download anything else. This guide will use Windows 10 Build 1909.
- Xcopy works on the command prompt or PowerShell, and this article assumes that you already have one open.
Understanding the Xcopy Command
Xcopy is a command-line utility, which has been available out of the box since Windows 98. You can find this tool under the %WINDIR%\system32 folder with the executable name xcopy.exe
.
Compared to the Windows copy
command, Xcopy is much more efficient in copying files and directories. In addition, Xcopy has more options that make it more customizable and lets you control the file copy behavior.
Benefits
There are several benefits or advantages of using Xcopy that you will learn as you progress in this guide. But below are some of the benefits of using Xcopy.
- Faster copy operation on large sets of files and directories.
- Simplifies application deployment.
- Can replicate the source directory structure as is.
- Copy files while retaining the owner and access control list (ACL) information.
- Copy and overwrite read-only files.
- Can exclude files based on the file name, extension, or path.
- Can identify updated files, which is useful for differential backups.
- Integrate and use with scripts.
Limitations
As awesome as Xcopy is, it is not without limitations. Although these limitations may not be a deal-breaker, knowing what they are would be good before diving in.
- Cannot copy open files.
- No support for the Windows Volume Shadow Copy Service (VSS). This lack of VSS support makes Xcopy not suitable for backing up live operating system volumes.
- Insufficient memory error when the length of the path plus file name exceeds 254 characters.
Xcopy Syntax Reference
Xcopy lets you perform various file and folder copy operations. But first, you’ll need to understand its syntax and options. There are a lot of options that would change how Xcopy operates. To help you make sense of these options, the tables that follow will cover them in detail.
The first path designation refers to the source file(s); the second path designation refers to the destination file(s).
xcopy [source] [destination] [options]
[source]
– Pathname for the file(s) to copy (accepts wildcards). You should specify the drive, path, and the files you would want to copy.[destination]
– Pathname for the new file(s). If you don’t specify the destination path, the command will copy the files to the same path as the source.[options]
– Can be any of the following options in the tables below. You can also view these options by running thexcopy /?
command or visiting the Xcopy online documentation.
Source Options
Switch | Explanation |
/A | Copy files that have the archive attribute. |
/M | Copy files with the archive attribute set, then switches off the archive attribute at the destination. Use this option when creating routine backups. |
/H | Copy hidden and system files and directories. The default value is N. |
/D:mm-dd-yyyy | Only copies that files with modification date on or after the given date. Without using this option, the command will copy only the files whose date/time is newer than the destination. |
/U | Only copies the files that already exists at the destination. |
/S | Copies folders and subfolders recursively excluding the empty one. |
/E | Copies folders and subfolders recursively including the empty one. |
/EXCLUDE:file1[+file2][+file3]… | Specify the text file(s) containing a list of strings to match absolute file paths to exclude. |
Destination Options
Switch | Explanation |
/I | If the destination does not exist, using this option assumes that the destination is a directory and creates it. If you omit this option, the command prompts to confirm if the destination is a directory or a file. |
/R | Overwrites read-only files. |
/T | Creates a recursive folder structure at the destination without copying the files. This option does not include copying empty folders. To include empty folders, use this option together with the /E option. |
/K | Preserves the file attributes during the copy. Otherwise, the command will reset read-only attributes. |
/N | Copies files using their short file names. |
/O | Copies files including ownership and access control list (ACL) information. |
/X | Copies files including file audit settings and ACL information (implies using /O). |
Copy Options
Switch | Explanation |
/W | Prompts for a key press before the copy process can start. |
/P | Prompts (Y/N) before creating each file. |
/Y | Suppresses the confirmation to overwrite destination file if exists. |
/-Y | Prompts to confirm overwriting an existing destination file. |
/V | Verifies that the source and destination file sizes are identical. |
/C | Ignores errors and forces the command to continue copying. |
/B | Copies only the symbolic link but not the link target file. |
/G | Copies encrypted files to a destination that does not support encryption. |
/J | Copies files without using the buffer. This switch useful when copying very large files. |
/Q | Suppresses the display of file names while copying. |
/F | Displays the full source and destination file paths while copying. |
/L | Only displays which files to copy without copying them. This switch can be useful when testing Xcopy commands with multiple options to assess the expected result. |
/Z | Copies files in restartable mode. Using this option, the command gracefully stops copying files during network interruption. The copying will resume after re-estabslishing the network connection. |
Working with Xcopy: Usage Examples
Now that you’re familiar with the Xcopy syntax and options, it’s time to begin actually using it by combining one or more options together. You’ll explore many different scenarios in the following sections to use Xcopy to copy files and folders.
Listing Files to Copy
If you have a file server containing a huge amount of files that you want to copy, perhaps to a backup location, testing out your Xcopy command first would be ideal. One situation when you’d want to test Xcopy is when you’re combining multiple Xcopy options.
Using the /L
switch with Xcopy, you can simulate what would have happened when you issued the command by listing which files it would copy. This way, you can confirm whether your command will copy all the files that you intended.
XCOPY C:\Workarea\XCopyDemo C:\Workarea\XCopyDemoFolder /L
Copying a Single File
The most basic operation you can do with Xcopy is copying a file from a source to a destination without using any options. For example, the command below will copy a text file to a destination directory, and the file name will be the same.
XCOPY C:\Workarea\XCopyDemo\NewFile.txt C:\Workarea\XCopyDemoFolder
You can also copy the source file and change the destination filename by running the command below.
XCOPY C:\Workarea\XCopyDemo\NewFile.txt C:\Workarea\XCopyDemoFolder\AnotherFile.txt
Copying Files to a New Folder
With Xcopy, you can copy files and create the destination directory on the fly. To do so, you need to append the /I
option to the Xcopy command.
For example, the command below copies the files from the C:\Workarea\Demo folder to the D:\Workarea folder. If the destination folder does not exist, Xcopy creates the destination folder using the /I
option.
XCOPY C:\Workarea\Demo D:\Workarea /I
Copying All Files and Folders Recursively
Aside from copying files from one folder to another, Xcopy also lets you copy folders and files recursively. And there are two ways you can do a recursive copy—with and without empty folders.
To copy all files and directories while ignoring empty directories, append the /S
option to the end of the Xcopy command, as shown below.
On the other hand, to include empty directories during the recursive copy, add the /E
option instead to the command.
Including Hidden and System Files
By default, Xcopy does not include hidden and system files in copy operations. But if you need to force Xcopy to include hidden and system files, add the /H
switch to the command.
The command below copies all files recursively, including hidden and system files. This command also ignores errors, creates the destination folders, and overwrites existing files.
XCOPY C:\Workarea\Documents D:\Workarea\Documents /S /H /C /Y
Preserving the Read-Only File Attribute
Another attribute that Xcopy can handle and preserve is the file’s read-only attribute. By default, when Xcopy copies a read-only file, it removes the file’s read-only attribute at the destination. To stop Xcopy from removing the read-only attribute, append the /K
switch to the command.
For example, the command below copies a read-only file to another location, and the resulting destination file will still have a read-only attribute. This command will also overwrite the destination file if the file exists.
XCOPY C:\Workarea\XCopyDemo\NewFile.txt D:\Workarea /I /K /Y
As a side-effect of preserving the file as read-only, Xcopy cannot overwrite the same file in future copy operations. But, you can force Xcopy to overwrite read-only files by adding the /R
switch.
XCOPY C:\Workarea\XCopyDemo\NewFile.txt D:\Workarea /K /R /Y
Filtering Files to Copy
Suppose there are multiple files in the source folder and subfolders you want to copy. Xcopy allows you to input wildcards as a way to filter which files to copy.
For example, the command below copies only the files with the .cs extension recursively from the root of the C: drive to the root of the E: drive. This command will also ignore errors and overwrites existing files without prompting for confirmation.
XCOPY C:\*.cs E:\ /S /C /Y
Excluding Files and Folders from the Copy
A powerful feature of Xcopy is its ability to exclude files from the copy process. To use this feature, you can leverage the /EXCLUDE
switch. This switch accepts the names of the file(s) that contain the exclusion lists.
First, you need to create or have a file containing the exclusion list. For example, you can have a text file called Exclude.txt that contains the following entries. As you can see, the exclusion file can have specific file names, file extensions, and folders as entries.
xyz.txt
Exclude.txt
.pdf
.png
\exclude\
To run Xcopy with exclusions, run the command below and specify the full path of the exclusion file to the /EXCLUDE
switch. This command uses the C:\Workarea\XCopyDemo\Exclude.txt file.
XCOPY C:\Workarea\XCopyDemo C:\Workarea\Backup /S /Y /Exclude:C:\Workarea\XCopyDemo\Exclude.txt
The result? Xcopy runs to copy files but skips the xyz.txt, Exclude.txt, all files with .pdf and .png extensions, and all files under the exclude directory.
Performing a Differential Copy
When you need to backup files from one location to another, Xcopy has an option that lets you perform a differential backup. Instead of copying all files, a differential backup only copies the files where the modified date falls on or after a date that you specify to the /D:m-d-y
switch.
For example, to copy only the files with the modified date on or after April 01, 2021, run the command below. Additionally, this command performs a recursive copy, ignores errors, and overwrites existing files at the destination.
XCOPY C:\Workarea\XCopyDemo C:\Workarea\Backup /D:04-01-2021 /S /C /Y
If you do not specify a date with the /D
option, Xcopy will copy only the source files that are newer than the destination files.
Copying Files Based on the Archive Attribute
Typically, backup programs remove a file’s archive attribute after a backup operation. After modifying a file (i.e., edit and save), Windows automatically resets the files archive attribute.
If you create a script to backup files using Xcopy, you can use the archive attribute to determine whether to copy or backup the file.
To copy files with the archive attribute, you can take advantage of the /A
and /M
switches. Which one of these switches to use depends on whether you want to preserve the file’s archive attribute or not.
The following command performs a recursive copy of files with the archive attribute only. The destination files will retain the archive attribute after the copy due to the /A
switch.
XCOPY C:\Workarea\Demo D:\Workarea\Test /A /S /Y
In contrast, to remove the source file’s archive attribute after copying to the destination, specify the /M
switch instead.
XCOPY C:\Workarea\Demo D:\Workarea\Test /M /S /Y
Retaining File Owner and ACL Information
Imagine creating a backup of an entire profile folder. Each file may have different owners or unique permissions. Should you need to restore those files, you’d want the same owners and permissions intact.
This way, the whole process would seem seamless, and the users will still have the same access to the files they had before. But first, you need to make sure that you’re backing up those files while retaining the existing owners and access.
To copy files and retain the owner and ACL information, add the /O
parameter to the xcopy
command, as shown below.
XCOPY C:\Workarea D:\Work /O /I /Y
Apart from retaining the owner and ACL information, you can also include the files’ audit settings. Copying audit settings can be useful if your organization applies group policy objects (GPO) to audit files.
To include file audit settings when copying files with Xcopy, use the /X
switch. The /X
switch implies the /O
switch, too, so you don’t have to use both switches at the same time.
XCOPY C:\Workarea D:\Work /X /I /Y
Copying Files and Folders Over the Network
Not only can Xcopy copy files between locations on the same or different disks, but it can also copy files over the network. Unfortunately, copying files over the network is not always reliable. Network connections may suffer from short interruptions and, in some cases, total loss of connection.
Luckily, you can run Xcopy in restartable mode. Meaning, even if the copy progress stops due to a network error, the copy can resume after re-establishing the network connection. To run Xcopy in restartable mode, you’ll need to add the /Z
switch to the command.
For example, the command below performs a recursive file copy from the C:\Workarea folder to a network location. The /Z
parameter makes Xcopy run in restartable mode.
XCOPY C:\Workarea "\\SERVER01\Backup" /S /Z
Using the
/Z
switch, Xcopy also displays the file copy progress in percentage.
Copy Files Using Unbuffered I/O
Buffered I/O corresponds to how the operating system buffers disk reads and writes in the file system cache. While buffering speeds up subsequent reads and writes to the same file, it comes at a cost. As a result, unbuffered I/O (or a raw file copy) is the preferred method for copying a large file.
This copy method reduces the file system cache overhead and prevents the file system cache from being easily flushed with large amounts of file data.
To copy large files using unbuffered I/O, you can append the /J
switch to the Xcopy command, as shown below.
XCOPY C:\Workarea\abc.mp4 D:\Temp /I /J
Copying Files with Verification
Like any other tasks, copying files may not always return 100% successful results. Some files may become corrupted during transfer even if there are no visible errors.
With Xcopy, you can use the /V
switch to verify that the destination and source files are identical based on their size after copying. Identical source and destination files indicate that the copy was successful and that file is intact.
The command below copies all files from C:\Workarea\XCopyDemo
to C:\Workarea\Backup
and verifies each file using the /V
switch. The /F
switch displays the source and destination files’ full path.
XCOPY C:\Workarea\XCopyDemo C:\Workarea\Backup /F /H /S /X /V
Copying Encrypted Files
Xcopy also supports copying encrypted files to destinations that do not support encryption. Using the /G
switch, Xcopy copies encrypted source files and creates decrypted destination files.
This copy mode is helpful, especially when backing up encrypted files to network shares or non-Encrypting File System (EFS) volumes.
Xcopy C:\Workarea\XCOPYDemo C:\Workarea\XCOPYDemoFolder /S /G
Copying Folder Structures
In some situations, backup scripts or programs may require you to pre-provision the destination before copying the files from the source. When needed, you can use Xcopy to replicate the source folder structure without the content.
To do so, run the Xcopy command with /T
switch. Using the /T
switch will cause Xcopy to copy only the directory tree structure to the destination but ignores the empty directories.
You can also add the /E
switch to the command to include empty directories, as shown in the example command below.
Xcopy C:\Workarea\XCOPYDemo C:\Workarea\XCOPYDemoFolder /T /E
Common Errors
If something can go wrong, it probably will. The same is the case when you’re copying files regardless of the volume. Below are the common Xcopy errors that you might encounter.
Invalid Number of Parameters
This error typically occurs when the command you’ve entered contains space characters in it. To avoid this error, when typing a source or destination path that contains space characters, you should surround the path with double-quotes.
For example, if the path is C:\Documents and Settings, enter the path as “C:\Documents and Settings”.
Cannot Perform a Cyclic Copy
This error happens if the source directory you are copying also includes the target directory. In this case, Xcopy would enter a loop, causing an error. When using Xcopy, the destination must be a fully independent directory or disk if you copy all directories and subdirectories.
Parse Error
This error occurs when you’ve typed wrong or invalid syntax in the command. As an example, you might encounter this error when you’ve specified your path in quotes and but the ending quotes are missing.
For example, the command below has a missing closing double-quote character in the destination path.
Xcopy C:\Workarea "C:\Workarea\XCopy Demo /S /I /E /Y
Exit Codes
Xcopy returns an exit code for an operation, which you can use to determine if the operation was successful. Exit codes are useful, especially if your task or script takes actions based on the exit code it receives.
The command shell will save exits code in the %ErrorLevel%
variable. Your code can then examine the value of this variable to determine the outcome of the Xcopy operation.
Below is a table that lists all the Xcopy exit codes.
Exit Code | Purpose |
0 | This exit code means that there were no errors. |
1 | This exit code means that Xcopy did not find any files to copy. |
2 | This exit code means that the Xcopy was terminated by pressing CTRL+C. |
4 | This exit code means that an initialization error happened either because of insufficient memory or disk space or because you’ve entered invalid syntax. |
5 | This exit code means that there was a disk write error. |
Scripting with Xcopy
Apart from using Xcopy interactively, you can reap its benefits better if you use it to automate tasks with scripts. Below are a couple of examples that demonstrate how you can use Xcopy in scripts.
Xcopy Batch Script to Copy Data with Error Handling
This example batch script copies all files recursively with error-handling logic. And based on the error or exit code, the script returns a message showing the reason for the error.
To create the batch file, open a text or code editor of your choice, copy the code below, paste it to your editor, and save the file as copycomplete.bat.
@echo off
rem This batch file copies all source
rem files in all directories and sub-directories from the source
rem path (%1) to the destination path (%2)
xcopy %1 %2 /s /e
if errorlevel 4 goto lowmemory
if errorlevel 2 goto abort
if errorlevel 0 goto exit
:lowmemory
echo Insufficient memory to copy files or
echo invalid drive or command-line syntax.
goto exit
:abort
echo You pressed CTRL+C keys due to which the copy operation has been aborted.
goto exit
:exit
Next, to run the script, invoke its name in the command prompt or PowerShell, followed by the source and destination folders.
pycomplete.bat xcopydemo xcopydemofolder
Xcopy Batch Script to Move Data
Xcopy has no built-in functionality to move files and folders from the source to the destination. But, as a workaround, you can create a script that would Xcopy the files first and then delete the files from the source.
The code below will copy the files to the destination. And if the copy process was successful, the script will delete the files at the source. Copy the code below and save it in a new file called movefiles.bat.
Rem Batch file to move files
Xcopy /D /V %1 %2
Rem If the Xcopy process is successful (0), then delete the files at the source.
if errorlevel 0 (
echo Xcopy process completed successfully
del /Q %1
exit /B
)
Rem Errorlevel is not 0
echo The copy process has failed.
Next, to execute the movefiles.bat batch file, invoke its name in the command prompt or PowerShell followed by the source and destination paths, as you did in the previous example.
movefiles.bat xcopydemo xcopydemofolder
Conclusion
If your job involves copying files in bulk or creating file backups, Xcopy is an excellent tool to help make your job easier. Xcopy delivers accurate and fast file copy results with many options to customize its behavior to fit your requirements.
-
Home
-
News
- Create Script To Copy Files From One Folder To Another On Win10
By Sarah |
Last Updated
The batch script, also known as batch file, actually refers to a list of several commands; whenever you double-click the file, the commands will be executed. If you want to copy files or folders from one folder to another automatically, creating a batch script is a nice choice. As for how to get the work done, please keep reading.
Windows Script/Batch File
When managing your data on PC, you may need to copy or move files and folders. The permissions set on files and folders may be changed when you move/copy them by using the Windows Explorer. Did you know this?
Windows Explorer Needs To Be Restarted: Problem Solved.
For instance, when you copy a folder/file in an NTFS partition or from one partition to another, the Windows system will regard it as the new folder/file; in this way, you will become the CREATOR OWNER since the system will take on the permissions. That’s why using script to copy files from one folder to another is necessary.
What Is Batch File
A batch file, also known as batch script, is actually a list of commands that will be run in the Command Prompt (CMD) tool. Users can create a batch file to let CMD copy and paste files for them automatically; all they need to do is double clicking on the batch file/script and waiting.
Can You Use Script to Copy Files from One Folder to Another
The Windows clipboard provides an easy way for users to copy and paste files/folder manually. The ordinary copy process seems pretty simple: select the file/folder that needs to be copied and choose copy option; open the destination; select the paste option.
Yet, do you want to automatically move files from one folder to another? The copying and pasting process can be finished quickly by using the Windows command line.
How to create batch file to copy files? How to use Windows script to copy files? Generally, it contains two stages: create batch file and create a task to run the batch file to copy files from one folder to another.
Stage One: Create Batch File to Copy
You can create a batch file to copy data in a file from one place to another place on your computer automatically. The following steps are finished on Windows 10.
- Open Windows search.
- Type notepad.
- Open the Notepad app.
- Copy & paste the script into the new Notepad file.
- Select File -> Save As.
- Give it a name.
- Select All Files.
- Click Save to finish creating the batch file.
Step one: click on the Cortana icon on the taskbar (fix taskbar not working on Window 10).
Step two: type notepad into the search text box.
Step three: select Notepad app from the search result by clicking on it.
Step four: type the following script in or copy & paste it into Notepad.
@echo off
set X=<days>
set “source=C:\<Source Folder Path>”
set “destination=D:\<Destination Folder Path>”
robocopy “source” “destination” “filename with extension” /z
exit /b
- The source folder, destination folder, and number of days can be changed due to your needs.
- The nunber of days refers to the time interval you want to archive your folders.
- “<>” is necessary if the source or desitination folder path includes space.
Please click here to know more about robocopy.
Step five: choose File option from the menu bar and choose Save As… from submenu.
Step six:
- Give it a name and type in the File name text box; the file name format is <filename>.BAT.
- Select All Files for Save as type option to change the default file type.
- Click on the Save button at the bottom to finish this step.
Stage Two: Create Task to Run Batch File
After finish this stage, you can use the batch file to copy folder to another location.
Step one: type task scheduler into the Cortana search box.
Step two: choose Task Scheduler from the search result.
Step three: find Actions section from the right-hand panel.
Step four: select Create Task… from the list.
Step five: give a name for the task and type it into the text box after Name.
Step six: shift to Triggers tab and click on the New… button.
Step seven: define the frequency to trigger the task, choosing from One time, Daily, Weekly, and Monthly.
Step eight: set the Start time to decide when the task will be triggered and click on the OK button to confirm.
Step nine: shift to Actions tab and click on the New… button.
Step ten: click on the Browse button to find & choose the .BAT file you have created in stage one.
Step eleven: click on the OK button to confirm. Then, you can close the Task Scheduler and start to move file from one folder to another by using it.
That’s how to use Windows batch to copy file. Besides, you can also use Xcopy or Robocopy to copy files and folders on your PC.
About The Author
Position: Columnist
Sarah has been working as an editor at MiniTool since she graduated from university. Sarah aims at helping users with their computer problems such as disk errors and data loss. She feels a sense of accomplishment to see that users get their issues fixed relying on her articles. Besides, she likes to make friends and listen to music after work.
To copy files in the Command Prompt (cmd), use the `copy` command followed by the source file path and the destination path; for example, to copy a file named «example.txt» from the Documents folder to the Desktop, you would use the following command:
copy C:\Users\YourUsername\Documents\example.txt C:\Users\YourUsername\Desktop\
Understanding CMD
What is CMD?
The Command Prompt (CMD) is a powerful command-line interface in Windows. It allows users to interact directly with the operating system by typing commands instead of using the traditional graphical user interface. CMD can be a vital tool for system administrators, developers, and anyone looking to perform tasks more efficiently or automate processes.
Opening CMD
To open the Command Prompt, follow these simple steps:
- Press Win + R to open the Run dialog.
- Type `cmd` and press Enter.
- For more advanced tasks, you may want to run CMD as an administrator. To do this, type `cmd` in the Windows search bar, right-click on Command Prompt, and select Run as administrator.
How to View Files in Cmd: A Simple Guide
Basic Copy Command Structure
CMD Copy Command Syntax
The fundamental syntax for copying files in CMD is straightforward:
copy [source] [destination]
- [source]: The path of the file you want to copy.
- [destination]: The path where you want to copy the file.
Understanding this syntax is crucial for executing copy commands effectively.
How to Display Files in Cmd: A Quick Guide
Copy a File from One Location to Another
Copying a single file from one location to another in CMD is simple. For instance, to copy a file named `file.txt` from the Documents folder to a Backup folder on another drive, you can use the following command:
copy C:\Users\YourName\Documents\file.txt D:\Backup\File.txt
In this example:
- The first part (`C:\Users\YourName\Documents\file.txt`) specifies the source file.
- The second part (`D:\Backup\File.txt`) indicates where to copy the file. If the filename is omitted in the destination, CMD will use the same name as the source.
Copy Multiple Files
Using Wildcards
To copy multiple files at once, you can utilize wildcards. The asterisk (*) acts as a placeholder for any character(s), which allows you to select multiple files efficiently. For example, to copy all `.txt` files from the Documents folder to the Backup folder, you can execute:
copy C:\Users\YourName\Documents\*.txt D:\Backup\
Here, all `.txt` files matching the wildcard will be copied to the specified destination.
Copy Files in Windows CMD: Different Scenarios
Copy within the Same Drive
When copying files within the same drive, the process remains the same. For instance, if you wanted to copy `file.txt` from one folder to another within the same drive, you could use:
copy C:\Users\YourName\Documents\file.txt C:\Users\YourName\Desktop\
This keeps the operation straightforward, with the source and destination both on the C: drive.
Copy Across Different Drives
Copying files across different drives is also quite simple. For example, if you are copying from drive C: to drive D:, just ensure you specify both the source and destination paths:
copy C:\Users\YourName\Documents\file.txt D:\Backup\
This command will effectively shift the specified file from one drive to another.
How to Run Py File in Cmd: A Quick Guide
Advanced Copy Techniques
Using xcopy Command
The `xcopy` command stands out as a more advanced utility for copying files and directories, especially when it comes to copying large amounts of data or entire directory trees. The syntax is similar but includes options for handling directories, attributes, and more.
For example:
xcopy C:\Users\YourName\Documents\* D:\Backup\ /s /i
- The `/s` flag copies directories and subdirectories except empty ones.
- The `/i` flag assumes that the destination is a directory if you’re copying multiple files.
Using robocopy Command
For even more powerful file copying capabilities, consider using `robocopy`, a robust command-line tool that’s included with Windows. It’s designed for reliable and efficient large-scale copying.
An example using `robocopy` would be:
robocopy C:\Source D:\Destination /E
- The `/E` option copies all subdirectories, including empty ones.
- `robocopy` offers additional flags for mirroring directories, retrying failed copies, and more, making it great for backup scripts.
How to Copy on Cmd: A Quick Guide
Error Handling and Common Issues
Understanding Copy Failures
Sometimes your copy commands may fail due to various reasons:
- File Not Found: Ensure the specified source file exists.
- Invalid Destination: Check if the path for the destination is accurate.
- Insufficient Permissions: You may need administrative rights if the files are in a protected directory. If encountered, running CMD as an administrator can often solve this.
Permissions Issues
Windows folder permissions can also prevent file copying. If you run into permission issues, make sure that you have access rights to the folders you’re working with. Running CMD as an administrator is often the easiest way to overcome these challenges.
How to List Drives in Cmd: A Quick Guide
Best Practices for Using CMD to Copy Files
Organizing Your Commands
When copying files regularly, maintaining organized commands and scripts can greatly enhance efficiency. Make sure to comment your commands and use clear naming conventions.
Using Batch Files
For users who execute copy operations repetitively, creating a batch file can save time. A batch file allows you to write a series of commands that can be executed in one go. Here is a simple example:
@echo off
copy C:\Users\YourName\Documents\file1.txt D:\Backup\
copy C:\Users\YourName\Documents\file2.txt D:\Backup\
This batch script will copy `file1.txt` and `file2.txt` to the backup folder with a singular command execution.
How to Ping in Cmd Like a Pro
Conclusion
Mastering how to copy files in CMD not only improves your efficiency but also empowers you to perform tasks that might seem daunting in a graphical interface. It’s essential to practice these commands and explore their advanced options to become proficient in using CMD effectively. With the skills you’ve learned, you’re well on your way to becoming a CMD expert!
How to Open a File on Cmd: A Quick Guide
Additional Resources
For further learning and exploration of CMD, consider referring to official Microsoft documentation, online tutorials, and community forums that focus on Windows command-line utilities and best practices.

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.