tags:

views:

39

answers:

2

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...

+1  A: 

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 
Aidan
Should be a comment.
Sjoerd
every 5 minutes it comes a mail with the last line of the test.txt! My Problem is now, i will only have a mail when there is something new in the txt. So i will check the txt all 5 minutes and if there something new in it (only the last line), i will have a mail. I'm sry for my english. keyword timestamp for example
matthias
thanks, but its the same problem all 5 minutes i became a mail...but i will only get a mail when there is a new line in the txt...
matthias
+1  A: 

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
Tester101
works great thank you :-)
matthias