-
File operations are at the heart of automation — whether you’re deploying code, backing up configs, or archiving logs.
Batch scripting provides three main tools for copying and moving files:
-
🧾
COPY– Simple file copying -
📁
XCOPY– Extended copy with directory support (deprecated) -
🛡️
ROBOCOPY– The robust modern file copier
-
-
COPY source [destination] [/options]COPY file.txt backup.txtCopies
file.txttobackup.txtin the current directory.
COPY file.txt D:\Backup\If
D:\Backup\file.txtalready exists, it will be overwritten.
Option Description /AASCII (default) /BBinary file /YSuppress confirmation on overwrite /-YPrompt before overwrite Example:
COPY /Y report.txt \\fileserver\archive\
- Can’t copy folders
- No recursion
- No retry on failure
Use it only for basic, single-file operations.
-
XCOPYexpands onCOPYby supporting:- Directories
- Recursion
- File attribute filtering
XCOPY source [destination] [/options]XCOPY C:\Project\* D:\Backup\ /S /I /YOption Meaning /SCopy subfolders (excluding empty) /EInclude empty folders too /IAssume destination is a folder /YOverwrite without prompt
🔍 Include Hidden/System Files
XCOPY *.* D:\Backup\ /H /S /YOption Meaning /HInclude hidden + system files
Microsoft recommends replacing
XCOPYwithROBOCOPYdue to:- Lack of robust error handling
- Poor logging
- No retry mechanism
-
Introduced in Windows Vista and available in later versions,
ROBOCOPYis a powerful and robust file copying utility.ROBOCOPY source destination [options]ROBOCOPY C:\Project D:\Backup /MIROption Description /MIRMirror: copy all + delete extras in dest /ECopy subdirectories (including empty) /ZCopy files in restartable mode /R:nRetry ntimes on failure (default: 1M)/W:nWait nseconds between retries (default: 30)/LOG:fileWrite output to a log file
ROBOCOPY C:\Logs D:\DailyBackup /E /Z /R:3 /W:5 /LOG:log_copy.txtROBOCOPY C:\Logs D:\Archive /MOVOption Action /MOVMove files only /MOVEMove files and folders
ROBOCOPY sets meaningful exit codes, e.g.:
Code Meaning 0No files copied, no errors 1Files copied successfully 2+Errors or partial failures You can check using:
IF %ERRORLEVEL% GEQ 1 ( ECHO Robocopy failed ) ELSE ( ECHO Backup complete )
-
SET SRC=C:\Reports SET DEST=\\NAS\Daily ROBOCOPY %SRC% %DEST% /E /Z /R:3 /W:2 /LOG:report_copy.log
-
COPY *.csv \\Server\Share\Uploads\ /Y -
FOR %%F IN (*.log) DO ( MOVE "%%F" "C:\Archive\%%~nF_%DATE:/=-%.log" )
| Task | Tool | Why |
|---|---|---|
| Copy a single file | COPY |
Simple and fast |
| Copy folders (legacy) | XCOPY |
Still used in old scripts |
| Copy/move large folders | ROBOCOPY |
Fast, robust, error-handling built in |
| Copy with logs/retries | ROBOCOPY |
Critical for production or backup scenarios |
| Move and archive files | MOVE, ROBOCOPY /MOVE |
Both are valid depending on scope |
| Mistake | Fix / Tip |
|---|---|
Using COPY to copy folders |
Use XCOPY or ROBOCOPY |
Forgetting /I in XCOPY |
It will prompt if destination is ambiguous |
Not using /Y in automation |
May cause batch scripts to hang on prompts |
Not checking %ERRORLEVEL% |
Always trap failures, especially in automation |
| Not quoting paths with spaces | Always quote path variables like "C:\My Docs\" |
| Command | Use Case |
|---|---|
COPY |
Simple file copy |
XCOPY |
Recursive folder copy (legacy) |
ROBOCOPY |
Enterprise-grade file/folder copy/move |
MOVE |
Rename or move files/folders |