'Getting vbscript to wait until a file is present

I'm trying to write an application that sends and receives service calls from a pc to a mobile phone.

I'm using a program called mobile data studio to do most of the work.

Basically the program generates a web-page as its report for a customer and this is mailed to the customer by the system which i have working

The problem is that the system does not wait until the file is generated before it tries to send it as an attachment and i get an error:

CDO.Message1

The system cannot find the file specified.

Position: 58.0

this is the code:

objmessage.Addattachment sFile

Once I click OK on the error the file is then created and if I run the script again it process the mail and the attachment and opens the file if fax is set to "yes" also.

This is all the code:

' Process incoming sessions from Pocket PCs

Function OnIncomingSession (theSession)             
' Check if the user indicated a confirmation was desired
If theSession("SendEmail") = "Yes" Then    
 sendobjMessage theSession     
    ElseIf theSession("SendFax") = "Yes" Then      
 sendobjfax theSession               
 End If

   ' Set the return value to true to indicate that normal
' processing should continue
OnIncomingSession = True 

End Function

Sub sendobjMessage (theSession)
' Get the email address from the session
 sEmail = theSession ( "EmailAddress" )

 'Get the file name from the session
 sFile = "C:\htm\"& theSession("ORN")&"."&"htm"   

Const cdoSendUsingPickup = 1 'Send message using the local SMTP service pickup directory. 
Const cdoSendUsingPort = 2 'Send the message using the network (SMTP over the network). 

Const cdoAnonymous = 0 'Do not authenticate
Const cdoBasic = 1 'basic (clear-text) authentication
Const cdoNTLM = 2 'NTLM

Set objMessage = CreateObject("CDO.Message") 
objMessage.Subject = "Our Company  - Service Report" & " " & theSession("rdate")  
objMessage.From = """Service Department"" <user@mydomain>" 
objMessage.To = sEmail
objMessage.TextBody = "Hi " & theSession("sname") & ","
objmessage.Addattachment sFile

Set objfax = CreateObject("WScript.Shell")
objfax.Run sFile 

'==This section provides the configuration information for the remote SMTP server.

objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 

'Name or IP of Remote SMTP Server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.mydomain.com"

'Type of authentication, NONE, Basic (Base64 encoded), NTLM
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = cdoBasic

'Your UserID on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusername") = "user@mydomain"

'Your password on the SMTP server
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "password"

'Server port (typically 25)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 

'Use SSL for the connection (False or True)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = False

'Connection Timeout in seconds (the maximum time CDO will try to establish a connection to the SMTP server)
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60

objMessage.Configuration.Fields.Update

'==End remote SMTP server configuration section==

objMessage.Send 

End Sub 


Solution 1:[1]

Option Explicit
Dim retval, fso, file
Set fso = CreateObject ("scripting.filesystemobject")

file = "c:\temp\myfile.txt"
retval = waitTilExists (file, true)
MsgBox "return value: " & retval


Function waitTilExists (ByVal file, withRepeat)
    ' Sleeps until the file exists
    ' The polling interval will increase gradually, but never rises above MAX_WAITTIME
    ' Times out after TIMEOUT msec. Will return false if caused by timeout.
    Dim waittime, totalwaittime, rep, doAgain
    Const INIT_WAITTIME = 20
    Const MAX_WAITTIME = 1000
    Const TIMEOUT = 5000
    Const SLOPE = 1.1
    doAgain  = true
    Do While doAgain
        waittime = INIT_WAITTIME
        totalwaittime = 0
        Do While totalwaittime < TIMEOUT
            waittime = Int (waittime * SLOPE)
            If waittime>MAX_WAITTIME Then waittime=MAX_WAITTIME
            totalwaittime = totalwaittime + waittime
            WScript.sleep waittime
            If fso.fileExists (file) Then
                waitTilExists = true
                Exit Function
            End If
        Loop
        If withRepeat Then
            rep = MsgBox ("This file does not exist:" & vbcr & file & vbcr & vbcr & "Keep trying?", vbRetryCancel+vbExclamation, "File not found")
            doAgain = (rep = vbRetry)
        Else
            doAgain = false
        End If
    Loop

    waitTilExists = false
End Function

Solution 2:[2]

I might have some helpful tools.
I get the impression that you need:

  1. Routine that creates a delay or pause of a certain period of time
  2. Routine that checks for a file's existence.

Here's a routine for creating a delay or pause:

Sub subSleep(strSeconds) ' subSleep(2)
    Dim objShell
    Dim strCmd
    set objShell = CreateObject("wscript.Shell")
    'objShell.Run cmdline,1,False

    strCmd = "%COMSPEC% /c ping -n " & strSeconds & " 127.0.0.1>nul"     
    objShell.Run strCmd,0,1 
End Sub

Here's a routine for checking for a file's existence:

Function fnFileExists_Bln(strFULLNamee)
    Dim strFULLName
    strFULLName = strFULLNamee

    Dim objFSO
    Set objFSO = CreateObject("scripting.filesystemobject")

    fnFileExists_Bln = objFSO.FileExists(strFULLName)
End Function ' Function fnFileExists_Bln(strFULLNamee)

I hope this helps.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 mgr326639
Solution 2 Shadow Wizard Says No More War