DEV Community

Cover image for Windows XCOPY Command: Copy a Folder and All Its Subfolders
arnostorg
arnostorg

Posted on Edited on

Windows XCOPY Command: Copy a Folder and All Its Subfolders

You want a copy of a project folder: every file, every subfolder, the empty logs folder too. COPY only handles files in one folder, so it is the wrong tool. XCOPY copies a whole tree from a normal Command Prompt, with no extra install.

Here is the one line most people need, then the flags that make it safe.

The command

xcopy C:\Projects\WebApp D:\Backup\WebApp /e /i /h /y
Enter fullscreen mode Exit fullscreen mode

What each flag does:

Flag What it does
/e Copies all subfolders, including empty ones
/i If the destination doesn't exist, treat it as a folder (no prompt)
/h Also copies hidden and system files
/y Overwrites existing files without asking

The result: D:\Backup\WebApp is created and gets the full tree from C:\Projects\WebApp.

/s vs /e: where empty folders go missing

/s copies subfolders unless they are empty. /e copies them even when they are empty.

rem Skips empty folders like logs\ or uploads\
xcopy C:\Projects\WebApp D:\Backup\WebApp /s /i

rem Keeps the full folder structure
xcopy C:\Projects\WebApp D:\Backup\WebApp /e /i
Enter fullscreen mode Exit fullscreen mode

If your app expects logs\ to exist at startup, the /s copy can break it. Use /e when you want an exact structure.

The "file or directory?" prompt

Run XCOPY without /i to a destination that doesn't exist yet, and it stops to ask:

Does D:\Backup\WebApp specify a file name
or directory name on the target
(F = file, D = directory)?
Enter fullscreen mode Exit fullscreen mode

That prompt also freezes a scheduled task or batch file that nobody is watching. Two ways to avoid it:

rem Option 1: /i says "it's a folder"
xcopy C:\Projects\WebApp D:\Backup\WebApp /e /i

rem Option 2: a trailing backslash also marks a folder
xcopy C:\Projects\WebApp D:\Backup\WebApp\ /e
Enter fullscreen mode Exit fullscreen mode

Preview first with /l

/l lists what would be copied without copying anything. Use it before a big copy:

xcopy C:\Projects\WebApp D:\Backup\WebApp /e /i /h /l
Enter fullscreen mode Exit fullscreen mode

The last line shows the count, for example 152 File(s). If that number looks wrong, fix the source path before you drop /l.

Copy only what changed with /d

Run the same backup tomorrow with /d, and XCOPY copies only source files that are newer than the copy already in the destination:

xcopy C:\Projects\WebApp D:\Backup\WebApp /e /i /h /y /d
Enter fullscreen mode Exit fullscreen mode

You can also give a date (MM-DD-YYYY format) to copy files changed on or after it:

xcopy C:\Projects\WebApp D:\Backup\WebApp /e /i /d:11-01-2026
Enter fullscreen mode Exit fullscreen mode

Skip node_modules, .git, and build output with /exclude

/exclude takes a text file with one search string per line. Any file whose full path contains one of those strings is skipped.

Create C:\Scripts\skip.txt:

\node_modules\
\.git\
\bin\
\obj\
Enter fullscreen mode Exit fullscreen mode

Then:

xcopy C:\Projects\WebApp D:\Backup\WebApp /e /i /h /y /exclude:C:\Scripts\skip.txt
Enter fullscreen mode Exit fullscreen mode

Watch out: the match is a plain substring of the whole path. A line that says just obj also skips objects.json and any folder named objects. Wrapping the name in backslashes, as above, limits the match to a folder with that exact name.

Check the result in a batch file

XCOPY returns an exit code, so a script can tell a good copy from a failed one:

Exit code Meaning
0 Files were copied without error
1 No files were found to copy
2 The user pressed Ctrl+C
4 Initialization error (bad path, bad syntax, not enough memory or disk space)
5 Disk write error
@echo off
xcopy C:\Projects\WebApp D:\Backup\WebApp /e /i /h /y /d
if errorlevel 4 (
    echo Backup FAILED with code %errorlevel%
    exit /b 1
)
if errorlevel 1 echo Warning: exit code %errorlevel% (1 = no files found, 2 = cancelled).
echo Backup finished.
Enter fullscreen mode Exit fullscreen mode

if errorlevel 4 is true for 4 or higher, so check the biggest code first.

XCOPY or Robocopy?

XCOPY is perfect for "copy this folder tree once" or a simple update copy. When you need retries on a flaky network share, multithreading, or a true mirror that also removes deleted files, use Robocopy. I covered that in Windows Robocopy Command: Mirror a Folder Without Deleting the Wrong Files.

Related CMD tips:

Official reference: xcopy on Microsoft Learn.

Practice it without risking real files

Want to try /s vs /e without touching your own folders? CMD Master's free interactive CMD lessons run in a browser terminal, so you can copy, break, and retry file commands safely. It also covers PowerShell and Bash.

Quick check: a source folder has readme.md, an empty logs\ folder, and a hidden .env file. After xcopy src dst /s /i, what ends up in dst? Answer in the comments.

Top comments (0)