Robocopy does not copy the root folder and its time stamp

10

5

Robocopy does not copy the root folder and its time stamp - it copies all subdirectories and files (when the appropriate options are set) and there seems to be no option/argument to tell Robocopy you want the root folder itself and its time stamp or attributes to be copied verbatim also.

So say I want I want to copy C:/Brushes

Robocopy will copy all its subdirectories and files into the destination, but not the Brushes folder itself, with all associated attributes and time stamp.

You understand what I am asking? This is pretty basic and Robocopy seems to lack a option for it. People have posed the question before in various forums but as far as I'm concerned, no one has answered the question.

enter image description here

ptrcao

Posted 2012-01-17T23:08:12.107

Reputation: 133

1This is a flyer, based on my experience with rsync, but what if you don't have a trailing slash at the end of your target folder? How about trying to modify the script at command-line level instead of using the GUI? I know for a fact that the command-line tool will do what you need. – None – 2012-01-18T05:07:50.850

@Randolph West: presently I don't have a trailing slash at the end of the target folder. I still can't make Robocopy include the source folder and its attributes, etc. in the actual copy job. Any ideas? – ptrcao – 2012-01-28T17:19:17.073

If there is no solution for robocopy, you could have a look at xxcopy (there are freeware and commercial versions).

– harrymc – 2012-01-29T16:16:11.997

Yep, I understand completely and have added a working solution below. The real problem is that you have to specify all the files and folders in the root NOT to copy. – opsin – 2012-01-31T00:09:48.140

Answers

8

robocopy %1 "C:\DestinationDirectory\%~n1" /E /V /DCOPY:T /LOG:"C:\DestinationDirectory\Copied.txt" /R:10 /W:30 

Drop a folder onto the bat or use in Send To. It will create folder with name & time stamp of source directory and copy all files.

Terence

Posted 2012-01-17T23:08:12.107

Reputation: 81

@hotzen, What does the %~n1 do? – Pacerier – 2015-04-25T07:05:04.660

"Expands %1 to a file name." – hotzen – 2015-04-25T16:27:07.473

Microsoft broke all their links, here has the percent tilde documentation for batch, https://en.wikibooks.org/wiki/Windows_Batch_Scripting#Percent_tilde

– kevinf – 2018-04-05T23:55:07.650

thank you very much, was searching for that magic %~n1 operator – hotzen – 2013-09-07T11:13:06.527

3

I agree with the OP, not being able to copy the root folder is a big shortcoming of robocopy. How about cheating and moving the brushes folder into a temporary folder, performing the robocopy operation on the temporary folder, then moving it back?

Something like:

md "c:\verytemporary" && move "c:\brushes" "C:\verytemporary"||( echo Line 1 error occurred & goto :eof )
robocopy c:\verytemporary d:\ /MIR /DCOPY:T
move "c:\verytemporary\brushes" "c:\" && rd /q "c:\verytemporary"||echo Line 3 error occurred

Jimadine

Posted 2012-01-17T23:08:12.107

Reputation: 832

2

Try md D:\Backup\Brushes && robocopy C:\brushes D:\Backup\Brushes from the command-line, substituting the correct paths.

ctype.h

Posted 2012-01-17T23:08:12.107

Reputation: 827

1Yeah, but you are creating a new folder which will not have the attributes of the original C:\brushes folder...? – ptrcao – 2012-01-28T17:18:43.907

2

As I can see no good and easy solution in any of the answers :

If there is no solution for robocopy, you could have a look at xxcopy (there are freeware and commercial versions).

There are of course many other alternatives to robocopy, some are listed here.
See also Best Free File Copy Utility.

harrymc

Posted 2012-01-17T23:08:12.107

Reputation: 306 093

1xxcopy is a great utility, but the freeware version has a prompt that you must respond to if you using any of a large list of command line switches. Using xxcopy from a command window, this extra prompt is irritating. But it prevents (complicates) use of xxcopy from a batch file at all. – Kevin Fegan – 2014-01-29T22:11:12.933

1

user105198's answer is the half way,

D:\Backup\Brushes && robocopy C:\brushes D:\Backup\Brushes will indeed just create a new folder. You will need to append /MIR and /DCOPY:T to it

With the /mir option, if the destination directory exists, the destination directory security settings are overwritten. Technet

/DCOPY:T :: COPY Directory Timestamps.

combined together, means : the source directory's attributes and timestamps will be mirrored to the created folder.

so:

D:\Backup\Brushes && robocopy C:\brushes D:\Backup\Brushes /MIR /DCOPY:T

user8228

Posted 2012-01-17T23:08:12.107

Reputation:

1

I think you'll find that Robocopy does exactly what you want, provided you tell it to copy folder timestamps (/DCOPY:T). From my experiments, I've found the following (seemingly undocumented) behavior to be true for Robocopy:

All specified properties of the source directory are applied to the destination directory, regardless of whether the destination directory exists, doesn't exist, or has a different name from the source directory.

The specified properties can include (at least) the security information (/SEC) or folder timestamps (/DCOPY:T).

Therefore, in the following example, c:\destination\folder will be assigned the same timestamps as c:\source\folder.

robocopy c:\source\folder C:\destination\folder /E /DCOPY:T

I tested this with the versions of Robocopy included with Windows 7, Windows 8, and the Windows 2003 Resource Kit (/SEC parameter only). Note that only newer versions of Robocopy (XP026 and above) support the /DCOPY:T parameter.

btriffles

Posted 2012-01-17T23:08:12.107

Reputation: 13

0

In theory, using /DCOPY:T should solve all your problems.

But, I've seen this happen, and I'm not sure I'd call it a "bug", but rather a consequence of the sequence of operations (OK, bug).

