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

发布于 2024-09-05 21:27:46 字数 1676 浏览 8 评论 0原文

你好,

我回来了哈哈:-)所以我有下一个问题,我希望有人能帮助我...... 我知道我有很多问题,但我会尝试学习 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 = "[email protected]"
                    objEmail.To = "[email protected]"
                    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

有人可以帮助我吗?

抱歉我的英语不好......

Hallo,

I'm back haha :-) so i have the next question and i hope someone can help me...
I know i have a lot of questions but i will try to learn vbscript :-)

Situation:
This script read out (every 5 min) the last line of a txt and send it to my eMail Address.

Problem:
I'll check the txt all 5 min, but at the moment every 5 min there comes a mail. I'll try to get only a new mail, when there is something new in the 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 = "[email protected]"
                    objEmail.To = "[email protected]"
                    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

Can someone help me?

Sry for my bad english...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

情话难免假 2024-09-12 21:27:46

试试这个

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 = "[email protected]"
                    objEmail.To = "[email protected]"
                    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

try this

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 = "[email protected]"
                    objEmail.To = "[email protected]"
                    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
乙白 2024-09-12 21:27:46

您只收到一封邮件的原因是由于 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 = "[email protected]" 
                objEmail.To = "[email protected]" 
                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 

The reason that you only get one mail is because of the UBound, which only gets you the last element of the array.

To send a mail for each line, you will have to remember the upper bound after sending the mails and then loop over the lines

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 = "[email protected]" 
                objEmail.To = "[email protected]" 
                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 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文