Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

mirror directory tree and preserve directory timestamp

Aug
258
4
I want to mirror a directory tree to different drive/location.
I tried COPYand SYNC but both create the new directories with the actual date&time.
Is there a way to preserve the date&time of the original directory ?
Especially with SYNC I hoped that it would be possible.
For me the timestamp of a directory is a useful information and I don't want to lose it.

btw:
I accidentally used a wrong syntax for @FILES:
Code:
echo %@files[ /s /a:d *.*]
This one stalls and doesn't come back to the prompt.
Of course it should be
Code:
echo %@files[ /s *.*,d]


and even more to complain :p:
It would be nice if SYNC would report the copied directories, too
Code:
>sync /s /a:d .\share-backups c:\temp\xxx\share-backups
SYNC: \\company.net\Net-BER\DV\Share\!w2kadm\share-backups => C:\temp\xxx\share-backups
    0 files copied
SYNC: C:\temp\xxx\share-backups => \\company.net\Net-BER\DV\Share\!w2kadm\share-backups
    0 files copied
 
I don't think that COPY has an option to preserve directory timestamps. So I *think* that you need to COPY the whole tree first, then set directory timestamps with TOUCH. Start from leaf directories and move upwards through the tree, because otherwise Windows resets the write date of a parent directory when you touch the dates of the folders it contains. Roughly coding,
Code:
DIR /A:D /S /F SourceTreeTop\ > DirList.txt
then sort DirList.txt in reverse order and
DO SourceDir in @dirlist.txt (TOUCH /A:D /R:w"%SourceDir%" "%@TargetDir[%SourceDir%]")
where your used-defined function TargetDir maps each source directory path into its corresponding target directory path.
TotalCommander, a file manager program, has an option to copy trees and preserve directory dates.
 
The reason directory timestamps are not copied is that only files are copied, but directories have to be created new on the target system. When you copy a file, in the various file systems which support long file names in Windows, by default the target file's creation date and access date are when the copy is made, and its modification date is copied from the source. Cf. POSIX - the default modification date of the target is the time the copy is made (I always hated that, and always used the option of the cp command which copies the source's modification date to the target. However, every time another file is copied to the target directory, it changes the content of that special file, so logically its modification time is new.

However, you can use the TOUCH command to make the modification dates of each target directory the same as the corresponding source directory in the manner below:

copy /s xxx\ yyy\
for /a:d /r yyy\ %d in (*) touch /a:d /r %@replace[yyy\,xxx\,%d] %d

BEWARE! NOT TESTED! Of particular issue is the possibility that directory names may need to be quoted...
 
Stefano and Steve, thank you for your explanation and sample code.
But somehow I'm not so excited with the TOUCH-solution (for ~ 50000 dirs).
It's a pity, but I will fall back on ROBOCOPY.
 

Similar threads

Back
Top