Is there a way to copy directories recursively inside a .bat file? Is an example of this available?
-
2you mean xcopy /s ? or the more advanced robocopy? – rene Nov 09 '12 at 19:14
-
This might help you, although this deletes you can easily change to copy instead http://www.daniweb.com/web-development/threads/61479/recursive-delete-using-a-batch-file-on-windows-xp – Georges Chitiga Nov 09 '12 at 19:15
-
See also http://superuser.com/questions/206036/commmand-line-command-to-copy-entire-directory-including-directory-folder-to-a – anre Mar 01 '17 at 12:30
4 Answers
Look into xcopy, which will recursively copy files and subdirectories.
There are examples, 2/3 down the page. Of particular use is:
To copy all the files and subdirectories (including any empty subdirectories) from drive A to drive B, type:
xcopy a: b: /s /e
- 5,373
- 4
- 30
- 51
- 109,978
- 20
- 153
- 183
-
21You probably want the /y flag included too to "Suppresses prompting to confirm you want to overwrite an existing destination file." – Matthew Lock Jul 23 '14 at 08:55
-
12'xcopy' is not a good idea because they are notoriously famous for **Insufficient memory error** . Try using 'robocopy' – Rahul Sep 03 '14 at 22:18
-
6@Rahul Hmm really? I've never seen that, but xcopy *has* certainly been around since the dark ages. You could probably do `Copy-Item -Recurse` in PowerShell instead too. – lc. Sep 04 '14 at 14:14
-
1Unfortunately yes, this behaviour is very common :). If have seen this happening recurrently particularly if you wish to copy large amount of data like gigs of data. – Rahul Sep 04 '14 at 16:38
-
-
11[This](http://ss64.com/nt/xcopy.html) says that xcopy has been deprecated and that robocopy should be used. – Mike H-R Jan 09 '15 at 16:00
-
Also consider adding the `/i` option: "If Source is a directory or contains wildcards and Destination does not exist, xcopy assumes destination specifies a directory name and creates a new directory. Then, xcopy copies all specified files into the new directory. By default, xcopy prompts you to specify whether Destination is a file or a directory." (from [MS online documentation](http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/xcopy.mspx?mfr=true)) – Sue Maurizio Mar 26 '15 at 11:13
-
Just to note that in case that the destination's drive is out of space only some directories/files will be copied without any warning message! – mchar Jan 17 '17 at 14:49
-
Do not forget quotes in path: xcopy /s /e /y "C:\*" "E:\BACKUP\= wow =\*" – Vladimir Sep 13 '18 at 05:06
After reading the accepted answer's comments, I tried the robocopy command, which worked for me (using the standard command prompt from Windows 7 64 bits SP 1):
robocopy source_dir dest_dir /s /e
- 2,568
- 23
- 19
-
6Never knew 'robocopy' is an inbuilt command in windows 7! Thanks for the answer, very helpful and powerful :) – Anmol Saraf Mar 08 '16 at 01:54
-
5Doesn't `/E` imply `/S`? `copy subdirectories, including Empty ones.` – mbomb007 May 11 '16 at 19:32
-
5Hmmm `/S` and `/E` seem to imply opposite things according to the robocopy `/?` help - s is "not empty ones" e is "empty ones". I think you should just choose one. – Stuart Brock Feb 23 '17 at 18:09
-
1For most users, I think it's sufficient to do /e (include empty directories). – phsource Jun 24 '18 at 23:48
-
4Note that `robocopy` returns an exit code of 1 if one or more files were successfully copied. – dougnorton Mar 18 '19 at 16:08
-
the Robocopy help isn't ambiguous at all with the difference between `/S` and `/E` - `/E` says `copy subdirectories, including Empty ones`. It doesn't say *only* empty ones. – LeeM Aug 30 '19 at 08:50
You may write a recursive algorithm in Batch that gives you exact control of what you do in every nested subdirectory:
@echo off
call :treeProcess
goto :eof
:treeProcess
rem Do whatever you want here over the files of this subdir, for example:
copy *.* C:\dest\dir
for /D %%d in (*) do (
cd %%d
call :treeProcess
cd ..
)
exit /b
Windows Batch File Looping Through Directories to Process Files?
-
5Great idea but isn't your program copy all the files from source directories into a single destination directory without preserving the folder hierarchy? – Jean-Francois T. Mar 28 '14 at 04:19
I wanted to replicate Unix/Linux's cp -r as closely as possible. I came up with the following:
xcopy /e /k /h /i srcdir destdir
Flag explanation:
/e Copies directories and subdirectories, including empty ones.
/k Copies attributes. Normal Xcopy will reset read-only attributes.
/h Copies hidden and system files also.
/i If destination does not exist and copying more than one file, assume destination is a directory.
I made the following into a batch file (cpr.bat) so that I didn't have to remember the flags:
xcopy /e /k /h /i %*
Usage: cpr srcdir destdir
You might also want to use the following flags, but I didn't:
/q Quiet. Do not display file names while copying.
/b Copies the Symbolic Link itself versus the target of the link. (requires UAC admin)
/o Copies directory and file ACLs. (requires UAC admin)
- 381
- 2
- 4