290

I have a storage folder on a network in which all users will store their active data on a server. Now that server is going to be replaced by a new one due to place problem so I need to copy sub folders files from the old server storage folder to new server storage folder. I have below ex:

from \Oldeserver\storage\data & files to \New server\storage\data & files.

djvg
  • 11,722
  • 5
  • 72
  • 103
user73628
  • 3,715
  • 5
  • 29
  • 24
  • 3
    I assume you're talking about a Windows environment? – Alan Plum Jun 12 '09 at 12:48
  • 2
    @pluma [Batch files are .bat. Yes, they only run on Windows.](http://en.wikipedia.org/wiki/Batch_file) – Hugo M. Zuleta Mar 25 '15 at 16:13
  • 4
    @HugoM.Zuleta, I'm aware of .bat files. But "batch file" is not necessarily guaranteeing a Windows environment. The use of the term pre-dates Windows (as do .bat files) and I've even seen novices use the term to mean "shell scripts" in *nix environments. – Alan Plum Mar 28 '15 at 16:05
  • 1
    @pluma I agree, and this is mostly because they refer to batch operations done by their OS's scripting tools. – Hugo M. Zuleta Mar 28 '15 at 16:36

11 Answers11

513

xcopy.exe is definitely your friend here. It's built into Windows, so its cost is nothing.

Just xcopy /s c:\source d:\target

You'd probably want to tweak a few things; some of the options we also add include these:

  • /s/e - recursive copy, including copying empty directories.
  • /v - add this to verify the copy against the original. slower, but for the paranoid.
  • /h - copy system and hidden files.
  • /k - copy read-only attributes along with files. otherwise, all files become read-write.
  • /x - if you care about permissions, you might want /o or /x.
  • /y - don't prompt before overwriting existing files.
  • /z - if you think the copy might fail and you want to restart it, use this. It places a marker on each file as it copies, so you can rerun the xcopy command to pick up from where it left off.

If you think the xcopy might fail partway through (like when you are copying over a flaky network connection), or that you have to stop it and want to continue it later, you can use xcopy /s/z c:\source d:\target.

starball
  • 20,030
  • 7
  • 43
  • 238
lavinio
  • 23,931
  • 5
  • 55
  • 71
  • 2
    @Iavinio It's asking file or directory when copying an archive. Is there a way to suppress that ? – Bitterblue Jan 09 '13 at 10:33
  • Hi @mini-me - I found that if it's a directory, you can specify a trailing '/' character and you won't see that message. – DaveDev Feb 04 '13 at 14:09
  • 2
    `/i` - If destination does not exist and copying more than one file, assumes that destination must be a directory. – lavinio Mar 05 '13 at 04:04
  • 1
    How to copy a single file from the directory? – Charan Raju C R Jan 07 '14 at 05:56
  • 17
    To get around the "file or directory" prompt, do the command like so... `echo f | xcopy /s /f srcfile destfile` – wintondeshong Apr 09 '14 at 11:30
  • How to make sure the Xcopy doesn't copy an incomplete file. I mean while another program is coping the file (when its not ready), can Xcopy hold until its finished? – Salindaw Dec 14 '17 at 13:44
  • @Salindaw did you got any answer to your query? – choudhury smrutiranjan parida Apr 10 '18 at 05:26
  • `xcopy /s /y c:\source d:\target` add y for overwriting without stopping the script – Alper Jul 27 '20 at 08:43
  • If you don't want to be bothered by xcopy question about copying a file or a directory, just add * at the end of the file to copy by default as a file (plus, /y to overwrite destination) `xcopy /Y C:\file.txt C:\myfold\file.txt*` – linsock Mar 30 '21 at 12:22
82

My favorite one to backup data is:

ROBOCOPY "C:\folder" "C:\new_folder" /mir

/mir is for mirror. You can also use /mov to move files. It reproduce the exact same folder. It can delete/overwrite files as needed. Works great for me. It's way faster than xcopy / copy. It's built in Windows as well.

Source: http://technet.microsoft.com/en-us/library/cc733145.aspx

Etienne Dupuis
  • 13,548
  • 6
  • 47
  • 58
59

Just to be clear, when you use xcopy /s c:\source d:\target, put "" around the c:\source and d:\target,otherwise you get error.

ie if there are spaces in the path ie if you have:

"C:\Some Folder\*.txt"

but not required if you have:

C:\SomeFolder\*.txt
Morgan
  • 19,934
  • 8
  • 58
  • 84
Ike
  • 1,194
  • 12
  • 18
  • 1
    The quotation marks made the trick ;) thanks I also used /Y to replace the file if it exists :) Example: ´´´xcopy /s "c:\source" "d:\target" /Y``` – Joel Carneiro Dec 16 '18 at 13:48
19

To bypass the 'specify a file name or directory name on the target (F = file, D = directory)?' prompt with xcopy, you can do the following...

echo f | xcopy /f /y srcfile destfile

or for those of us just copying large substructures/folders:

use /i which specifies destination must be a directory if copying more than one file

Community
  • 1
  • 1
wintondeshong
  • 1,257
  • 15
  • 19
  • (echo "f" for files or "D" for directories) – JinSnow Nov 26 '15 at 09:46
  • trying to do this now and it just aint working - I always get prompts when trying to copy one file to another directory. Using Win 10 if that affects it at all echo f | xcopy /f /y "My.dll" "C:\myFolder\My.dll". I've tried it with a combination of other switches to no avail (and capital F) – friartuck Apr 08 '16 at 06:42
18

You may want to take a look at XCopy or RoboCopy which are pretty comprehensive solutions for nearly all file copy operations on Windows.

djvg
  • 11,722
  • 5
  • 72
  • 103
Joey
  • 344,408
  • 85
  • 689
  • 683
  • 3
    RoboCopy seems to be better than XCopy because xcopy asks for file or folder decisions. And I can't turn it down. It MUST be able to work full automatic. – Bitterblue Jan 09 '13 at 10:51
  • @mini-me I know this is very late however I cant find the same relevant switch. I ended up resorting to making it create a blank file with the same name I am copying to and then overwrite it. If the file already exists then it doesnt bother asking if its a file or directory. (If you want auto directory then you can append a '/' to the path and it will do it). Hope this helps future people! – Tom C Apr 03 '13 at 11:12
  • 2
    @Eve For reference, `echo f | xcopy source destination /y` will make it automatic. It assigns all questions an "f" as a response. It will also pass overwrite requests (f is taken as yes, I think). – ndm13 Jun 09 '14 at 02:39
  • [Difference between xcopy and robocopy](https://stackoverflow.com/q/24121046) – djvg Apr 11 '22 at 16:25
11

If you want to copy file not using absolute path, relative path in other words:

Don't forget to write backslash in the path AND NOT slash

Example:

copy children-folder\file.something .\other-children-folder

PS: absolute path can be retrieved using these wildcards called "batch parameters"

@echo off
echo %%~dp0 is "%~dp0"
echo %%0 is "%0"
echo %%~dpnx0 is "%~dpnx0"
echo %%~f1 is "%~f1"
echo %%~dp0%%~1 is "%~dp0%~1"

Check documentation here about copy: https://technet.microsoft.com/en-us/library/bb490886.aspx

And also here for batch parameters documentation: https://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/percent.mspx?mfr=true

Donald Duck
  • 8,409
  • 22
  • 75
  • 99
marcdahan
  • 2,654
  • 25
  • 25
4
@echo off

rem The * at the end of the destination file is to avoid File/Directory Internal Question.

rem You can do this for each especific file. (Make sure you already have permissions to the path)
xcopy /Y "\\Oldeserver\storage\data\MyFile01.txt" "\\New server\storage\data\MyFile01.txt"*
pause

rem You can use "copy" instead of "xcopy "for this example.
David Castro
  • 1,773
  • 21
  • 21
1

You can use esentutl to copy (mainly big) files with a progress bar:

esentutl /y "my.file" /d "another.file" /o

the progress bar looks like this:

enter image description here

npocmaka
  • 55,367
  • 18
  • 148
  • 187
0

Look at rsync based Windows tool NASBackup. It will be a bonus if you are acquainted with rsync commands.

Joey
  • 344,408
  • 85
  • 689
  • 683
nik
  • 13,254
  • 3
  • 41
  • 57
-1

If Bash is available to you, you can simple use the following command with recursive option.

cp -r "C:\Users\sourceFolder\." "C:\Users\destinationFolder"

Which will copy all files and folders contained under sourceFolder insde destincationFolder

Panagiotis Bougioukos
  • 15,955
  • 2
  • 30
  • 47
-2

For Windows Users

  1. Create copyfile.exe OR copyfile.cmd OR copyfile.bat

  2. Add this

#!AutoCopy
xcopy /s C:\folder\from  C:\folder\to
  1. Just open by double click copyfile.exe

OR open cmd type copyfile.bat press enter

GMKHussain
  • 3,342
  • 1
  • 21
  • 19