Robocopy not deleting source files

jayanthi ramprakash 46 Reputation points
2020-07-30T22:24:11.783+00:00

Hi Experts,

Could you please advice on the following. Thank you.

1) Robocopy is having issues deleting the source files after the move. Below is the error. (from Robocopy.log)

ERROR 32 (0x00000020) Deleting Source Directory \server01\Folder1\
The process cannot access the file because it is being used by another process.

2) Also, I am not getting the error email. Am I missing something? (code below)

    $SourceFolder1 = "\\server01\Folder1"
    $TargetFolder1 = "\\server02\Folder2"


    robocopy $SourceFolder1 $TargetFolder1 *.log  /XX /Move /LOG+:Robocopy.log


    if ($lastexitcode -ne 0)
     {

     Send-MailMessage -From "alerts@company.com" -To  "servicedesk@company.com" -Subject "Automated message: Robo copy failed" -Body  "Robocopy failed with exit code: $lastexitcode"  -SmtpServer "smtp.company.com"

    }
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
12,084 questions
Windows Server Backup
Windows Server Backup
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.Backup: A duplicate copy of a program, a disk, or data, made either for archiving purposes or for safeguarding valuable files from loss should the active copy be damaged or destroyed.
450 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,355 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Rich Matheisen 44,696 Reputation points
    2020-07-31T19:38:03.557+00:00

    Try some of these utilities:
    find-process-locked-file-openfiles-utility

    As for robocopy, I've hardly ever used it but it seems to me that you're trying to remove the source directory and that directory is in use. Perhaps adding the "/E" switch to the parameters (i.e., leave the source directory in place) and deal with the directory's removal in some other way after the files have been moved.

    1 person found this answer helpful.
    0 comments No comments

  2. Rich Matheisen 44,696 Reputation points
    2020-07-31T02:47:44.633+00:00

    Is the directory Folder1 in use on SERVER01?

    Does the server smtp.compcny.com accept unauthenticated connections on port 25? Perhaps you should be using port 587 and providing credentials?

    If you want to know what the error is, wrap the Send-MailMessage cmdlet in a Try/Catch block and add "-ErrorAction STOP" to the cmdlet. In the Catch block display or record the error for examination.

    0 comments No comments

  3. jayanthi ramprakash 46 Reputation points
    2020-07-31T03:05:11.84+00:00

    Hi Rich,

    Thanks for the response. Email issue is sorted now.

    However, the source files are still not deleted after the move. I do not see any connections in "Open files" or "Sessions" in computer management. What's the best to see if the folder is in use by some process? Thank you.

    0 comments No comments

  4. jayanthi ramprakash 46 Reputation points
    2020-08-03T20:53:00.29+00:00

    Hi Rich,

    Thanks for the response.

    I used the tools and looks like the files are not locked. Looks like Robocopy is trying to delete the source folders and hence the error. Adding the "/E" switch did not help.
    Is there a way to fine tune robocopy not to delete the folders but only the log files? Thank you.