Windows – using xcopy or copy to back up files

batchwindowsxcopy

I have a local drive that contains a directory per user:

c:\user1\
c:\user2\
c:\user3\

and I have a remote drive (via a share) that contains the same directory tree:

f:\user1\
f:\user2\
f:\user3\

inside each directory can be any number of files but they are all .txt. i've been trying to create a batch file that can iterate on the local directories and copy files newer than X timestamp into the corresponding directory on f:.

is there any way to achieve this using xcopy or copy plus some batch magic?

thanks!

EDIT: to avoid confusion: I need to parse the users directories (can have ANY name), grab the files newer than those copied the last time and copy them to the directory with the same name on F:\

copy . is not what i am trying to do. more like:

for /f "tokens=*" %%a IN ('dir /b c:\users\*') do call dobackup.bat "%%a"

or similar

Best Answer

Robocopy is your friend in this. It has a flag that'll do exactly what you want.

robocopy c:\ f:\ /mir /r:1 /sec

That'll do a mirror copy, retrying open files once before moving on, and will also copy security. The mirror copy will also remove files from F:\ that no longer exist on C:\, which is something xcopy can't do. Also, after the first sync it'll only copy newer files.

If robocopy isn't a possibility for some reason, you can get kind of close with xcopy.

xcopy C:\*.* F:\*.* /S/E/H/D

That'll copy the entire C:\ structure to F:\, copy hidden files, and only copy newer ones. It won't remove files from F:\ that exist in C:\ though.

Edit:

Perhaps this'll do what you want:

 for /D %D in (C:\User*\) do xcopy  C:\User%D\*.* F:\User%D /s /e /d 08-01-2010

That'll iterate over all directories with User in them, and copy files newer than the first of August.