2

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

Mr Aleph
  • 231
  • 1
  • 5
  • 10

2 Answers2

4

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.

sysadmin1138
  • 131,083
  • 18
  • 173
  • 296
  • Thanks, but I was trying to do something a little more complex than just do a copy c:\*.* f:, i mean I was trying to see whether you can grab the 1st directory, parse the name and copy the newer files to the same directory on f:\. I don't want to copy any other directory but those of the users. maybe i didn't explain myself well – Mr Aleph Aug 03 '10 at 16:44
  • something like: for /f "tokens=*" %%a IN ('dir /b c:\users\*') do call robocopy "%%a" "f:\%%a" /mir /r:1 /sec – Mr Aleph Aug 03 '10 at 16:59
  • @Mr Aleph, perhaps my edit will do what you're looking for. – sysadmin1138 Aug 03 '10 at 16:59
  • @sysadmin1138 thanks, but it's not working. what i have to so is: xcopy c:\users\[directoryname]\*.txt f:\[directoryname] (where directoryname is the same on both drives) – Mr Aleph Aug 03 '10 at 17:56
  • this will list all the directories, how do I grab each directory and copy *.txt from them?: for /D %%a IN (c:\users\*) do call dir %%a\*.txt – Mr Aleph Aug 03 '10 at 18:08
  • OK, if I put the batch file in C:\users\ then for /D %%a IN (*) do call robocopy %%a\ f:\%%a\ *.txt /mir /r:1 /sec --- works – Mr Aleph Aug 03 '10 at 18:27
1

OK, if I put the batch file in C:\users\ then

for /D %%a IN (*) do call robocopy %%a\ f:\%%a\ *.txt /mir /r:1 /sec

works

Mr Aleph
  • 231
  • 1
  • 5
  • 10