90

I'm trying to copy some directories from a server before I restore from backup (my latest backup was corrupt, so I have to use an older one :( ). I'm in the Windows Recovery Environment and have access to the server's file system G:\ and my backup media C:\. But, since I'm more familiar with Linux, I'm having a bit of trouble with the command line in Windows, specifically robocopy.

I want to copy multiple directories (maintaining the same directory structure) from G:\ to C:\ while excluding others (namely, the Windows and Program Files folders). I can't figure out the syntax for the /XD option. I was hoping to do something like:

robocopy G: C:\backup /CREATE /XD "dir1","dir2", ...

NOTE: I want to clarify that I want to copy the actual files while maintaining the directory structure too. I just checked, and /create only creates empty files. Weird.

6 Answers 6

136

I figured it out with a little trial and error and the /L (to test the command before doing it for real). The command I end up with is:

robocopy G: C:\backup /MIR /XD G:\dir1 "G:\dir 2" G:\dir3 ...

Apparently, including trailing slashes keeps robocopy from parsing the list of directories correctly, so be sure not to include trailing slashes on directory names and remember to put quotes around directories with spaces in the name.

The /MIR option maintains the same directory structure while copying the files.

Edit: After some more research, I improved the command a bit:

robocopy G: C:\backup /MIR /Z /LOG:C:\todaysdate-backup.log /XF *.iso *.log *.au /XD G:\dir1 ...

The additions are as follows:

  • /Z allows the job to be restarted
  • /LOG:<logfile path> is pretty self-explanatory.
  • /XF is being used to exclude certain filetypes so it doesn't take so long
4
  • 1
    You can always use the /? switch to get help information for Windows console commands. There's also the help command, which similar to Linux's man program, but I haven't seen it used by third-party programs (it could be--I am not familiar with how it works, or if that's even possible). Commented Oct 1, 2012 at 22:18
  • Hey, thanks for the comment. I thought there was a help command, but it doesn't seem to be active in the Windows Recovery Environment (I received an error like "No command 'help' found"). Robocopy is a windows default command that replaced xcopy and is not 3rd-party. Commented Oct 1, 2012 at 22:38
  • 1
    I know it's a Windows command and not 3rd party. I just put that in there just for future reference's sake, since you said you aren't as familiar with Windows' command environment. :) Commented Oct 1, 2012 at 23:00
  • 2
    clarification: /MIR (Mirror A to B) destroys any files in B that are not present in A. This is useful when the directory you are copying to already exists, and you want the new version (of B) to be an exact duplicate of A (as it currently exists). Commented May 26, 2018 at 22:47
9

you have to repeat the /XDpart

C:\>robocopy "C:\Users\weberjn\Google Drive" "u:\Google Drive" /e /dcopy:t /copy:DT /r:0 /XD "C:\Users\weberjn\Google Drive\photos" /XD "C:\Users\weberjn\Google Drive\Google Photos"
3
  • No you don't. How this got upvoted is beyond me.
    – Lucero79
    Commented Jun 10, 2020 at 21:40
  • 3
    Repeating /XD works as long as you specify the source and target folders before the exclude parameters, i.e. robocopy /XD foo /XD bar source target raises an error (no source directory specified) whereas robocopy source target /XD foo /XD bar is perfectly fine.
    – CodeManX
    Commented Jul 3, 2020 at 15:37
  • This was the best answer for using relative paths/just folder names. Commented Dec 13, 2023 at 20:28
7

I know this doesn't answer OP's question, but to anyone here from google: XD will fail in a job file if you use quotes.

Bad:

::
:: Exclude These Directories :
::
    /XD     :: eXclude Directories matching these names
        "Temporary Internet Files"

Good:

::
:: Exclude These Directories :
::
    /XD     :: eXclude Directories matching these names
        Temporary Internet Files

Place as many exclusions as you want, line after line, without using quotes (whether there are spaces or not).

The way I discovered this was by using the command line switch /SAVE:myjobname which took the quotes off my quoted directories!

0
3

My solution for this was to create .txt files that contain the files or directories I want to include or exclude. I have these files in a subfolder "rcXcludes" under my "Backup" folder. My method for naming the files is as follows. I preface them with "rc" (for robocopy), then some recognizable notation for the application or part of the file system in the robocopy command, then append "B" or "R" (for Backup or Restore), then "I" or "X" (for Include or Exclude), then "D" or "F" (for Directory or File). I surround each entry with double quotes and a space between entries. An "Include" file can have files or directories, but directories must have a trailing backslash. In an "Exclude" file for directories you do not use a trailing backslash. Any directory entries are relative to the source path in the robocopy command. The entire contents of any of these .txt files must be on one line and not have a carraige return line feed. In my batch file, I use a SET /P command to import the .txt file into a variable. I then use these variables for FILES or after /XF or /XD. For instance, to backup the current user's Chrome profile without copying the entire "Default" folder, I use the following.

rcChromeBIF.txt  
"Bookmarks" "Custom Dictionary.txt" "Extension Cookies" "Favicons" "History" "Login Data" "Preferences" "Top Sites" "Visited Links" "Web Data" "Databases\" "Extensions\" "Local Storage\" "Plugin Data\" "User Scripts\" "User StyleSheets\"

