Skip to content

Latest commit

 

History

History
executable file
·
250 lines (168 loc) · 7.07 KB

File metadata and controls

executable file
·
250 lines (168 loc) · 7.07 KB

📤 Copying and Moving Files (COPY, XCOPY, ROBOCOPY)

  • 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:

    1. 🧾 COPY – Simple file copying

    2. 📁 XCOPY – Extended copy with directory support (deprecated)

    3. 🛡️ ROBOCOPY – The robust modern file copier


  1. 📄 COPY: The Basic File Copier

    ✅ Syntax

    COPY source [destination] [/options]

    🔹 Example

    COPY file.txt backup.txt

    Copies file.txt to backup.txt in the current directory.


    📦 Copy to Another Folder

    COPY file.txt D:\Backup\

    If D:\Backup\file.txt already exists, it will be overwritten.


    ⚙️ Common Options

    Option Description
    /A ASCII (default)
    /B Binary file
    /Y Suppress confirmation on overwrite
    /-Y Prompt before overwrite

    Example:

    COPY /Y report.txt \\fileserver\archive\

    ❌ Limitations

    • Can’t copy folders
    • No recursion
    • No retry on failure

    Use it only for basic, single-file operations.


  1. 📂 XCOPY: Directory-Capable Copier (Legacy)

    XCOPY expands on COPY by supporting:

    • Directories
    • Recursion
    • File attribute filtering

    ✅ Syntax

    XCOPY source [destination] [/options]

    🔹 Example: Copy Folder Recursively

    XCOPY C:\Project\* D:\Backup\ /S /I /Y
    Option Meaning
    /S Copy subfolders (excluding empty)
    /E Include empty folders too
    /I Assume destination is a folder
    /Y Overwrite without prompt

    🔍 Include Hidden/System Files

    XCOPY *.* D:\Backup\ /H /S /Y
    Option Meaning
    /H Include hidden + system files

    🚫 Deprecated Status

    Microsoft recommends replacing XCOPY with ROBOCOPY due to:

    • Lack of robust error handling
    • Poor logging
    • No retry mechanism

  1. 🚚 ROBOCOPY: The Reliable File Copier

    Introduced in Windows Vista and available in later versions, ROBOCOPY is a powerful and robust file copying utility.

    ✅ Syntax

    ROBOCOPY source destination [options]

    🔹 Example: Mirror a Folder

    ROBOCOPY C:\Project D:\Backup /MIR
    Option Description
    /MIR Mirror: copy all + delete extras in dest
    /E Copy subdirectories (including empty)
    /Z Copy files in restartable mode
    /R:n Retry n times on failure (default: 1M)
    /W:n Wait n seconds between retries (default: 30)
    /LOG:file Write output to a log file

    ✅ Example: Safe Daily Backup

    ROBOCOPY C:\Logs D:\DailyBackup /E /Z /R:3 /W:5 /LOG:log_copy.txt

    🔄 Example: Move Instead of Copy

    ROBOCOPY C:\Logs D:\Archive /MOV
    Option Action
    /MOV Move files only
    /MOVE Move files and folders

    📊 Built-in Logging and Exit Codes

    ROBOCOPY sets meaningful exit codes, e.g.:

    Code Meaning
    0 No files copied, no errors
    1 Files copied successfully
    2+ Errors or partial failures

    You can check using:

    IF %ERRORLEVEL% GEQ 1 (
        ECHO Robocopy failed
    ) ELSE (
        ECHO Backup complete
    )

🧪 Real-World Scenarios

  1. 🔁 Scenario 1: Daily Report Archiver

    SET SRC=C:\Reports
    SET DEST=\\NAS\Daily
    ROBOCOPY %SRC% %DEST% /E /Z /R:3 /W:2 /LOG:report_copy.log
  2. 📤 Scenario 2: Upload Files to Network Share

    COPY *.csv \\Server\Share\Uploads\ /Y
  3. 🗃️ Scenario 3: Monthly Cleanup and Archive

    FOR %%F IN (*.log) DO (
        MOVE "%%F" "C:\Archive\%%~nF_%DATE:/=-%.log"
    )

🧠 Choosing Between COPY, XCOPY, ROBOCOPY

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

⚠️ Pitfalls and Best Practices

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\"

🧠 Summary

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