让vbscript等到文件存在

时间:2011-10-15 20:28:45

标签: vbscript

我正在尝试编写一个应用程序,用于从PC向手机发送和接收服务电话。

我正在使用一个名为mobile data studio的程序来完成大部分工作。

基本上,程序会为客户生成一个网页作为其报告,并通过我工作的系统邮寄给客户

问题是系统在尝试将其作为附件发送之前不会等到文件生成,我收到错误:

CDO.Message1

  

系统找不到指定的文件。

     

位置:58.0

这是代码:

objmessage.Addattachment sFile

如果我在错误上单击“确定”,则会创建该文件,如果我再次运行该脚本,则会处理邮件和附件,并在传真设置为“是”的情况下打开文件。

这是所有代码:

' 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 

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:-2)

我可能会有一些有用的工具 我得到你需要的印象:

  1. 在一段时间内造成延迟或暂停的例程
  2. 检查文件是否存在的例程。
  3. 以下是创建延迟或暂停的例程:

    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
    

    这是检查文件存在的例程:

    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)
    

    我希望这会有所帮助。