rcChromeBXF.txt  
"Bookmarks.bak" "ChromeDWriteFontCache" "Cookies" "Cookies-journal" "Current Session" "Current Tabs" "Extension Cookies-journal" "Favicons-journal" "Google Profile.ico" "History Provider Cache" "History-journal" "Last Session" "Last Tabs" "Login Data-journal" "Network Action Predictor" "Network Action Predictor-journal" "Network Persistent State" "Origin Bound Certs" "Origin Bound Certs-journal" "QuotaManager" "QuotaManager-journal" "README" "Secure Preferences" "Shortcuts" "Shortcuts-journal" "Top Sites-journal" "TransportSecurity" "Web Data-journal"

rcChromeBXD.txt  
"Application Cache" "Cache" "data_reduction_proxy_leveldb" "Extension State" "File System" "GPUCache" "IndexedDB" "JumpListIcons" "JumpListIconsOld" "Local Extension Settings" "Media Cache" "Pepper Data" "Platform Notifications" "Service Worker" "Session Storage" "Storage" "Thumbnails" "Web Applications"

In the bat file in, say, C:\Backup.

REM ChromeBak.bat
SET chromeprofdir=Google\Chrome\User Data\Default
SET /P rcChrmBIF=<C:\Backup\rcXcludes\rcChromeBIF.txt
SET /P rcChrmBXF=<C:\Backup\rcXcludes\rcChromeBXF.txt
SET /P rcChrmBXD=<C:\Backup\rcXcludes\rcChromeBXD.txt
robocopy "%LOCALAPPDATA%\%chromeprofdir%" "H:\ChromeBackup\%chromeprofdir%" %rcChrmBIF% /E /ZB /COPY:DAT /DCOPY:T /MT:4 /XJ /XF %rcChrmBXF% /XD %rcChrmBXD% /R:10 /W:2 /TBD /NP /V /TS /Log+:"H:\ChromeBackup\ChromeBackup.log"
1

This is an answer adapted on something picked up by Fara Importanta's answer on ServerFault and The Ultimate Guide to Robocopy

It follows from Charlie C's answer on using a file to store exclusions / inclusions. However, instead we leverage off the /JOB switch.

Let us take this example list of exclusions stored in the following file

exclude.rcj

/XF
    *.pyc
    *.pyo
    *.pyd

/XD
    __pycache__
    .pytest_cache

Note how we are able to have multiple switches inside the file and separate the names with new lines for better readability !

Our batch script can then be something along the lines of:

backup.bat

SET _backupDir=backup_path\
SET _excl="%_backupDir%exclude.rcj"

robocopy src_path dest_path /JOB:%_excl%
1
  • This should be the accepted answer as it is the most up-to-date! It includes even a way to mimick rsync which allows for outsourcing exclude-information to another text-file. Moreover, it is shown how to use the SET - command within BATCH-scripts in order to make it a whole lot more readable. Kudos and thx!
    – Andreas L.
    Commented Nov 26, 2021 at 14:28
0

I know this is a very old question but I figured this could help someone here
I know this does not follow the order for Robocopy but it does work
Also these work while in the same location as the source or destination
If you need to add an address juts copy your address and place it over this
%~dp0Source\

rem this will copy any file or image
robocopy "%~dp0Source" "%~dp0Destination" /E /S File*.txt
robocopy "%~dp0Source" "%~dp0Destination" /E /S Filename.txt
robocopy "%~dp0Source" "%~dp0Destination" /E /S Filename.jpg
robocopy "%~dp0Source" "%~dp0Destination" /E /S *.txt

rem This will exclude all files with the name File*
robocopy "%~dp0Source" "%~dp0Destination\" /E /S /XF File*.txt
rem This will exclude all files with the exact name Filename.txt
robocopy "%~dp0Source" "%~dp0Destination\" /E /S /XF Filename.txt
rem This will exclude all files with the exact name Filename.jpg
robocopy "%~dp0Source" "%~dp0Destination\" /E /S /XF Filename.jpg
rem This will exclude all files with the extension .txt
robocopy "%~dp0Source" "%~dp0Destination\" /E /S XF *.txt


here we have 4 folders
`Folder`
`Folder 1`
`Folder 2`
`Folder 3`
`Folder 4`
rem In order for the Move to work your folder must have 2 names ex: Folder 4
rem it will not work with 1 name ex: Folder
rem This is how I made it work with 1 named folders
robocopy "%~dp0Source\" "%~dp0Destination\" /E /S /XD "Folder 1" "Folder 2" "Folder 3" "Folder 4" /move Folder 1
rem This will still move Folder 4

rem This will move and exclude folders 1, 2, and 3
robocopy "%~dp0Source" "%~dp0Destination" /E /S /XD "Folder 1" "Folder 2" "Folder 3" /move Folder 4

rem This will copy and exclude folders 1, 2, and 3 
robocopy "%~dp0Source" "%~dp0Destination" /E /S /XD "Folder 1" "Folder 2" "Folder 3"
1
  • 1
    Welcome to SuperUser! Please do not post new answers to very old questions unless the accepted (or highest voted) answer does no longer work or even is dangerous to the user's system. Commented Sep 20, 2022 at 18:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .