31 Dec 2024

How to Prevent Robocopy from Hiding Your Files and Find Them When it Does.

I recently had a scary moment in a production environment when I was testing to see how long it would take to copy a data file between two of their disks. I was using Robocopy and an old and relatively small backup file for my test.

If you haven’t heard of Robocopy, then you should look into it as a command line (cmd) tool for copying your files quickly; it is much quicker than copying and pasting larger files around and also provides useful data on the speed of the transfer.

In my case, I was running the copy without specifying any specific attributes, with the expectation that it would just do what it was told and nothing else. Below is an example of what I ran using command prompt.

robocopy I:\DATA\ K:\DATA\ DB_DataFile.bak

I ended up stopping it halfway through as it was taking far too long and from what I’d done, I could calculate a rough rate of transfer. However, when I navigated to the destination folder I’d specified (the ‘K:\DATA’ folder), I found that the folder had disappeared from the drive and was no longer listed as a folder in the K: drive! (That folder contained multiple data files as well).

I immediately tried explicitly searching in Windows Explorer for “K:\DATA” and lo and behold, it did exist and all the files were still in there, I just couldn’t see the folder from the level above…

After doing some research, I found out that Robocopy has a known bug where it alters the attributes of the directory to a system and hidden file. It’s been known for quite some time and there are numerous blogs and questions out there on the subject. A shame I didn’t know this beforehand….

In order to reverse this change, I had to alter the “K:\DATA” folder’s attributes so that the folder would no longer be marked as either a system or a hidden file. These attributes are explicitly altered in cmd using the parameters ‘s’ and ‘h’ respectively, with more information on these parameters from Microsoft if you need it:

Attrib | Microsoft Learn

Be aware though that you have to change both the hidden attribute and system file attribute in one single go in cmd. If you try to alter these individually then your cmd line will likely error with either ‘Not Resetting System File’ (if you tried fixing the hidden attribute) or ‘Not Resetting Hidden File’ (if you tried to fixing the system attribute).

The Fix

So if this ever happens to you and you need to fix it asap, use the following command in an admin cmd, which will change the specified file or directories’ system (s) and hidden (h) file attribute:

attrib -s -h <directory>

e.g. For us, we ran the below:

attrib -s -h K:\DATA

This changed the “K:\DATA” folder and removed the system and hidden file attributes, bringing the folder out of hiding and back into file explorer.

If you wanted to hide a file or folder instead, you’d use “+” rather than “-“ in the above commands

Preventing it happening again next time!

If you don’t want to deal with the problem in the first place and like me, want to copy only a specific file (as it’s a known bug with Robocopy) and ensure the destination folder isn’t hidden, then ensure you use the below additional parameters when you write out your cmd:

e.g. In our case:

ROBOCOPY I:\DATA\ K:\DATA\ DB_DataFile.bak /A-:SH

When you  include the minus attributes (“A-:SH”) at the end of your command. This will stop the destination folder from being hidden and from being turned into a system file, thus preventing the problem ever happening in the first place:

“A-“ = Remove the specified Attributes

“S” = System file attribute

“H” = Hidden file attribute

If however you want to also ensure that only minimum attributes (e.g. Data, Attributes and Timestamp properties) of the file are also copied from source to destination (not things like ownership), you can also add on “/COPY:DAT” at the end of your cmd – this is the safer option.

e.g. In our case:

ROBOCOPY I:\DATA\ K:\DATA\ DB_DataFile.bak /A-:SH /COPY:DAT

I hope this helps!