读出一个文本并将最后一行发送到电子邮件地址--vbscript

时间:2010-06-17 11:58:11

标签: vbscript wsh text-files

喂,

我回来了哈哈:-)所以我有下一个问题,我希望有人可以帮助我...... 我知道我有很多问题,但我会尝试学习vbscript: - )

情况: 该脚本读出(每5分钟)txt的最后一行并将其发送到我的电子邮件地址。

问题: 我会在5分钟内检查txt,但是每5分钟就有一封邮件。当txt中有新内容时,我会尝试只收到一封新邮件。

Option Explicit

Dim fso, WshShell, Text, Last, objEmail

Const folder = "C:\test.txt"

Set fso=CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")

Do
    Text = Split(fso.OpenTextFile(Datei, 1).ReadAll, vbCrLF)
    Letzte = Text(UBound(Text))
                       Set objEmail = CreateObject("CDO.Message")
                    objEmail.From = "test@test.com"
                    objEmail.To = "test@test.com"
                    objEmail.Subject = "Control" 
                    objEmail.Textbody = Last
                    objEmail.Configuration.Fields.Item _
                        ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
                    objEmail.Configuration.Fields.Item _
                        ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
                            "smtpip" 
                    objEmail.Configuration.Fields.Item _
                        ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
                    objEmail.Configuration.Fields.Update
                    objEmail.Send

                    WScript.Sleep 300000
Loop

有人可以帮助我吗?

我的英语不好......

2 个答案:

答案 0 :(得分:1)

你只收到一封邮件的原因是因为UBound只能获得数组的最后一个元素。

要为每一行发送邮件,您必须在发送邮件后记住上限,然后循环上线

Option Explicit 

Dim fso, WshShell, Text, Last, objEmail 

Const folder = "C:\test.txt" 

Set fso=CreateObject("Scripting.FileSystemObject") 
Set WshShell = WScript.CreateObject("WScript.Shell") 

dim index = 1

Do 
    Text = Split(fso.OpenTextFile(Datei, 1).ReadAll, vbCrLF) 
    Letzte = Text(UBound(Text)) 

                WScript.Sleep 300000 

for i = index to UBound(Text) 
                   Set objEmail = CreateObject("CDO.Message") 
                objEmail.From = "test@test.com" 
                objEmail.To = "test@test.com" 
                objEmail.Subject = "Control"  
                objEmail.Textbody = Last 
                objEmail.Configuration.Fields.Item _ 
                    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 
                objEmail.Configuration.Fields.Item _ 
                    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _ 
                        "smtpip"  
                objEmail.Configuration.Fields.Item _ 
                    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 
                objEmail.Configuration.Fields.Update 
                objEmail.Send 
Next 

index = UBound(Text) 

Loop 

答案 1 :(得分:1)

试试这个

Option Explicit

Dim fso, WshShell, Text, Last, objEmail, linecount

Const folder = "C:\test.txt"
linecount = 0

Set fso=CreateObject("Scripting.FileSystemObject")
Set WshShell = WScript.CreateObject("WScript.Shell")

Do
    Text = Split(fso.OpenTextFile(Datei, 1).ReadAll, vbCrLF)
    if UBound(Text) > linecount Then
         linecount = UBound(Text)
         Letzte = Text(UBound(Text))
                       Set objEmail = CreateObject("CDO.Message")
                    objEmail.From = "test@test.com"
                    objEmail.To = "test@test.com"
                    objEmail.Subject = "Control" 
                    objEmail.Textbody = Last
                    objEmail.Configuration.Fields.Item _
                        ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
                    objEmail.Configuration.Fields.Item _
                        ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = _
                            "smtpip" 
                    objEmail.Configuration.Fields.Item _
                        ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
                    objEmail.Configuration.Fields.Update
                    objEmail.Send
     End If

                    WScript.Sleep 300000
Loop