2010-06-17 52 views
0

喂,讀出一個txt和發送的最後一行到的電子郵件地址 - VBScript中

我回來了哈哈:-),所以我有一個問題,我希望有人能幫助我...... 我知道我有很多問題,但我會嘗試學習VBScript :-)

情況: 此腳本讀出(每5分鐘)一個TXT的最後一行並將其發送到我的電子郵件地址。

問題: 我會檢查txt全部5分鐘,但此刻每隔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 

有人能幫助我嗎?

Sry基因我的英文不好...

回答

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 = "[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 
+0

偉大的工程謝謝你: - ) – Sebastian 2010-06-18 05:15:23

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 = "[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 
+0

應該是一個註釋。 – Sjoerd 2010-06-17 12:04:46

+0

每5分鐘發一封郵件,並附上test.txt的最後一行!我現在的問題是,當txt中有新的東西時,我只會收到一封郵件。所以我會檢查txt全部5分鐘,如果有新的東西(只有最後一行),我會收到一封郵件。我對我的英語很滿意。 關鍵字時間戳例如 – Sebastian 2010-06-17 12:06:54

+0

謝謝,但它同樣的問題,所有5分鐘,我成爲一個郵件......但我只會得到一個郵件時,有一個新的行在TXT ... – Sebastian 2010-06-17 13:05:35

相關問題