VBS对文本文件的行进行排序

时间:2018-02-07 16:59:26

标签: sorting vbscript

我正在寻找一种对文本文件行进行排序的好方法,我不知道最好的方法是使用数组。这是我的档案:

VW;blue;20000;link
VW;blue;21000;link
VW;blue;29000;link
VW;blue;28000;link
VW;blue;22000;link
VW;red;20000;link
VW;red;28000;link
VW;red;30000;link
VW;red;21000;link
VW;red;26000;link
MERCEDES;blue;30000;link
MERCEDES;blue;38000;link
MERCEDES;blue;40000;link
MERCEDES;blue;31000;link
MERCEDES;blue;37000;link
MERCEDES;red;40000;link
MERCEDES;red;40000;link
MERCEDES;red;47000;link
MERCEDES;red;41000;link
MERCEDES;red;44000;link

我需要重新组织一个文件,通过对每个品牌的汽车进行分类,蓝色的价格更高,红色价格更便宜。 (只有这两种颜色)。我正在寻找的结果如下:

VW;blue;29000;link
VW;blue;28000;link
VW;blue;22000;link
VW;blue;21000;link
VW;blue;20000;link
VW;red;20000;link
VW;red;21000;link
VW;red;26000;link
VW;red;28000;link
VW;red;30000;link
MERCEDES;blue;40000;link
MERCEDES;blue;38000;link
MERCEDES;blue;37000;link
MERCEDES;blue;31000;link
MERCEDES;blue;30000;link
MERCEDES;red;40000;link
MERCEDES;red;40000;link
MERCEDES;red;41000;link
MERCEDES;red;44000;link

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

数据库问题应该通过数据库工具(ADO,ODBC,Schema.ini,SQL,IIF)来解决。与此演示中一样(64 Bit, Access driver):

Option Explicit

Const adClipString = 2

Dim oFS : Set oFS = CreateObject("Scripting.FileSystemObject")
WScript.Echo oFS.OpenTextFile("schema.ini").ReadAll()
WScript.Echo "--------------------------"
Dim oDb : Set oDb = CreateObject("ADODB.Connection")
Dim sCS : sCS = Join(Array( _
                    "Driver=Microsoft Access Text Driver (*.txt, *.csv)" _
                  , "Dbq=" & oFS.GetAbsolutePathName(".") _
                  , "Extensions=asc,csv,tab,txt" _
                ), ";")
WScript.Echo sCS
WScript.Echo "--------------------------"
oDb.Open sCS
Dim sSQL 
For Each sSQL In Array( _
                    "SELECT * FROM [48669323.csv]" _
                  , "SELECT * FROM [48669323.csv] ORDER BY Brand, Color, Price" _
                  , "SELECT * FROM [48669323.csv] ORDER BY Brand, Color, IIF(Color='red', Price * -1, Price)" _
                 )
    WScript.Echo sSQL
    WScript.Echo "--------------------------"
    WScript.Echo oDb.Execute(sSQL).GetString(adClipString, , ",", vbCrLf, "<NULL>")
    WScript.Echo "--------------------------"
Next
oDb.Close    

输出:

cscript 48669323.vbs
[48669323.csv]
Format=Delimited(;)
ColNameHeader=False
Col1=Brand Text
Col2=Color Text
Col3=Price Long
Col4=WTF Text

--------------------------
Driver=Microsoft Access Text Driver (*.txt, *.csv);Dbq=C:\Users\eh\tmp;Extensions=asc,csv,tab,txt
--------------------------
SELECT * FROM [48669323.csv]
--------------------------
VW,blue,20000,link
VW,blue,21000,link
VW,blue,29000,link
VW,blue,28000,link
VW,blue,22000,link
VW,red,20000,link
VW,red,28000,link
VW,red,30000,link
VW,red,21000,link
VW,red,26000,link
MERCEDES,blue,30000,link
MERCEDES,blue,38000,link
MERCEDES,blue,40000,link
MERCEDES,blue,31000,link
MERCEDES,blue,37000,link
MERCEDES,red,40000,link
MERCEDES,red,40000,link
MERCEDES,red,47000,link
MERCEDES,red,41000,link
MERCEDES,red,44000,link

--------------------------
SELECT * FROM [48669323.csv] ORDER BY Brand, Color, Price
--------------------------
MERCEDES,blue,30000,link
MERCEDES,blue,31000,link
MERCEDES,blue,37000,link
MERCEDES,blue,38000,link
MERCEDES,blue,40000,link
MERCEDES,red,40000,link
MERCEDES,red,40000,link
MERCEDES,red,41000,link
MERCEDES,red,44000,link
MERCEDES,red,47000,link
VW,blue,20000,link
VW,blue,21000,link
VW,blue,22000,link
VW,blue,28000,link
VW,blue,29000,link
VW,red,20000,link
VW,red,21000,link
VW,red,26000,link
VW,red,28000,link
VW,red,30000,link

--------------------------
SELECT * FROM [48669323.csv] ORDER BY Brand, Color, IIF(Color='red', Price * -1, Price)
--------------------------
MERCEDES,blue,30000,link
MERCEDES,blue,31000,link
MERCEDES,blue,37000,link
MERCEDES,blue,38000,link
MERCEDES,blue,40000,link
MERCEDES,red,47000,link
MERCEDES,red,44000,link
MERCEDES,red,41000,link
MERCEDES,red,40000,link
MERCEDES,red,40000,link
VW,blue,20000,link
VW,blue,21000,link
VW,blue,22000,link
VW,blue,28000,link
VW,blue,29000,link
VW,red,30000,link
VW,red,28000,link
VW,red,26000,link
VW,red,21000,link
VW,red,20000,link

--------------------------

另请参阅:ABC