0

我有两个包含以下内容的文本文件:

文件 1.txt:

Windows                  1.36
Linux                    2.78
MacOS                    3.45
Ubuntu                   4.12
FreePhysicalMemory      30.12
TotalVisibleMemorySize  48.00

文件2.txt:

MacOS                    6.39
Windows                  4.42
Linux                    5.76
Android                  3.46
FreePhysicalMemory      31.65
TotalVisibleMemorySize  48.00

输出.xls:

OPERATING SYSTEM       SERVER1    SERVER2
Windows                  1.36     4.42
Linux                    2.78     5.76
MacOS                    3.45     6.39
Ubuntu                   4.12     0.00
Android                  0.00     3.46
FreePhysicalMemory      30.12     31.65
TotalVisibleMemorySize  48.00     48.00

我想在 VBScript 程序或任何适合在 Windows 服务器上运行的程序中实现以下两件事:

  1. 将 file1.txt 和 fil2.txt 的内容插入到上面的 excel 表 output.xls 中。
  2. 将 output.xls 文件的内容作为正文发送到电子邮件中。

我认为,Point2 是使用下面的代码实现的,但没有得到如何在同一个程序中实现 Point1。

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const FileToBeUsed = "c:\output.xls"
Dim objCDO1
Dim fso, f
Set fso = CreateObject("Scripting.FileSystemObject")
Set f = fso.OpenTextFile(FileToBeUsed, ForReading)
Set objCDO1 = CreateObject("CDO.Message")
objCDO1.Textbody = f.ReadAll
f.Close
objCDO1.TO ="sunny@abc.com"
objCDO1.From = "dontreply@abc.com"
objCDO1.Subject = "Server Memory"
objCDO1.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration /sendusing") = 2 
objCDO1.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtpb.intra.abc.com"
objCDO1.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration /smtpserverport") = 25 
objCDO1.Configuration.Fields.Update     
objCDO1.Send
Set f = Nothing
Set fso = Nothing

任何建议都受到高度赞赏。

注意:我在这里的目的是发送带有正确格式和对齐数据的电子邮件。

编辑1:

这就是我对第 1、2、3 点所能做的一切。不知道如何以 HTML 电子邮件正文格式整合所有这些内容..:(

Const ForReading = 1, ForWriting = 2, ForAppending = 8
Const TristateUseDefault = -2, TristateTrue = -1, TristateFalse = 0

Dim fso,f,objFSO,objFile,objRE, FileName
Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 

const strFileName1 = "D:\file1.txt"
const strFileName2 = "D:\file2.txt"

Set objFile = objFSO.OpenTextFile(strFileName1, fsoForReading) +    objFSO.OpenTextFile(strFileName2, fsoForReading)
objobjFile.Close
Set objFile = Nothing
Set objFSO = Nothing

Set objRE = New RegExp

With objRE
    .Pattern    = "[A-Z][0-9]"
    .IgnoreCase = False
    .Global     = False
End With

Set objFSO = Server.CreateObject("Scripting.FileSystemObject") 
const strFileName1 = "D:\file1.txt"
const strFileName2 = "D:\file2.txt"

Set objFile = objFSO.OpenTextFile(strFileName1, fsoForReading) +   objFSO.OpenTextFile(strFileName2, fsoForReading)

Do Until objFile.AtEndOfStream
    strSearchString = objFile.ReadLine
    Set colMatches = objRegEx.Execute(strSearchString)  
    If colMatches.Count > 0 Then
        For Each strMatch in colMatches   
            Wscript.Echo strSearchString 
        Next
    End If
Loop
    objFile.Close

Class memory1class
    Public operatingsystem, memory
End Class
Dim memory1dict: Set memory1 = CreateObject("Scripting.Dictionary")
Dim memory2dict: Set memory2 = CreateObject("Scripting.Dictionary")

Dim memory1: Set memory1 = new memory1class
With memory1
    .operatingsystem = "?"
    .memory = "?"

End With
memory1dict.Add "1", memory1

Dim memory1details: Set memory1details = memory1dict.Item("1")
WScript.StdOut.WriteLine("operatingsystem:" & memory1details.first & " " &  memory1details.memory & " )
4

1 回答 1

1

我不想直接给你代码,因为这是一个很好的机会让你了解更多关于你自己解决问题的不同选项,但我可以给你一个粗略的大纲。

  1. 打开两个文本文件进行阅读。Windows 提供了一个“脚本”库,其中包含FileSystemObject. 您将声明它的一个实例,如下所示,您可以在 Google 上搜索如何使用它打开文本文件的详细信息:

    Set fso = CreateObject("Scripting.FileSystemObject")
    
  2. 一次读取一行中的每个文件。解析每一行以获得“名称”部分和“数字”部分作为不同的值。您可以通过组合 、 和 函数来做到这一点InStrInStrRev或者Mid您可以使用更强大的 RegExp 库(VBScript 的正则表达式类)。

    Set re = New RegExp
    
  3. 将每个“名称”和“值”存储到一个数据结构中以供以后使用——我推荐一个关联数组,例如 VBScript 的 Dictionary 类。为每个文本文件使用一个单独的文件将允许您稍后交叉引用它们。

    Set dictStats1 = CreateObject("Scripting.Dictionary")
    
  4. 要在电子邮件中正确显示数据格式,我建议使用 HTML 表格。除了 之外TextBody,该CDO.Message对象还有一个HTMLBody属性,允许您为电子邮件提供结构化格式,而不仅仅是原始文本。从w3school.com上的简单示例推断,您可以构造一个函数,该函数将接受两个字典并使用它们来构造 HTML 表并将其作为字符串返回,以通过HTMLBody属性加载到电子邮件中。

我希望这会有所帮助!如果您对具体细节有任何疑问,请告诉我。

于 2013-10-21T19:02:57.477 回答