0

首先,我不是编程背景,对 VBscript 完全陌生。由于某些原因,我必须在办公室执行此脚本任务。我想将它与 Quick Test Professional 11 一起使用。

我在这里以及其他论坛上浏览了许多帖子,但无法找到所需的信息。

好的,这就是我需要做的:

我必须比较两个文本文件并将差异写入第三个文件。这两个文件在某些​​字段中具有几乎相同的内容,即:日期、订单号等。

例如,文件 1 的日期:00/11/1234 和订单号:1111 和文件 2 的日期:11/00/6789 和订单号:2222 那么有什么方法可以忽略这些字段及其值? 有什么方法可以创建我可以添加的忽略列表,在比较过程中将使用它并在比较过程中跳过这些字段?所以我的差异文件不会有这些差异,因为这些值总是不同的。所以我可以在我的结果文件中得到所有其他的差异。

供您参考,这里是示例文件。

到目前为止,我已经比较了这两个文件,但以最简单的方式,我不知道如何忽略这些字段。我想将这些任务作为一个函数,以便我可以在我的函数库中使用它。

文件-1

日期:00/11/1234 / 订单号:1111

价格 1:1111.00 美元

价格 2:2222.00 美元

价格 3:1234.00 美元

ABC def GHI kjl 1111

订单号:1111

期限:2年

日期:00/11/1234

文件-2

日期:11/00/6789 和订单号:2222

价格 1:1111.00 美元

价格 2:2222.00 美元

价格 3:5678.00 美元

ABC def GHI kjl 1111

订单号:2222

期限:3年

日期:11/00/6789

结果文件应显示:

差异:

文件 1 第 4 行:价格 3:$1234.00

文件 2 第 4 行:价格 3:$5678.00

文件 1 第 7 行:期限:2 年

文件 2 第 7 行:期限:3 年

非常感谢您提前。

