Skip to main content

How to Copy Files to a Remote Computer in Batch Script

Copying files to remote computers is a fundamental task for system administration and data backup. Whether you are deploying software, distributing configuration files, or backing up important documents, batch scripts provide several methods to accomplish this securely.

In this guide, we will cover the built-in xcopy command (for basic copying) and the more robust robocopy tool (for advanced mirroring and logging).

Understanding UNC Paths

Before we begin, remember that Windows identifies network locations using UNC (Universal Naming Convention) paths. A UNC path starts with two backslashes followed by the computer name or IP address, and then the share name.

Format: \\ServerName\ShareName\Folder

Example: \\FileServer01\Backup\MyReport.txt

If the remote computer has hidden administrative shares (like C$), you can access the C drive directly if you have administrator credentials: \\TargetPC\C$\Users\Public\Downloads

Method 1: Using xcopy (Basic Copying)

xcopy is the traditional command for copying files and directory trees. It is simple to use and works on almost every Windows version.

Syntax

xcopy "Source" "Destination" [Options]

Options

  • /s: Copies directories and subdirectories, except empty ones.
  • /e: Copies directories and subdirectories, including empty ones.
  • /y: Suppresses the prompt to confirm if you want to overwrite an existing destination file.
  • /i: If the destination does not exist and copying more than one file, assumes the destination is a directory.
  • /h: Copies hidden and system files.

Example Script

@echo off
setlocal

set "SourceFile=C:\Deploy\AppConfig.xml"
set "TargetPC=Workstation05"
set "DestPath=\\%TargetPC%\C$\ProgramData\MyApp\"

if not exist "%SourceFile%" (
echo Source file not found: %SourceFile%
pause
exit /b 1
)

echo Copying file to %TargetPC%...

xcopy "%SourceFile%" "%DestPath%" /y /i

if %errorlevel% equ 0 (
echo Copy successful!
) else (
echo Copy failed. Check permissions or network path.
)

endlocal
pause

Method 2: Using robocopy (Advanced Copying)

robocopy (Robust File Copy) is a more powerful tool built into modern Windows versions. It is designed for reliable mirroring of directories and can resume interrupted transfers.

Syntax

robocopy "SourceFolder" "DestinationFolder" [Files] [Options]

Key Options

  • /mir: Mirrors a directory tree (equivalent to /e plus /purge). Be careful: this deletes files from destination that are not in source!
  • /e: Copies subdirectories, including empty ones.
  • /z: Copies files in restartable mode (resumes if network drops).
  • /r:3: Retries 3 times on failed copies (default is 1 million!).
  • /w:5: Waits 5 seconds between retries (default is 30 seconds).
  • /log:file.txt: Writes status to a log file instead of console.

Example Script: Mirroring a Folder

This script mirrors a local "Deploy" folder to a remote machine, ensuring the remote folder looks exactly like the source.

@echo off
setlocal

set "SourceDir=C:\Deploy\Configs"
set "RemotePC=Server01"
set "RemoteDir=\\%RemotePC%\C$\AppConfigs"

if not exist "%SourceDir%\" (
echo Source directory not found: %SourceDir%
pause
exit /b 1
)

echo Starting sync to %RemotePC%...

robocopy "%SourceDir%" "%RemoteDir%" /mir /z /r:3 /w:5

:: Checking Robocopy Exit Codes
:: 0-7 = varying degrees of success, 8+ = failure
if %errorlevel% leq 7 (
echo Success - Exit Code: %errorlevel%
) else (
echo Failure or Serious Errors - Exit Code: %errorlevel%
)

endlocal
pause
tip

Robocopy Exit Codes: Unlike other commands, robocopy exit code 1 means "One or more files were copied successfully", which is good! Anything up to 7 usually indicates success (with some warnings or skipped files). 8 or higher is a failure.

Mapping a Network Drive First (Optional)

Sometimes accessing a UNC path directly might fail due to authentication issues if you are not on a domain. In such cases, map a network drive with credentials first.

@echo off
setlocal

net use Z: \\Server01\Share /user:Username Password >nul 2>nul
if %errorlevel% neq 0 (
echo Failed to map network drive. Check credentials and share path.
pause
exit /b 1
)

copy "C:\local.txt" "Z:\"

net use Z: /delete >nul 2>nul

endlocal

(Using plain text passwords in scripts is generally discouraged but sometimes necessary for ad-hoc tasks).

Troubleshooting "Access Denied"

If you get an "Access is denied" error:

  1. Verify you have permission on the target share.
  2. If copying to administrative shares (C$, D$), ensure you are running the script as a user with local admin rights on the target machine.
  3. Check firewall settings on the target (ensure "File and Printer Sharing" is allowed).

Summary

  • Use xcopy for simple, one-off file transfers because its syntax is slightly easier.
  • Use robocopy for complex synchronizations, backups, or large file transfers over unstable networks due to its robustness and resume capabilities.