Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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


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


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?


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

share|improve this question
Try robocopy. ref – Zoredache Aug 3 '10 at 16:09
XCOPY C:\USER1\*.* F:\USER1\ /E /D Lather, rinse, repeat. – Evan Anderson Aug 3 '10 at 16:12
@Evan, thanks but I need to iterate through ALL the directories (i don't know the names) and copy the files inside newer than x date – Mr Aleph Aug 3 '10 at 16:41
up vote 4 down vote accepted

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.


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.

share|improve this answer
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 3 '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 3 '10 at 16:59
@Mr Aleph, perhaps my edit will do what you're looking for. – sysadmin1138 Aug 3 '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 3 '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 3 '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


share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.