Copy a folder but exclude certain subfolders with robocopy

Use robocopy's /XD switch to skip named directories while copying a whole folder tree for backup.

WindowsFilescopyrobocopyMedium risk
Run: robocopy "C:\Source" "D:\Backup" /E /XD "node_modules" ".git" "temp"

01Command

Command
$ robocopy "C:\Source" "D:\Backup" /E /XD "node_modules" ".git" "temp"

02What it does

robocopy is ideal for folder backups, and /E copies every subfolder including empty ones. /XD takes one or more directory names and leaves them out of the copy entirely, wherever they appear in the tree. This keeps backups small by skipping caches and dependency folders such as node_modules.

03Risk

RiskMedium risk
!Warning: /XD matches by name anywhere in the tree — a folder named temp anywhere will be skipped; pass full paths to exclude only specific locations.

04Undo

✓No undo needed — this command does not change persistent state.

05Sources

  • Robocopy - Robust file and folder copy
    SS64.com · technical · 2026-09-17
    Evidence: Documents /E (copy subfolders including empty ones) and /XD dirs (eXclude Directories matching given names/paths) in the robocopy switch list.
  • robocopy (Windows Commands)
    Microsoft Learn · vendor-doc · 2026-09-17
    Evidence: Official robocopy reference lists /e (copies subdirectories including empty ones) and /xd <directory>[ ...] (excludes directories matching the specified names or paths).

Last verified: 2026-09-19 · Category: Files & Folders

06Related solutions