使用VB计数文件中的项目

时间:2019-12-02 18:29:40

标签: vbscript count scripting delimiter

VBS的新功能。我正在尝试计算文件中的字段并获取此代码。

Col()
Function Col()

Const FSpec = "C:\test.txt"
Const del = ","

  dim fs : Set fs = CreateObject("Scripting.FileSystemObject")
  dim f : Set f = fs.OpenTextFile(FSpec, 1)
  Dim L, C

  Do Until f.AtEndOfStream
   L = f.ReadLine()
   C = UBound(Split(L, del))
   C = C +1

   WScript.Echo "Items:", C

  Loop
  f.Close

End Function

但是它有效,我不想在“”中计算出delim。

这里的文件内容:

  

1,“ 2,999”,3

所以基本上,我现在要买4件,但我想买3件。

1 个答案:

答案 0 :(得分:0)

作为第二个建议的示例,一个非常简单的示例可能是这样的。并不是说它是完美的,而是说明了这个想法:

Dim WeAreInsideQuotes  'global flag
Function RemoveQuotedCommas(ByVal line)
    Dim i
    Dim result
    Dim current
    For i = 1 To Len(line)
        current = Mid(line, i, 1) 'examine character
        'check if we encountered a quote
        If current = Chr(34) Then
            WeAreInsideQuotes = Not WeAreInsideQuotes 'toggle flag
        End If
        'process the character
        If Not (current = Chr(44) And WeAreInsideQuotes) Then 'skip if comma and insode quotes
            result = result & current
        End If
    Next
    RemoveQuotedCommas = result
End Function