让vbscript等待文件出现
我正在尝试编写一个应用程序,用于从pc向移动电话发送和接收服务呼叫 我正在使用一个叫做mobile data studio的程序来完成大部分工作 基本上,该程序生成一个网页作为其报告为客户,这是邮寄给客户的系统,我有工作 问题是系统没有等到文件生成后才尝试将其作为附件发送,我收到一个错误: CDO.Message1 系统找不到指定的文件 职位:58.0 代码如下:让vbscript等待文件出现,vbscript,Vbscript,我正在尝试编写一个应用程序,用于从pc向移动电话发送和接收服务呼叫 我正在使用一个叫做mobile data studio的程序来完成大部分工作 基本上,该程序生成一个网页作为其报告为客户,这是邮寄给客户的系统,我有工作 问题是系统没有等到文件生成后才尝试将其作为附件发送,我收到一个错误: CDO.Message1 系统找不到指定的文件 职位:58.0 代码如下: objmessage.Addattachment sFile ' Process incoming sessions from P
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
一旦我在错误上单击OK,文件就会被创建,如果我再次运行脚本,它会处理邮件和附件,如果fax也设置为“yes”,则会打开文件
以下是所有代码:
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
处理Pocket PC传入的会话
通信会话功能(会话)
'检查用户是否表示需要确认
如果会话(“发送电子邮件”)=是,则
sendobjMessage会话
如果会话(“发送传真”)=“是”,则
发送传真
如果结束
'将返回值设置为true以指示正常
“应继续处理
OnIncomingSession=True
端函数
Sub sendobjMessage(会话)
'从会话获取电子邮件地址
sEmail=会话(“电子邮件地址”)
'从会话获取文件名
sFile=“C:\htm\”会话(“ORN”)和“&”htm”
Const CDOSENDUSINGPICKEP=1'使用本地SMTP服务分拣目录发送邮件。
Const cdoSendUsingPort=2'使用网络发送邮件(通过网络发送SMTP)。
Const cdoAnonymous=0'不进行身份验证
常量cdoBasic=1'基本(明文)身份验证
常数cdoNTLM=2'NTLM
Set objMessage=CreateObject(“CDO.Message”)
objMessage.Subject=“我们的公司-服务报告”和“&theSession”(“rdate”)
objMessage.From=“”服务部门“”
objMessage.To=sEmail
objMessage.TextBody=“Hi”和会话(“sname”)和“
objmessage.Addattachment文件
设置objfax=CreateObject(“WScript.Shell”)
objfax.runsfile
'==此部分提供远程SMTP服务器的配置信息。
objMessage.Configuration.Fields.Item_
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
'远程SMTP服务器的名称或IP
objMessage.Configuration.Fields.Item_
("http://schemas.microsoft.com/cdo/configuration/smtpserver“”=“smtp.mydomain.com”
'身份验证类型,无,基本(Base64编码),NTLM
objMessage.Configuration.Fields.Item_
("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate“”=cdoBasic
'您在SMTP服务器上的用户ID
objMessage.Configuration.Fields.Item_
("http://schemas.microsoft.com/cdo/configuration/sendusername") = "user@mydomain"
'您在SMTP服务器上的密码
objMessage.Configuration.Fields.Item_
("http://schemas.microsoft.com/cdo/configuration/sendpassword“”=“密码”
'服务器端口(通常为25个)
objMessage.Configuration.Fields.Item_
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
'将SSL用于连接(False或True)
objMessage.Configuration.Fields.Item_
("http://schemas.microsoft.com/cdo/configuration/smtpusessl“”=假
'连接超时(秒)(CDO尝试建立到SMTP服务器的连接的最长时间)
objMessage.Configuration.Fields.Item_
("http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout") = 60
objMessage.Configuration.Fields.Update
'==结束远程SMTP服务器配置节==
objMessage.Send
端接头
我可能有一些有用的工具。
我觉得你需要:
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)
我希望这有帮助。选项
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
Dim retval、fso、文件
设置fso=CreateObject(“scripting.filesystemobject”)
file=“c:\temp\myfile.txt”
retval=waitilexists(文件,true)
MsgBox“返回值:”&retval
函数waitilexists(ByVal文件,带repeat)
'将一直休眠,直到文件存在
'轮询间隔将逐渐增加,但不会超过最大等待时间
'超时毫秒后超时。如果由超时引起,将返回false。
暗淡等待时间、总等待时间、代表、doAgain
Const INIT_WAITTIME=20
Const MAX_WAITTIME=1000
常数超时=5000
常数斜率=1.1
doAgain=true
得寸进尺
waittime=INIT_waittime
totalwaittime=0
totalwaittime<超时时执行
waittime=Int(waittime*斜率)
如果waittime>MAX_waittime,则waittime=MAX_waittime
totalwaittime=totalwaittime+waittime
WScript.sleep waittime
如果存在fso.files(文件),则
waitilexists=true
退出功能
如果结束
环
如果有,请重复
rep=MsgBox(“此文件不存在:”&vbcr&file&vbcr&vbcr&“继续尝试?”,vbRetryCancel+vbequipment,“未找到文件”)
doAgain=(rep=vbRetry)
其他的
doAgain=false
如果结束
环
waitilexists=false
端函数
pinglocalhost是一个穷人在批处理文件中睡一觉的解决方案。在VBScript中,我们可以使用WScript.sleep。并且不需要复制参数值strFULLNamee。