Robocopy Destination Folder Hidden After Copying a Drive? Use attrib

スポンサーリンク
Robocopy Destination Folder Hidden After Copying a Drive? Use attrib File Operations
スポンサーリンク

I used robocopy to copy a whole drive (B:\) into a folder on another drive. The log said the copy finished, but the destination folder G:\B-drive didn’t show up in Explorer.

Short answer: when the source is the top of a drive, such as B:, the destination folder gets the “hidden” plus “system” attributes. Removing them with attrib -s -h after robocopy makes it visible.

Cause: a drive root has the hidden, system attributes

Microsoft’s robocopy documentation says it too: if data is copied from the root of a device, the destination directory adopts the “hidden”, “system” attributes during the copy.

The root of an NTFS drive carries the hidden, system attributes from the start. robocopy copies folder attributes as well, because /DCOPY defaults to DA (A means attributes). So the destination folder becomes a hidden system folder. By default Explorer doesn’t show folders with both attributes, so it looks like the folder vanished. The contents were copied properly; typing the path into the address bar opens it.

Fix: remove the hidden, system attributes with attrib

Run this on the destination folder:

attrib -s -h "G:\B-drive"

-s removes the system attribute, -h the hidden one; always give both together. Each robocopy run sets the attributes again, so run it right after robocopy in your batch file (a whole-drive copy batch is at the end of this post).

Copying a system image? Put it at the drive root

If you’re copying a Windows backup (system image), keep WindowsImageBackup at the top level of the destination drive, not inside a folder. Windows’ recovery tool is said to search for it at the top level of each drive. Since you aren’t copying a whole drive, the hidden attribute doesn’t appear either.

Here’s the batch file I use now. Put your own NAS name, PC name where the angle-bracket placeholders are. The log file name takes the date from %date% assuming the yyyy/mm/dd format of my Japanese Windows. If your date looks different (run echo %date% to check), adjust the character positions.

@echo off
set "logfolder=\\<NAS>\backup\backup-tools\BtoG-copy\log"
set "logfile=%logfolder%\%date:~0,4%-%date:~5,2%-%date:~8,2%_imagecopy.log"
set "opts=/MIR /DCOPY:DAT /COPYALL /IT /B /log+:"%logfile%" /np /R:3 /W:3"

rem Stop if B has no backup, because /MIR would also remove the images on G
if not exist "B:\WindowsImageBackup\" (
  echo B:\WindowsImageBackup not found. Exiting without copying.
  pause
  exit /b 1
)

set "result=OK"
robocopy "B:\WindowsImageBackup" "G:\WindowsImageBackup" %opts%
if errorlevel 8 set "result=NG"

if exist "B:\<PC name>\" (
  robocopy "B:\<PC name>" "G:\<PC name>" %opts%
  if errorlevel 8 set "result=NG"
)

forfiles /P "%logfolder%" /M "*.log" /D -93 /C "cmd /c del @file"

if "%result%"=="NG" (
  echo There were copy errors. Check the log: %logfile%
  pause
  exit /b 1
)
echo Done. Log: %logfile%

It stops if WindowsImageBackup isn’t on drive B. Running /MIR while drive B is broken would also wipe the images on drive G. An exit code of 8 or higher is reported as an error (robocopy exit codes explained). In my environment, a system image of about 343GB copied in 2 hours 43 minutes with zero failures. A PowerShell version that backs up to a NAS is in Back up a local folder to a password-protected NAS with Robocopy.

When it says “You do not have the Manage Auditing user right”

Double-clicked normally, this batch stops with an error saying you lack the Manage Auditing user right. /COPYALL copies auditing info as well, while /B is backup mode; both need administrator rights. Make a shortcut to the batch file, then check “Run as administrator” under “Advanced” in its properties. After that, a double-click works.

Shortcut properties for the batch file with Run as administrator checked under Advanced (Japanese UI)

Summary

  • Copying a drive root with robocopy gives the destination folder the hidden, system attributes, making it invisible
  • Running attrib -s -h "destination folder" after robocopy makes it show up
  • Put system images as WindowsImageBackup directly under the destination drive’s root

Bonus: a batch file that copies a whole drive

This copies an entire drive into a folder on another drive. The attrib on line 8 removes the hidden, system attributes.

set srcfolder="B:"
set dstfolder="G:\B-drive"

set logfolder="\\<NAS>\backup\backup-tools\BtoG-copy\log"
set logfile=%logfolder%\%date:~0,4%-%date:~5,2%-%date:~8,2%_datacopy.log

robocopy "%srcfolder%" "%dstfolder%" /MIR /DCOPY:DAT /COPYALL /IT /B /log+:"%logfile%" /np /R:3 /W:3
attrib -s -h "%dstfolder%"
forfiles /P "%logfolder%" /M "*.log" /D -93 /C "cmd /c del @file"

/MIR on line 7 deletes files at the destination that aren’t in the source, so double-check the destination folder. Line 9 deletes logs older than 93 days.

References

コメント

Copied title and URL