嗨@Ekkehard.Horner 非常感谢您的帮助和时间以及容忍我愚蠢的问题。事实是,我越是试图理解你的代码,我就越感到困惑。当我将以下代码放入 Quick Test Pro_11 中时,它会抛出语法错误 @“Dim oDiffer : Set oDiffer = New cDiffer.init("C:......" QTP 说“预期语句结束”在“ ...新的 cDiffer" 和 ".init" QTP 在函数 "TrailVersion" 和函数 "GoldVersion" 中都显示了错误

如果您对此有所了解,那就太好了。是否有必要拥有“预期的”文本文件......?因为我不想包含该部分,否则我必须为每次比较创建“预期”文件。

请原谅我愚蠢的问题。

提前致谢。

Class cDiffer   
Option Explicit  
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")  
    WScript.Quit TrialVersion() 
    WScript.Quit TinVersion()  
Function TinVersion()   
    WScript.Echo "can't compare files yet."   
    TinVersion = 1 
End Function ' TinVersion  
HERE I'VE COMMENTED TRIALVERSION FUNCTION 
Function TrialVersion()   
Dim oDiffer : Set oDiffer = New cDiffer.init("C:\Documents and Settings\24800\My Documents\PDF comparison\A_30120625003267.TXT", "C:\Documents and Settings\aa24800\My Documents\PDFcomparison\B_30120502002776.TXT", Array("Quote ID:", "Quote Summary for:", "Quote Date:", "Tracking ID (A):", "Tracking ID (Z):", "Tracking ID:")
    ' the differ should be able to return a result - the differences   
    Dim sRes : sRes = oDiffer.diffs()   
    ' check actual vs. expected result   
    Dim sExp : sExp = goFS.OpenTextFile("Expected").ReadAll()  
    WScript.Echo "--------- Res"   
    WScript.Echo sRes   
    If sExp = sRes Then      
        WScript.Echo "ok"      
        ' save result      
        goFS.CreateTextFile("C:\Documents and Settings\aa24800\My Documents\PDF comparison\Result.TXT", True).Write sRes      
        TrialVersion = 0   Else      
        ' show failure      
        WScript.Echo "--------- Exp"      
        WScript.Echo sExp      
        WScript.Echo "not ok"      
        TrialVersion = 1   
    End If 
End Function ' TrialVersion  
'trivial Differ 
'Class cDiffer   
    Dim m_sLFSpec : m_sLFSpec = "C:\Documents and Settings\aa24800\My Documents\PDF comparison\A_30120625003267.TXT"
Dim m_sRFSpec : m_sRFSpec = "C:\Documents and Settings\aa24800\My Documents\PDF comparison\B_30120502002776.TXT"   
    ' "constructor" with params   
    Public Function init(sLFSpec, sRFSpec)     
        Set init  = Me     
        m_sLFSpec = sLFSpec     
        m_sRFSpec = sRFSpec   
    End Function   
    Public Function diffs()     
        diffs = "cDiffer.diffs() not implemented yet."   
    End Function ' diffs 
'End Class ' cDiffer00
'gold Differ 
'Class cDiffer   
'   Private m_sLFSpec   ' file specs   
'   Private m_sRFSpec   
    Private m_sLFiNa    ' file names   
    Private m_sRFiNa   
    Private m_dicLabels ' store and efficiently find selective labels   
    ' "constructor" with params   
    Public Function init(sLFSpec, sRFSpec, aLabels)     
        Set init  = Me     
        m_sLFSpec = sLFSpec     
        m_sRFSpec = sRFSpec     
        m_sLFiNa  = goFS.GetBaseName(sLFSpec)     
        m_sRFiNa  = goFS.GetBaseName(sRFSpec)     
        Set m_dicLabels = CreateObject("Scripting.Dictionary")     
        m_dicLabels.CompareMode = vbTextCompare ' case-insensitive     
        Dim sKey     
        For Each sKey In aLabels         
            m_dicLabels(sKey) = 0     
        Next   
    End Function   
    Public Function diffs()     ' Use ArrayList to collect the results     
        Dim alRes : Set alRes = CreateObject("System.Collections.ArrayList")     
        ' requested title     
        alRes.Add "Differences:"     
        ' open both input files     
        Dim tsL   : Set tsL   = goFS.OpenTextFile(m_sLFSpec)     
        Dim tsR   : Set tsR   = goFS.OpenTextFile(m_sRFSpec)     
        ' loop over lines     
        Do Until tsL.AtEndOfStream        
            Dim sLL : sLL = tsL.ReadLine()        
            Dim sRL        
            ' second file could be shorter        
            If tsR.AtEndOfStream Then           
                alRes.Add "tsR.AtEndOfStream"           
        Exit Do        
            Else           
                sRL = tsR.ReadLine()        
            End If        
            ' no need for work if lines are equal        
            If sLL <> sRL Then           
                If m_dicLabels.Exists(Split(sLL, ":")(0))Then                  
                Dim sLiNo : sLiNo = CStr(tsL.Line - 1)& ":"              
            alRes.Add Join(Array(m_sLFiNa, "Line", sLiNo, sLL))              
            alRes.Add Join(Array(m_sRFiNa, "Line", sLiNo, sRL))           
            End If        
        End If     
    Loop     
    tsL.Close     
    tsR.Close     
    diffs = Join(alRes.ToArray(), vbCrLf) & vbCrLf   
End Function ' diffs 
End Class ' cDiffer

Function GoldVersion()   
   ' the differ should know about the files to compare   
   ' and the info labels to select   
   Dim oDiffer : Set oDiffer = New cDiffer.init("C:\Documents and Settings\aa24800\My Documents\PDF comparison\A_30120625003267.TXT", "C:\Documents and Settings\aa24800\My Documents\PDF comparison\B_30120502002776.TXT", Array("Quote ID:", "Quote Summary for:", "Quote Date:", "Tracking ID (A):", "Tracking ID (Z):", "Tracking ID:")
   ' the differ should be able to return a result - the differences    
   Dim sRes : sRes = oDiffer.diffs()   
   ' check actual vs. expected result   
   Dim sExp : sExp = goFS.OpenTextFile("Expected").ReadAll()   
   WScript.Echo "--------- Res"   
   WScript.Echo sRes   
    If sExp = sRes Then      
       WScript.Echo "ok"      
       ' save result      
       goFS.CreateTextFile("C:\Documents and Settings\aa24800\My Documents\PDF comparison\Result.TXT", True).Write sRes      
       GoldVersion = 0   Else      
       ' show failure      
       WScript.Echo "--------- Exp"      
       WScript.Echo sExp      
       WScript.Echo "not ok"      
       GoldVersion = 1   
    End If 
End Function ' GoldVersion
4

1 回答 1

1

如果您是 VBScript 新手,您可能会从解决当前问题中获益,从而帮助您解决下一个问题。

从放置 SelFileComp.vbs 开始:

'' SelFileComp.vbs - selective file compare

Option Explicit

Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")

WScript.Quit TinVersion()

Function TinVersion()
  WScript.Echo "can't compare files yet."
  TinVersion = 1
End Function ' TinVersion

进入一些合适的目录。添加您的输入和预期结果文件。启动 SelFileComp.vbs:

cscript SelFileComp.vbs
can't compare files yet.
echo %ErrorLevel%
1

添加(并调用)一个 TrialVersion,它准备并使用(微不足道的)Differ 对象在适用于健全性检查实现的骨架中进行繁重的工作:

'' SelFileComp.vbs - selective file compare

Option Explicit

Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")

WScript.Quit TrialVersion()
WScript.Quit TinVersion()

Function TinVersion()
  WScript.Echo "can't compare files yet."
  TinVersion = 1
End Function ' TinVersion

Function TrialVersion()
  ' the differ should know about the files to compare
  ' we'll worry about the selection later
  Dim oDiffer : Set oDiffer = New cDiffer.init( _
     "File-1", "File-2" _
  )
  ' the differ should be able to return a result - the differences
  Dim sRes : sRes = oDiffer.diffs()
  ' check actual vs. expected result
  Dim sExp : sExp = goFS.OpenTextFile("Expected").ReadAll()
  WScript.Echo "--------- Res"
  WScript.Echo sRes
  If sExp = sRes Then
     WScript.Echo "ok"
     ' save result
     goFS.CreateTextFile("..\data\Result", True).Write sRes
     TrialVersion = 0
  Else
     ' show failure
     WScript.Echo "--------- Exp"
     WScript.Echo sExp
     WScript.Echo "not ok"
     TrialVersion = 1
  End If
End Function ' TrialVersion

' trivial Differ
Class cDiffer
  Private m_sLFSpec
  Private m_sRFSpec
  ' "constructor" with params
  Public Function init(sLFSpec, sRFSpec)
    Set init  = Me
    m_sLFSpec = sLFSpec
    m_sRFSpec = sRFSpec
  End Function
  Public Function diffs()
    diffs = "cDiffer.diffs() not implemented yet."
  End Function ' diffs
End Class ' cDiffer00

输出:

cscript SelFileComp.vbs
--------- Res
cDiffer.diffs() not implemented yet.
--------- Exp
Differences:
File-1 Line 4: Price 3: $1234.00
File-2 Line 4: Price 3: $5678.00
File-1 Line 7: Term: 2-Year
File-2 Line 7: Term: 3-Year

not ok

echo %ErrorLevel%
1

如果你然后重命名平凡的 cDiffer

' trivial Differ
Class cDiffer00

您可以重新使用名称(以及 TrialVersion() 中的代码)来提出一个 cDiffer,它至少可以进行一些比较:

' simple Differ
Class cDiffer
  Private m_sLFSpec
  Private m_sRFSpec
  ' "constructor" with params
  Public Function init(sLFSpec, sRFSpec)
    Set init  = Me
    m_sLFSpec = sLFSpec
    m_sRFSpec = sRFSpec
  End Function
  Public Function diffs()
    ' Use ArrayList to collect the results
    Dim alRes : Set alRes = CreateObject("System.Collections.ArrayList")
    ' requested title
    alRes.Add "Differences:"
    ' open both input files
    Dim tsL   : Set tsL   = goFS.OpenTextFile(m_sLFSpec)
    Dim tsR   : Set tsR   = goFS.OpenTextFile(m_sRFSpec)
    ' loop over lines
    Do Until tsL.AtEndOfStream
       Dim sLL : sLL = tsL.ReadLine()
       Dim sRL
       ' second file could be shorter
       If tsR.AtEndOfStream Then
          alRes.Add "tsR.AtEndOfStream"
          Exit Do
       Else
          sRL = tsR.ReadLine()
       End If
       ' no need for work if lines are equal
       If sLL <> sRL Then
          alRes.Add "??? " & sLL
          alRes.Add "??? " & sRL
       End If
    Loop
    tsL.Close
    tsR.Close
    diffs = Join(alRes.ToArray(), vbCrLf)
  End Function ' diffs
End Class ' cDiffer00

输出:

cscript SelFileComp.vbs
--------- Res
Differences:
??? Date: 00/11/1234 / Order no: 1111
??? Date: 11/00/6789 and Order no: 2222
??? Price 3: $1234.00
??? Price 3: $5678.00
??? Order no: 1111
??? Order no: 2222
??? Term: 2-Year
??? Term: 3-Year
??? Date: 00/11/1234
??? Date: 11/00/6789
--------- Exp
Differences:
File-1 Line 4: Price 3: $1234.00
File-2 Line 4: Price 3: $5678.00
File-1 Line 7: Term: 2-Year
File-2 Line 7: Term: 3-Year

not ok

这清楚地显示了哪些子任务仍需完成:

  1. 选择相关差异
  2. 输出格式

让我们保持乐观,添加并调用 GoldVersion()

Function GoldVersion()
  ' the differ should know about the files to compare
  ' and the info labels to select
  Dim oDiffer : Set oDiffer = New cDiffer.init( _
      "File-1", "File-2" _
    , Array("Price 3", "Term") _
  )
  ' the differ should be able to return a result - the differences
  Dim sRes : sRes = oDiffer.diffs()
  ' check actual vs. expected result
  Dim sExp : sExp = goFS.OpenTextFile("Expected").ReadAll()
  WScript.Echo "--------- Res"
  WScript.Echo sRes
  If sExp = sRes Then
     WScript.Echo "ok"
     ' save result
     goFS.CreateTextFile("..\data\Result", True).Write sRes
     GoldVersion = 0
  Else
     ' show failure
     WScript.Echo "--------- Exp"
     WScript.Echo sExp
     WScript.Echo "not ok"
     GoldVersion = 1
  End If
End Function ' GoldVersion

使用更好的 cDiffer:

' gold Differ
Class cDiffer
  Private m_sLFSpec   ' file specs
  Private m_sRFSpec
  Private m_sLFiNa    ' file names
  Private m_sRFiNa
  Private m_dicLabels ' store and efficiently find selective labels
  ' "constructor" with params
  Public Function init(sLFSpec, sRFSpec, aLabels)
    Set init  = Me
    m_sLFSpec = sLFSpec
    m_sRFSpec = sRFSpec
    m_sLFiNa  = goFS.GetBaseName(sLFSpec)
    m_sRFiNa  = goFS.GetBaseName(sRFSpec)
    Set m_dicLabels = CreateObject("Scripting.Dictionary")
    m_dicLabels.CompareMode = vbTextCompare ' case-insensitive
    Dim sKey
    For Each sKey In aLabels
        m_dicLabels(sKey) = 0
    Next
  End Function
  Public Function diffs()
    ' Use ArrayList to collect the results
    Dim alRes : Set alRes = CreateObject("System.Collections.ArrayList")
    ' requested title
    alRes.Add "Differences:"
    ' open both input files
    Dim tsL   : Set tsL   = goFS.OpenTextFile(m_sLFSpec)
    Dim tsR   : Set tsR   = goFS.OpenTextFile(m_sRFSpec)
    ' loop over lines
    Do Until tsL.AtEndOfStream
       Dim sLL : sLL = tsL.ReadLine()
       Dim sRL
       ' second file could be shorter
       If tsR.AtEndOfStream Then
          alRes.Add "tsR.AtEndOfStream"
          Exit Do
       Else
          sRL = tsR.ReadLine()
       End If
       ' no need for work if lines are equal
       If sLL <> sRL Then
          If m_dicLabels.Exists(Split(sLL, ":")(0)) Then
             alRes.Add Join(Array(m_sLFiNa, "Line", sLL))
             alRes.Add Join(Array(m_sRFiNa, "Line", sRL))
          End If
       End If
    Loop
    tsL.Close
    tsR.Close
    diffs = Join(alRes.ToArray(), vbCrLf)
  End Function ' diffs
End Class ' cDiffer

输出:

cscript SelFileComp.vbs
--------- Res
Differences:
File-1 Line Price 3: $1234.00
File-2 Line Price 3: $5678.00
File-1 Line Term: 2-Year
File-2 Line Term: 3-Year
--------- Exp
Differences:
File-1 Line 4: Price 3: $1234.00
File-2 Line 4: Price 3: $5678.00
File-1 Line 7: Term: 2-Year
File-2 Line 7: Term: 3-Year

not ok

选择完成,格式仍然不好。改善输出:

       If sLL <> sRL Then
          If m_dicLabels.Exists(Split(sLL, ":")(0)) Then
'            alRes.Add Join(Array(m_sLFiNa, "Line", sLL))
'            alRes.Add Join(Array(m_sRFiNa, "Line", sRL))
             Dim sLiNo : sLiNo = CStr(tsL.Line - 1) & ":"
             alRes.Add Join(Array(m_sLFiNa, "Line", sLiNo, sLL))
             alRes.Add Join(Array(m_sRFiNa, "Line", sLiNo, sRL))
          End If
       End If

添加尾随 vbCrLf:

'   diffs = Join(alRes.ToArray(), vbCrLf)
    diffs = Join(alRes.ToArray(), vbCrLf) & vbCrLf

最终输出:

cscript SelFileComp.vbs
--------- Res
Differences:
File-1 Line 4: Price 3: $1234.00
File-2 Line 4: Price 3: $5678.00
File-1 Line 7: Term: 2-Year
File-2 Line 7: Term: 3-Year

ok

echo %ErrorLevel%
0

下一个问题,拜托!

更新 A(wrt 文件规格/文件名)

将 File-1 移动/复制到 ..\data\,更改

  Dim oDiffer : Set oDiffer = New cDiffer.init( _
      "File-1", "File-2" _
    , Array("Price 3", "Term") _
  )

  Dim oDiffer : Set oDiffer = New cDiffer.init( _
      "..\data\File-1", "File-2" _
    , Array("Price 3", "Term") _
  )

结果将是相同的,因为 cDiffer 使用

m_sLFSpec = sLFSpec
  to store the (full) path
m_sLFiNa  = goFS.GetBaseName(sLFSpec)
  to extract the file name for output formatting
Dim tsL   : Set tsL   = goFS.OpenTextFile(m_sLFSpec)
  to open the file

更新 B(wrt 字典)

字典是在唯一键下存储元素的集合(与数组相反,数组可以通过数字访问其项目)。通过使用标签作为字典的键来查找,diffs() 函数可以有效地(看起来,没有循环!)检查该行的第一部分是否达到:

  Split(sLL, ":")(0)

包含在字典中

  If m_dicLabels.Exists(Split(sLL, ":")(0)) Then

更新 C(wrt 类/构造函数)

类是(一组相似的)对象的定义/规范,它是保存/组合数据(成员)和功能(方法)的变量。cDiffer 是一个定义对象的类,它“知道”所有关于要比较的文件和要查找的标签(成员变量,如 m_sLSpec),并且可以“进行”比较(方法/函数,如 diffs())。New 语句用于根据规范构造/创建对象:

  Dim oDiffer : Set oDiffer = New cDiffer

New 创建的对象是 Empty 的,对实际用途毫无用处;您可以实现 Class_Initialize() Sub(在 Class ... End Class 块中),但是由于该代码将用于该类的所有对象,因此收益很小。

如果您查看文档中Class语句的示例,您会意识到,无参数的“构造函数”(Class_Initialize)对于不按行/小时付费的程序员几乎没有用处。样板代码

   Private Sub Class_Initialize
      m_CustomerName = ""
      m_OrderCount = 0
      ... ad nauseam: set all other member data to 'nix'
   End Sub

特别恶心,因为VBScript

  • 只要您调用 New,就会自动执行相当于空的 Class_Initialize
  • 自动将所有变量初始化为 Empty 并且 Empty 将
    适用于字符串和数字

补救方法是忘记Class_Initialize(特殊情况除外),在一个或多个上投入一些精力

  Public Function initXXX(p1, p2, ... pn)
    Set initXXX = Me  ' return VBScript's this to caller
    ... use p1 ... pn to initialize member data to useful values
  End Function
于 2012-09-06T17:55:11.623 回答