【问题标题】:Getting vbscript to wait until a file is present让 vbscript 等到文件存在
【发布时间】:2011-10-15 20:28:45
【问题描述】:

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

我正在使用一个名为移动数据工作室的程序来完成大部分工作。

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

问题是系统没有等到文件生成后才尝试将其作为附件发送,我收到错误:

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 

【问题讨论】:

    标签: vbscript


    【解决方案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
    

    【讨论】:

      【解决方案2】:

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

      1. 产生延迟或暂停一段时间的例程
      2. 检查文件是否存在的例程。

      以下是创建延迟或暂停的例程:

      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)
      

      我希望这会有所帮助。

      【讨论】:

      • Ping localhost 是一个穷人在批处理文件中睡一会儿的解决方案。在 VBScript 中,我们可以使用 WScript.sleep。而且不需要复制参数值strFULLNamee。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-08-04
      • 2010-10-09
      • 2013-09-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多