2017-08-07 346 views
0

我在嘗試使用VBScript對Z-A文本文件(CSV文件)進行排序。 我的文本文件看起來像:按字母順序排序

 
ZYUIP, ALGORITHM,TESTING,\ ,TABLE1 

我想訂購從Z-A中的CSV文件,但我阻止把CSV文件導入陣列。

這是由Z-A定購陣列的代碼:

ArrayOfTerms = Array("B","A","C","D") 

For a = UBound(ArrayOfTerms) - 1 To 0 Step -1 
    For j= 0 To a 
     If ArrayOfTerms(j)<ArrayOfTerms(j+1) Then 
      temp = ArrayOfTerms(j+1) 
      ArrayOfTerms(j+1) = ArrayOfTerms(j) 
      ArrayOfTerms(j) = temp 
     End If 
    Next 
Next 
+0

您的csv文件包含項目符號點嗎?是否應該有其中的優點,並且只是格式不正確? – JNevill

+0

我的CSV沒有項目符號點。我會糾正我的問題 –

+0

你想按字母順序排列每一行的字段嗎?或者你想按字母順序排序CSV的行嗎?在後者的情況下:通過哪一列? –

回答

0

如果您不能下載.Net框架。這是我的解決方案(不優化)將CSV文件從Z排序到A:

'--------------------------------------- 
' Load a CSV File into a VBScript Array 
'---------------------------------------- 
Function CSVArray(CSVFile) 

    comma = "," 
    quote = Chr(34) 

    colMax = -1 

    rowCount  = -1 
    Set inCsvSys = CreateObject("Scripting.FileSystemObject") 
    Set inCsv = inCsvSys.OpenTextFile(CSVFile,"1",True) 
    Do While Not inCsv.AtEndOfStream 
    rowCount = rowCount + 1 
    Redim Preserve inRow(rowCount) 
    inRow(rowCount) = inCsv.ReadLine 
    Loop 
    inCsv.Close 

    For r = 0 to rowCount 

    csvRecord = inRow(r) 
    colNum = -1 
    charPos = 0 
    cellComplete = True 

    Do While charPos < Len(csvRecord) 

     If (cellComplete = True) Then 
     colNum  = colNum + 1 
     cellPos  = 0 
     cellQuoted = False 
     cellComplete = False 
     If colNum > colMax Then 
      colMax = colNum 
      Redim Preserve cellArray(rowCount,colMax) 
     End If    
     End If 

     charPos = charPos + 1 
     cellPos = cellPos + 1 
     charVal = Mid(csvRecord, charPos, 1) 
     If (charVal = quote) Then 
     If (cellPos = 1) Then 
      cellQuoted = True 
      charVal = "" 
     Else 
      Select Case Mid(csvRecord, charPos+1, 1) 
      Case quote 
      charPos = charPos + 1 
      Case comma 
      charPos = charPos + 1 
      cellComplete = True 
      End Select 
     End If 
     ElseIf (charVal = comma) And (cellQuoted = False) Then 
     cellComplete = True 
     End If 
     If (cellComplete = False) Then 
     cellArray(r,colNum) = cellArray(r,colNum)&charVal 
     End If 

    Loop 

    Next 
    CSVArray = cellArray 
End Function 



csv = CSVArray("\\Ad.ing.net\wps\BE\D\UD\002001\D-JS15GY\Desktop\migrate_RA\externalfiles.csv") 

Set fso = WScript.CreateObject("Scripting.Filesystemobject") 
Set objFSO = CreateObject("Scripting.FileSystemObject") 
Set objFile=objFSO.CreateTextFile("\\Ad.ing.net\wps\BE\D\UD\002001\D-JS15GY\Desktop\migrate_RA\externalfiles2.csv",2,true) 

'------------------------------ 
'Order the CSV File from Z to A 
'------------------------------ 

For a = UBound(csv,1) - 1 To 0 Step -1 
    for j= 0 to a 
    if csv(j,0)<csv(j+1,0) then 
    temp=csv(j+1,0) 
    csv(j+1,0)=csv(j,0) 
     csv(j,0)=temp 

    End If 
Next 

Next 

For j = 0 to Ubound(csv,1) 

    objFile.WriteLine csv(j,0) 

Next 
0

可以使用ArrayList.Sort從.NET框架。項目必須逐個輸入。

Dim ArrayList : Set ArrayList = CreateObject("System.Collections.ArrayList") 

'Add your items here: 
ArrayList.Add "B" : ArrayList.Add "A" : ArrayList.Add "C" : ArrayList.Add "D" 

ArrayList.Sort 
ArrayList.Reverse