我正在尝试确定提供给我的应用的数据文件确实没有列,以便它不会在应用程序中失败。此外,我想计算行数并检查是否将相同数量的行加载到DB。
我还需要检查每行是否包含相同数量的列。
我试着把它写成如下,但仍然在努力解决结果。
Dim objFSO, dataArray, clippedArray
Set objFSO = CreateObject("Scripting.FileSystemObject")
'Create an array out of the CSV
'open the data file
Set oTextStream = objFSO.OpenTextFile("C:\Users\ds\Desktop\Quota\new_file.csv")
Set newFile = objFSO.CreateTextFile("C:\Users\ds\Desktop\loginfo.txt")
'make an array from the data file
dataArray = Split(oTextStream.ReadAll, vbNewLine)
'close the data file
oTextStream.Close
WScript.Echo "No. of Rows is " & UBound(dataArray)
For Each strLine In dataArray
'Now make an array from each line
clippedArray = Split(strLine,"|")
lngHowManyColumns = UBound(clippedArray)
WScript.Echo UBound(clippedArray)
Next
WScript.Echo "No. of Columns is " & lngHowManyColumns
WScript.Echo "Done"
编辑更有意义,因为很少有专家无法理解这个问题,但是下面已经回答过。
由于
答案 0 :(得分:4)
保持简单:循环遍历行,通过拆分检查每行中的字段sesh :
Option Explicit
Const csFSpec = "..\data\47261155.csv"
Const csFSep = ","
Const cnCols = 3 ' one less to optimize for UBound()!
Const cnRows = 6 ' one more to optimize for .Line
Dim goFS : Set goFS = CreateObject("Scripting.FileSystemObject")
Dim oTS : Set oTS = goFS.OpenTextFile(csFSpec)
Dim sL, nC
Do Until oTS.AtEndOfStream
sL = oTS.ReadLine()
nC = UBound(Split(sL, csFSep))
If cnCols <> nC Then
WScript.Echo "Col Error in line", oTS.Line - 1 & ":", cnCols, "<>", nC, "(" & sL & ")"
End If
Loop
If cnRows <> oTS.Line Then
WScript.Echo "Row Error: ", cnRows, "<>", oTS.Line
End If
oTS.Close
WScript.Echo "no news is good news"
输出:
type ..\data\47261155.csv
1,2,3,4
4,3,2,1
1,2,3,4,5
1,2,3
1,2,3,4
1,2,3,4
cscript 47261155.vbs
Col Error in line 3: 3 <> 4 (1,2,3,4,5)
Col Error in line 4: 3 <> 2 (1,2,3)
Row Error: 6 <> 7
no news is good news