How To Get Robocopy Status Emails

What’s that? Robocopy can send emails? Well not exactly but here’s how you can do it.

Robocopy is a free tool from Microsoft that was originally part of Windows Resource Kit for Windows NT 4. It was then included as a standard feature with Windows Server 2003 and each subsequent release of Windows Server. It is a powerful tool for transferring data from 1 place to another.  You can transfer data from 1 folder to another on the same server or between 2 servers provided you use an account with permissions on both servers. There are extensive command switches available to control how the data transferred such as preserving ACL permissions, directory structures, and even empty folders. The basic syntax is as follows:

robocopy Source Destination [File[ ...]] [Options]

Recently I had to transfer nearly 50 GB of data from a legacy server to a new server. This migration was occurring on 2 production mail servers and there would be a impact to users so downtime needed to be minimal. I had done thorough testing so I knew it was going to take approximately 3 hours to transfer the data.

Since I was transferring the data from a legacy server to a new Windows 2008 R2 server I planned to initiate robcopy from the new server. By doing this I was able to use the /MT multi-threading switch. I had done several tests using different values for this switch and settled on 32. Increasing the value beyond 32 did not produce any noticeable difference in transfer speed. Without using the switch I was consistently seeing 150 MB/min transfer speeds. Here is the syntax that I used:

robocopy.exe  "\\oldserver\bigfolder" "D:\newfolder" /LOG:d:\temp\log.txt /MT:32 /E /W:5 /Z /V /R:6\r\n

I knew that once I initiated the robocopy job there was nothing else I could do other than wait for the data transfer to finish. Furthermore, I did not want to have to frequently check the destination server to know how the transfer was progressing.  So I needed a way to count the folders as they showed up on the destination server and then send me an email.

The solution to this was to leverage the power of Windows Script Host which is native to Windows 2008 servers.  Using Vbscript I could easily create a script to count folders in a directory and then send myself an email. The 2nd half of this challenge was to have that script run every 10 minutes which I explain later in this post.\r\n\r\n

Using Vbscript I create a function that will check a folder and return count of folders it contains. \r\n

 function countFolders(strPath)\r\n        dim objShell\r\n        dim objFolder\r\n        dim folderCount\r\n        \r\n        set objShell = CreateObject("shell.application")\r\n        set objFolder = objShell.NameSpace(strPath)\r\n\r\n        if (not objFolder is nothing) then\r\n            dim objFolderItems\r\n\r\n            set objFolderItems = objFolder.Items\r\n\r\n            if (not objFolderItems Is Nothing) then\r\n                 folderCount=objFolderItems.Count\r\n            end if\r\n\r\n            set objFolderItem = nothing\r\n        end if\r\n\r\n        set objFolder = nothing\r\n        set objShell = nothing\r\n\r\n        countFolders=folderCount\r\n    end function

\r\n\r\nNext I needed to create a function that would send an email with the folder count. I format the message so that the pertinent details are in the subject line. \r\n\r\n

Function SendCount(strCount)\r\n\r\nSet objMessage = CreateObject("CDO.Message")  \r\nobjMessage.Subject = strCount\r\nobjMessage.From = ""  \r\nobjMessage.To = ""  \r\nobjMessage.TextBody = strCount\r\n \r\nobjMessage.Configuration.Fields.Item _ \r\n("") = 2  \r\n \r\n'Name or IP of Remote SMTP Server \r\nobjMessage.Configuration.Fields.Item _ \r\n("") = "" \r\n \r\n'Server port (typically 25) \r\nobjMessage.Configuration.Fields.Item _ \r\n("") = 25 \r\n \r\nobjMessage.Configuration.Fields.Item _ \r\n("") = 1 \r\n \r\nobjMessage.Configuration.Fields.Item _ \r\n("") = false \r\n \r\nobjMessage.Configuration.Fields.Item _ \r\n("") = "" \r\n \r\nobjMessage.Configuration.Fields.Item _ \r\n("") = "xyz123" \r\n \r\nobjMessage.Configuration.Fields.Update \r\n\r\nobjMessage.Send  \r\nEnd Function\r\n

\r\n\r\nSo now that I’ve got my two functions I need to code how they’ll be used. I create local variables for the destination path that is going to be monitored, the total number of folders being copied, and a counter. \r\n

Option Explicit\r\n\r\nDim destFolder \r\nDim maxFolders\r\nDim folderCount\r\n\r\ndestFolder="d:\destinationfolder"\r\nmaxFolders=500\r\nfolderCount=countFolders(destFolder)\r\n\r\nsendCount("F: " & folderCount & "  " & FormatPercent(folderCount/maxFolders))\r\n\r\n

\r\n\r\nAt this point the script is ready to run. I knew there were 500 folders being copied to the new server so each time the script was run it would send me an email with the number of folders copied and the % complete.  In the beginning of this post I indicated I need this to be automated so the next step is to login to the destination server and create a scheduled task to run our new script. Since I know from testing this transfer will take approximately 3 hours I schedule the task to end after 3 hours but I want it to run every 10 minutes until then.

Peter Viola

Creative, customer focused, results oriented, Senior Web Systems Engineer who enjoys providing the highest level of customer service supporting complex Windows hosting solutions. MCITP, MCSA, MCTS

More Posts - Website