Wait for Completed VBScript Process before Emailing Result

I have a two part process of first running robocopy, then secondly emailing the result.  My problem is the first process of running robocopy has not completed and created its "E:\figs\result.txt" log file, which is to be emailed by the second part of the script.  How can I script the email fuction to wait until the log file is available?

'===
'Pull GIS files to client with Robocopy
'===
Dim EXCLUDEFILES, ROBOCOPYLOCATION, strDirExclude, mycommand
EXCLUDEFILES = "E:\excludefiles.txt"
SOURCELOCATION = "E:\test1"
TARGETLOCATION = "E:\test2"
Const FOR_READING = 1
Set WshShell = WScript.CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.OpenTextFile(EXCLUDEFILES, FOR_READING)
strFileExclude = objFile.ReadAll
objFile.Close
arrFileExclude = Split(strFileExclude, vbCrLf)
For Each bestand In arrFileExclude
bestanden = bestanden + " /xf " + chr(34) + bestand + chr(34)
Next
objCommand = "RoboCopy.Exe " & Chr(34) & SOURCELOCATION & Chr(34) & " " & Chr(34) & TARGETLOCATION & Chr(34) & " /MIR /r:0 /w:0 /ETA /LOG:result.txt /TEE " & dirs & bestanden
WshShell.Run objCommand
'===
'Mail Paul Status
'===
Const cdoSendUsingPickup = 1 
Const cdoSendUsingPort = 2 'Must use this to use Delivery Notification
Const cdoAnonymous = 0
Const cdoBasic = 1 ' clear text
Const cdoNTLM = 2 'NTLM
'Delivery Status Notifications
Const cdoDSNDefault = 0 'None
Const cdoDSNNever = 1 'None
Const cdoDSNFailure = 2 'Failure
Const cdoDSNSuccess = 4 'Success
Const cdoDSNDelay = 8 'Delay
Const cdoDSNSuccessFailOrDelay = 14 'Success, failure or delay
 
set objMsg = CreateObject("CDO.Message")
set objConf = CreateObject("CDO.Configuration")
 
Set objFlds = objConf.Fields
With objFlds
  .Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = cdoSendUsingPort
  .Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "mail.com"
  .Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic
  .Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "myemail"
  .Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "my-password"
  .Update
End With
 
strBody = "This is a sample message." & vbCRLF
strBody = strBody & "It was sent using CDO." & vbCRLF
 
With objMsg
  Set .Configuration = objConf
  .To = "daniel@box.com" 
  .From = "myemail"
  .Subject = "MDC Engine 5 Map File Pull"
  .TextBody = strBody
   'use .HTMLBody to send HTML email.
  .Addattachment "E:\figs\result.txt"
  .Fields("urn:schemas:mailheader:disposition-notification-to") = "myemail"
  .Fields("urn:schemas:mailheader:return-receipt-to") = "myemail" 
  .DSNOptions = cdoDSNSuccessFailOrDelay
  .Fields.update
  .Send
End With

Open in new window

danfiggolfAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

danfiggolfAuthor Commented:
Should I put a loop that says if file "results.txt" exist then launch the email process, and if it doesn't wait and check again?
sr75Commented:
Instead of using this method:

objCommand =
WshShell.Run objCommand

use this method instead:

set objExec = WshShell.Exec("ping -n 2 -w 1000 " & Server)
strPingResults = LCase(objExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") then
      ping = TRUE
End If

The first method is only going to start the process and move on.  The second method ties the process to the script, therefore it will wait for the results before moving on.
set objExec = WshShell.Exec("ping -n 2 -w 1000 " & Server)
strPingResults = LCase(objExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") then
	ping = TRUE
End If

Open in new window

danfiggolfAuthor Commented:
So I would use an IF the results.txt file exist then run the CDO code to email? And put the CDO into a email() function?
Introduction to R

R is considered the predominant language for data scientist and statisticians. Learn how to use R for your own data science projects.

RobSampsonCommented:
You should be able to change this:
WshShell.Run objCommand

to this
WshShell.Run objCommand, 1, True

See
http://msdn2.microsoft.com/en-us/library/d5fk67ky.aspx

The second parameter is the Window style, where 1 is visible, and nomal.  The third parameter is you key one, where True makes the rest of the script wait until the command has finished executing.

Regards,

Rob.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
danfiggolfAuthor Commented:
Thanks Rob, you are the best.
RobSampsonCommented:
No worries. Thanks for the grade.

Regards,

Rob.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.