Home > Software engineering >  How to get list of failed Directories using Robocopy
How to get list of failed Directories using Robocopy

Time:12-23

Good day,

Can't find a way how to get list of directories' names which were failed to copy with Robocopy. Using below command: robocopy C:\Temp P:\TEMP\ /FFT /E /R:0 /W:0 /ETA /NP /NFL /NJH /XD "System Volume Information" "$RECYCLE.BIN" /LOG:TEMP_LOG

And getting below summary: QUOTE 8 C:\Temp
0 C:\Temp\Logs
2 C:\Temp\Logs\Defender
0 C:\Temp\Test\


           Total    Copied   Skipped  Mismatch    FAILED    Extras
Dirs :         4         0         3         0         1         0

Files : 10 0 10 0 0 0 Bytes : 78.9 k 0 78.9 k 0 0 0 Times : 0:00:00 0:00:00 0:00:00 0:00:00 Ended : Wednesday, December 22, 2021 10:24:02 UNQUOTE

One directory failed to copy. How can I get it's name? Thanks

CodePudding user response:

You cannot use Try/Catch for external applications like robocopy.exe. You can rather use $LASTEXITCODE to do that. A sample is below:

robocopy b:\destinationdoesnotexist C:\documents /MIR

 if ($lastexitcode -eq 0)
 {
      write-host "Robocopy succeeded"
 }
else
{
      write-host "Robocopy failed with exit code:" $lastexitcode
}

Check the List of Robocopy Exit Codes

Note: There is a /log parameter in the robocopy. Kindly use it for more insights.

CodePudding user response:

Yes, I'm using $lastexitcode to get execution status and using /LOG parameter. My full command is below:

  • robocopy C:\Temp P:\Temp\ /FFT /E /R:0 /W:0 /TEE /XJD /NP /NFL /NDL /NJS /NJH /ETA /XD "System Volume Information" "$RECYCLE.BIN" /LOG :TEMP.LOG

Log file is showing list of Files which failed to copy. But I need also list of Folders which failed to copy. Just did several test and found weird behaviour. I removed completely read/write permissions for one file C:\Temp\FuncTest.html and one folder C:\Temp\Test to replicate copy error.

If I remove destination folder "P:\Temp" and run command like for the first time, then I get in LOG file name of failed File and failed Folder:

QUOTE

2021/12/22 15:05:01 ERROR 5 (0x00000005) Copying File C:\Temp\FuncTest.html

Access is denied.

2021/12/22 15:05:01 ERROR 5 (0x00000005) Time-Stamping Destination Directory *P:\Temp\Test*

Access is denied.

UNQUOTE

If I run the same command again then I get to LOG file only failed file and no failed folder:

QUOTE

2021/12/22 15:12:49 ERROR 5 (0x00000005) Copying File C:\Temp\FuncTest.html Access is denied.

UNQUOTE

Noticed that after first run robocopy is creating empty folder of source failed folder at destination location: P:\Temp\Test

So all subsequent runs robocopy see folder at destination location and treat that as no changes and do not place this folder name to the LOG file. That means if I have hourly run and overlooked first run then I need to search all previous reports in order to find folder name. How to make this failed folder name to appear all the time in LOG file (not only after first run). Failed file name appears in all future LOG files despite how many times I run robocopy command.

  • Related