167

I have a folder: C:\Folder1

I want to copy all the contents of Folder1 to another location, D:\Folder2

How do I do this using a batch file?

ctrl-alt-delor
  • 7,506
  • 5
  • 40
  • 52
SCM
  • 2,065
  • 2
  • 14
  • 15

12 Answers12

198

xcopy.exe is the solution here. It's built into Windows.

xcopy /s c:\Folder1 d:\Folder2

You can find more options at http://www.computerhope.com/xcopyhlp.htm

gunr2171
  • 16,104
  • 25
  • 61
  • 88
eHussain
  • 3,297
  • 1
  • 21
  • 19
  • 3
    Hello Hussain, I have tried xcopy /s c:\Folder1 d:\Folder2 command in batch file, but it does't work for me. can you please guide me more. – SCM Jan 05 '11 at 06:57
  • Hi, Might be you don't having that xcopy on your machine.. However you can download batch file from here http://www.brothersoft.com/xcopy-177904.html – eHussain Jan 05 '11 at 09:55
  • 4
    If you want to copy also empty subdirectories you should use /s /e flags. – Ameba Spugnosa Aug 27 '13 at 18:31
  • Doesn't /e automatically include /s? – mghicks Nov 04 '15 at 15:27
  • 21
    `xcopy` is deprecated and fires an 'Insufficient memory' error when file name is longer than 254 characters. Use `robocopy` instead: `robocopy C:\Folder1 D:\Folder2 /COPYALL /E` https://en.wikipedia.org/wiki/Robocopy – Marco Demaio Jul 05 '16 at 16:10
  • If that copies the contents of Folder1, what do you do if you want to copy the folder itself? – Kyle Delaney May 18 '17 at 21:31
  • relative path is also applicable ex `../some-folder` – FindOutIslamNow Apr 30 '18 at 05:04
  • 1
    I just ran this (01/13/2020 on Windows 10) and it worked exactly as required on the original questions and my whole command was 295 characters long. – Xedret Jan 13 '20 at 20:56
53

If you have robocopy,

robocopy C:\Folder1 D:\Folder2 /COPYALL /E

otherwise,

