2011-07-09 67 views
0
解決

能否請您在VBScript試試這個: 你有幾個貨幣金額的文本文件,每一個在一個行格式如下:一個小問題在VBScript

DESCRIPTION Date Amount 

McDonalds 2011-01-01 20.00 
Clothing Store 2011-01-02 30.00 

嘗試建立一個程序閱讀該文本文件並對其中找到的所有值進行求和。

+2

好吧,我試過。 –

回答

2

優先(行人)嘗試:

Option Explicit 

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

Dim sFSpec : sFSpec = goFS.GetAbsolutePathName("..\data\expenses.csv") 
WScript.Echo goFS.OpenTextFile(sFSpec).ReadAll() 
WScript.Echo "---------" 
Dim tsIn : Set tsIn = goFS.OpenTextFile(sFSpec) 
Dim nSum : nSum  = 0.0 
Dim sLine : sLine = tsIn.ReadLine() 
Do Until tsIn.AtEndOfStream 
    sLine = tsIn.ReadLine() 
    Dim aFields : aFields = Split(sLine, ",") 
    If 2 = UBound(aFields) Then 
     nSum = nSum + CDbl(aFields(2)) ' en_us, ... 
'  nSum = nSum + CDbl(Replace(aFields(2), ".", ",")) ' locale de_de, ... 
    End If 
Loop 
tsIn.Close 
WScript.Echo "Sum:", nSum  

輸出:

lineLoop - sum values (line loop) 
--------------------------------------------------- 
"DESCRIPTION","Date","Amount" 
"McDonalds",2011-01-01,20.00 
"Clothing Store",2011-01-02,30.00 
"paying programmer",2011-07-09,47.11 

--------- 
Sum: 97.11 
=================================================== 
xpl.vbs: Erfolgreich beendet. (0) [ 0,01563 secs ] 

第二次嘗試(使用正則表達式):

Dim sFSpec : sFSpec = goFS.GetAbsolutePathName("..\data\expenses.csv") 
Dim sAll : sAll = goFS.OpenTextFile(sFSpec).ReadAll() 
WScript.Echo sAll 
WScript.Echo "---------" 
Dim reAmount : Set reAmount = New RegExp 
reAmount.Global = True 
reAmount.MultiLine = True 
reAmount.Pattern = ",(\d+\.\d+)\r$" 
Dim nSum  : nSum   = 0.0 
Dim oMTS  : Set oMTS  = reAmount.Execute(sAll) 
Dim oMT 
For Each oMT In oMTS 
    nSum = nSum + CDbl(oMT.Submatches(0)) ' en_us, ... 
' nSum = nSum + CDbl(Replace(oMT.Submatches(0), ".", ",")) ' locale de_de, ... 
Next 
WScript.Echo "Sum:", nSum  

第三次嘗試(ADO /文本驅動) :

個SCHEMA.INI:

[expenses.csv] 
Format=CSVDelimited 
ColNameHeader=True 
DateTimeFormat=YYYY-MM-DD 
CharacterSet=ANSI 
Col1=DESCRIPTION CHAR 
Col2=Date DATE 
Col3=Amount FLOAT 

代碼:

Const adClipString = 2 

Dim sTDir : sTDir = goFS.GetAbsolutePathName("..\data") 
Dim oTDb : Set oTDb = CreateObject("ADODB.Connection") 
Dim sCS : sCS  = Join(Array(_ 
    "Provider=Microsoft.Jet.OLEDB.4.0" _ 
, "Data Source=" & sTDir _ 
, "Extended Properties=" & Join(Array(_ 
     "text" _ 
    ), ";") _ 
), ";") 
oTDB.Open sCS 
WScript.Echo oTDb.Execute("SELECT * FROM [expenses.csv]") _ 
    .GetString(adClipString, , vbTab, vbCrLf, "") 
WScript.Echo oTDb.Execute("SELECT SUM(Amount) AS [Sum] FROM [expenses.csv]") _ 
    .GetString(adClipString, , vbTab, vbCrLf, "") 
WScript.Echo oTDb.Execute("SELECT SUM(Amount) AS [Sum] FROM [expenses.csv] WHERE MONTH([Date]) = 7") _ 
    .GetString(adClipString, , vbTab, vbCrLf, "") 
oTDb.Close 

輸出:

McDonalds  01.01.2011  20 
Clothing Store 02.01.2011  30 
paying programmer  09.07.2011  47,11 

97,11 

47,11 

=================================================== 
xpl.vbs: Erfolgreich beendet. (0) [ 0,23438 secs ] 

或 - 用多一點額外的工作:

useAdoText - sum values (ADO/Text driver) 
---------------------------------------------------------------------------- 
---------------------------------------------------------------------------- 
SELECT * FROM [expenses.csv] 
---------------------------------------------------------------------------- 
|DESCRIPTION  |Date  |Amount| 
|McDonalds  |01.01.2011| 20| 
|Clothing Store |02.01.2011| 30| 
|paying programmer|09.07.2011| 47,11| 
---------------------------------------------------------------------------- 
---------------------------------------------------------------------------- 
SELECT SUM(Amount) AS [Sum] FROM [expenses.csv] 
---------------------------------------------------------------------------- 
|Sum | 
|97,11| 
---------------------------------------------------------------------------- 
---------------------------------------------------------------------------- 
SELECT SUM(Amount) AS [Sum] FROM [expenses.csv] WHERE MONTH([Date]) = 7 
---------------------------------------------------------------------------- 
|Sum | 
|47,11| 
---------------------------------------------------------------------------- 
============================================================================ 
xpl.vbs: Erfolgreich beendet. (0) [ 0,25000 secs ]