2012-09-06 81 views
0

首先,我不是從編程背景和全新的VBscript。出於某些原因,我必須在辦公室完成這個腳本任務。 我想使用它與快速測試專業11.比較文本文件 - 使用VB編寫Igonring一些文本

我已經在這裏經歷了很多帖子以及在其他論壇,但無法找到所需的信息。

好了,所以這裏就是我需要做的:

我要比較兩個文本文件,寫在第三個文件的差異。 這兩個文件都具有幾乎相同的內容aprt從一些領域,即:日期,訂單號等。

例如,File-1有Date:00/11/1234和Order no:1111,File-2有Date:11/00/6789和Order no: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 
+0

你有什麼迄今寫的嗎? –

+0

@ user1652222通過以隨機順序將選定部分的代碼複製並粘貼到QTP中,您創建了一個語法混亂。如果您通過我提出的(.vbs /命令行ui)步驟工作,您將獲得將解決方案整合到QTP中的知識(並瞭解預期結果文件的作用和相關性)。我會在這裏提出一個關於QTP語法錯誤的問題 –

回答

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迥異對象 做適合理智的骨架繁重檢查落實:

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

選擇DONE ,仍然格式化不好。爲了提高輸出:

 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文件規格/文件名)

移動/複製文件-1 ..\數據\,更改

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字典)

dictionary是一個存儲un元素的集合ique鍵(如 與數組相反,這使得它的項可以通過數字訪問)。通過 使用標籤來尋找一個字典的鍵,將diff文件() 功能,可以有效地檢查,是否該行的 第一部分高達的(看馬,沒有循環!):

Split(sLL, ":")(0) 

被包含在字典

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

更新C(WRT類/構造函數)

類是一(組simil的定義/規範ar)對象,即變量保存/組合數據(成員)以及功能(方法)的 。 cDiffer是一個定義對象的類,它可以「知道」所有關於要比較的文件和要查找的標籤(成員變量,如m_sLSpec)以及可以「做」比較(方法/函數,如diffs())。根據規格的新語句用於構造/創建對象:

Dim oDiffer : Set oDiffer = New cDiffer 

通過新創建的對象是空的,即沒有用出於實用的目的;你可以在 中實現一個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

  • 作爲 自動執行空Class_Initialize相當於只要你撥打新
  • 對所有變量自動和虛空將
    正常工作的字符串和數字

補救的辦法是忘記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 
+0

非常感謝@ Ekkehard.Horner的幫助。我真的很感激。我需要花些時間來理解你的代碼。如果你能解釋我如何在我的函數庫中使用你的代碼,那將是非常棒的。而且文件規格和文件名稱的情況如何,如果我傳遞完整的文件路徑(包括文件名)?我不明白:私人m_dicLabels的商店,並有效地找到選擇性標籤的「構造」與參數這是你談論的字典對象的東西......?如果我的問題很煩人,請原諒我。在此先感謝您的幫助。 – ManishChristian

+0

嗨@ Ekkehard.Horner非常感謝您的快速回復和解釋。我仍然與這個cDiffer混淆。那是什麼,從哪裏來,我仍然不明白。再一次非常感謝你。 – ManishChristian

+0

作爲關於構造函數的一個注意事項:在QTP中,創建一個公共函數_outside_是一種很好的做法,它按照Ekkehard.Horner用'initXXX'函數解釋的方式進行構造並返回一個對象實例。爲什麼? QTP中的類對於它們所在的庫來說始終是私有的。通過創建一個返回對象實例的公共函數,可以將該類作爲公共類使用。現在,您可以將每個類的.QFL文件中的類保存在單獨的文件中;就像你在Java中所做的一樣。 – AutomatedChaos