This command should work to copy all files from C:\Brushes to D:\NEWBrushes, and /DCOPY:T should cause the timestamps of the source folder to be copied to the destination folder:

robocopy "C:\Brushes" "D:\NEWBrushes" /COPY:DAT /DCOPY:T

What I've seen (at least on some versions of Robocopy) is that the command succeeds, but the timestamp of the D:\NEWBrushes folder is set to the current date and time.

So it appears that the /DCOPY:T feature does not work for the "base" (topmost) folder you are copying.

I believe that it is working, but that the timestamp of the destination folder is being "stepped on" by operations that are later in the operation sequence.

What I believe is happening for this command:

robocopy "C:\Brushes" "D:\NEWBrushes" /COPY:DAT /DCOPY:T

is something like this:

  1. Create folder D:\NEWBrushes if it doesn't already exist
  2. Copy the timestamp from C:\Brushes to D:\NEWBrushes
  3. Copy files and sub folders from C:\Brushes to D:\NEWBrushes

It appears that Step 3, the modification of the contents of the folder D:\NEWBrushes causes the timestamp of D:\NEWBrushes to change.

For me, the "workaround" has been to run the robocopy command to copy the files and folders, then run the (same) robocopy command a second time.

So, the first time you run:

robocopy "C:\Brushes" "D:\NEWBrushes" /COPY:DAT /DCOPY:T

all the files and folders will be copied. Then running it again:

robocopy "C:\Brushes" "D:\NEWBrushes" /COPY:DAT /DCOPY:T
  (or)
robocopy "C:\Brushes" "D:\NEWBrushes" /DCOPY:T

The second time robocopy runs, no files or folders within D:\NEWBrushes will be modified, and the /DCOPY:T option will tell robocopy to copy the timestamp from C:\Brushes to D:\NEWBrushes (unless there are folders/files in C:\Brushes that have changed since the first run of robocopy).

Kevin Fegan

Posted 2012-01-17T23:08:12.107

Reputation: 4 077

0

If you combine /IF :: Include the following Files. with /E :: copy subdirectories, including Empty ones. you get (close to) the desired effect:

C:\Temp\Robocopy Source Root>robocopy . "C:\Temp\Robocopy Destination" /E /IF "Brushes*"

ta.speot.is

Posted 2012-01-17T23:08:12.107

Reputation: 13 727

1What do you mean "close to"? What is the limitation you are implying? – ptrcao – 2012-01-31T00:14:59.043

Danger Will Robinson!!! Your suggestion seems to copy every folder in the current folder, not limit it to the folders beginning with "Brushes" – GlennFromIowa – 2013-11-27T15:24:12.187

0

Robocopy isn't really designed to do that easily. However, there is a way.

Root folder contains lets say 50 files and 40 folders. You only want folder X (but you also want it's time and date stamp).

ROBOCOPY c:\ d:\ /XF 50 root file names c:\boot.ini c:\bla.txt etc. /XD c:\windows c:\drivers c:\temp etc. /S /E /SEC /DCOPY:T

The /DCOPY:T tells it to keep the folder time stamps. The /XF specifies which files NOT to copy, /XD specifies which folders to NOT copy. Just don't specify folder c:\X.

The main drawback to this is researching all the folders and file names in the root and specifically calling them all here. If you are doing this a lot on different systems a script could be written.

Working Example

robocopy c:\testing c:\abc /XF c:\testing\Testing.txt /XD "C:\testing\New Folder (2)" "C:\testing\New Folder (3)" /S /E /SEC /DCOPY:T

Root Working Example

robocopy c:\ d:\ /XF c:\Testing.txt /XD "C:\New Folder (2)" "C:\New Folder (3)" /S /E /SEC /DCOPY:T

My test worked, I create a few folders and sub folders, c:\testing was root in this case, c:\abc was root of another drive (obviously they aren't really, but for this purpose it should make sense). The folder I wanted from the root was called "New Folder", as you can see I did not specify it in the /XD. Also, make sure you specify the c:\ or the names you use will be global. In other words if I didn't specify c:\testing in from of the Testing.txt file, it would omit it from ALL of the subfolders being copied. The /SEC will copy all the file security and time stamps.

opsin

Posted 2012-01-17T23:08:12.107

Reputation: 1 401

Hmmm, I suppose you could use /XF c:*.* to exclude all the root files. I'll test that when I'm in front of a machine and not on a phone. – opsin – 2012-01-29T07:28:36.587

Nope, c:*.* is invalid, you'll have to manually list all of the files. – opsin – 2012-01-29T19:34:54.907

-1

I was solving the root folders right now for one really quick and easy backup script with robocopy, there is an example. Should help to anyone:

$Destination="F:\Backup" #change destionation backup drive and foder
$BackupDirs="d:\folder1", "d:\folder2", "d:\folder3\subfolder", "d:\fodler4\subfolder\subsubfodler" #change source folders

#make a loop for every source folder
foreach ($Backup in $BackupDirs) {

#split folder name
$fdname = Split-Path $Backup -leaf

#define folder to create in destionation
$dirtocreate = Join-Path $Destination $fdname

#make directory, yes, some extra script to skip when exist should be in place, but -erroraction solved this in lazy mode :)
md $dirtocreate -ErrorAction SilentlyContinue 

#copy with subfolders and log
robocopy $backup $dirtocreate  /s /nfl /ndl /log:c:\scripts\backup.log

}

Milhauzz

Posted 2012-01-17T23:08:12.107

Reputation: 1

So use this PowerScript and the problem is resolved, right? – Pimp Juice IT – 2017-09-02T20:39:40.987