xcopy /e /v C:\Folder1 D:\Folder2
mghicks
  • 1,144
  • 6
  • 16
  • 2
    If that copies the contents of Folder1, what do you do if you want to copy the folder itself? – Kyle Delaney May 18 '17 at 21:31
  • 4
    @KyleDelaney include the source folder name in the destination, e.g. "xcopy /e /v C:\Folder1 D:\Folder2\Folder1\" – mghicks May 22 '17 at 20:17
  • 1
    Note that [RoboCopy uses \ as an escape character](https://ss64.com/nt/robocopy.html) [(CMD does not)](https://superuser.com/q/182454/511411), if you try and pass in a quoted path with a space and an *ending slash* like `"C:\My Folder\"` you may get a nasty surprise. I recommend reading that SS64 page very carefully. For a full trip down the "what is an escape character in CMD" rabbit hole, see [Escaping Double Quotes in Batch Script](https://stackoverflow.com/a/31413730/4975230). – jrh Mar 12 '19 at 19:31
28

I see a lot of answers suggesting the use of xcopy. But this is unnecessary. As the question clearly mentions that the author wants the content in the folder not the folder itself to be copied in this case we can do

copy "C:\Folder1\*.*"  "D:\Folder2"

Thats all xcopy can be used for if any subdirectory exists in C:\Folder1

Community
  • 1
  • 1
Kingzel
  • 427
  • 4
  • 9
  • 1
    This does not work if the folder you are copying to does not yet exist (I believe) so you might want to add `md D:\Folder2` – Mark Deven Dec 02 '17 at 18:12
  • 6
    My `copy` command (Windows 10 command prompt) tells me that the syntax is incorrect. The following would be the correct syntax: `copy "C:\Folder1\*.*" "D:\Folder2"` – j00hi Aug 01 '19 at 09:29
26

if you want remove the message that tells if the destination is a file or folder you just add a slash:

xcopy /s c:\Folder1 d:\Folder2\

ghiboz
  • 7,863
  • 21
  • 85
  • 131
13

RoboCopy did not work for me, and there are some good solutions here, but none explained the XCopy switches and what they do. Also you need quotes in case your path has spaces in it.

xcopy /i /e "C:\temp\folder 1" "C:\temp\folder 2"

Here is the documentation from Microsoft:

XCopy MS Documentation

/s: Specifies to include subdirectories. Excludes empty subdirectories
/e: Copies all subdirectories, even if they are empty
/i: specifies the destination is a folder (Otherwise it prompts you)
Jordan Ryder
  • 2,336
  • 1
  • 24
  • 29
4

Here's a solution with robocopy which copies the content of Folder1 into Folder2 going trough all subdirectories and automatically overwriting the files with the same name:

robocopy C:\Folder1 C:\Folder2 /COPYALL /E /IS /IT

Here:

/COPYALL copies all file information
/E copies subdirectories including empty directories
/IS includes the same files
/IT includes modified files with the same name

For more parameters see the official documentation: https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/robocopy

Note: it can be necessary to run the command as administrator, because of the argument /COPYALL. If you can't: just get rid of it.

Simone
  • 1,418
  • 15
  • 22
2
@echo off
::Ask
echo Your Source Path:
set INPUT1=
set /P INPUT1=Type input: %=%

echo Your Destination Path:
set INPUT2=
set /P INPUT2=Type input: %=%

xcopy %INPUT1% %INPUT2% /y /s
2

On my PC, xcopy and robocopy need also the path to them, i.e. C:\Windows\System32\xcopy.exe

That's why I use simply "copy": copy /y ....\Folder1\File.txt ....\Folder2\

DirtyDog
  • 21
  • 1
2
@echo off
xcopy /s C:\yourfile C:\anotherfile\

This is how it is done! Simple, right?

hampusma
  • 39
  • 1
-1

FYI...if you use TortoiseSVN and you want to create a simple batch file to xcopy (or directory mirror) entire repositories into a "safe" location on a periodic basis, then this is the specific code that you might want to use. It copies over the hidden directories/files, maintains read-only attributes, and all subdirectories and best of all, doesn't prompt for input. Just make sure that you assign folder1 (safe repo) and folder2 (usable repo) correctly.

@echo off
echo "Setting variables..."
set folder1="Z:\Path\To\Backup\Repo\Directory"
set folder2="\\Path\To\Usable\Repo\Directory"
echo "Removing sandbox version..."
IF EXIST %folder1% (
    rmdir %folder1% /s /q
)
echo "Copying official repository into backup location..."
xcopy /e /i /v /h /k %folder2% %folder1%

And, that's it folks!

Add to your scheduled tasks and never look back.

-2

I have written a .bat file to copy and paste file to a temporary folder and make it zip and transfer into a smb mount point, Hope this would help,

    @echo off
    if not exist "C:\Temp Backup\" mkdir "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%"
    if not exist "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\ZIP" mkdir "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\ZIP"
    if not exist "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\Logs" mkdir "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\Logs"
    xcopy /s/e/q "C:\Source" "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%"
   Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\Logs"
    "C:\Program Files (x86)\WinRAR\WinRAR.exe" a  "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\ZIP\ZIP_Backup_%date:~-4,4%_%date:~-10,2%_%date:~-7,2%.rar" "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\TELIUM"
    "C:\Program Files (x86)\WinRAR\WinRAR.exe" a  "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\ZIP\ZIP_Backup_Log_%date:~-4,4%_%date:~-10,2%_%date:~-7,2%.rar" "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\Logs"
    NET USE \\IP\IPC$ /u:IP\username password
    ROBOCOPY "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%\ZIP"  "\\IP\Backup Folder" /z /MIR /unilog+:"C:\backup_log_%date:~-4,4%%date:~-10,2%%date:~-7,2%.log"
    NET USE \\172.20.10.103\IPC$ /D
    RMDIR /S /Q "C:\Temp Backup_%date:~-4,4%%date:~-10,2%%date:~-7,2%"
VinRocka
  • 299
  • 4
  • 15
-3
@echo off
:: variables
echo Backing up file
set /P source=Enter source folder:
set /P destination=Enter Destination folder:
set xcopy=xcopy /S/E/V/Q/F/H/I/N
%xcopy% %source% %destination%
echo files will be copy press enter to proceed
pause
  • 2
    An explanation of your code will benefit not only the asker of this question but future people who stumble across this in search of a solution to the same problem. – Josh Burgess Nov 19 '14 at 15:06
  • 3
    You have several flags that directly contradict other flags. `/S` copies directories and subdirectories except for empty ones, while `/E` copied directories and subdirectories including empty ones. `/Q` does not display the files names while copying, while `/F` displays the full source and destination file names while copying. – SomethingDark Nov 19 '14 at 15:21