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?
 
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.
0
 
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?
0
 
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

0
Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

 
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?
0
 
danfiggolfAuthor Commented:
Thanks Rob, you are the best.
0
 
RobSampsonCommented:
No worries. Thanks for the grade.

Regards,

Rob.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.