有人可以帮我重新格式化/使用VBScript正确格式化分隔文本文件吗?
我有一个^
分隔的文本文件,如下所示:
AGREE^NAME^ADD1^ADD2^ADD3^ADD4^PCODE^BAL^ARREARS
00010004000051162^MISS JENNIFER GRAY ^123 FAKE STREET ^ ^TOWN ^COUNTY ^POSTCODE ^ 004978.00^ 000188.72
所有数据都包含需要删除的前导和尾随空格。我只有VBScript可以做到这一点。
我曾尝试使用ADO GetStrings,但由于前导和尾随空格,它产生了不一致的结果。
任何人都可以提供任何建议或替代方案吗?
由于
答案 0 :(得分:3)
使用ADO文本文件时,应该从表定义开始 在approriate schema.ini文件中:
[agree.txt]
Format=Delimited(^)
ColNameHeader=True
DecimalSymbol=.
CharacterSet=ANSI
TextDelimiter=None
Col1=AGREE CHAR
Col2=NAME CHAR
Col3=ADD1 CHAR
Col4=ADD2 CHAR
Col5=ADD3 CHAR
Col6=ADD4 CHAR
Col7=PCODE CHAR
Col8=BAL FLOAT
Col9=ARREARS FLOAT
然后您可以访问您的数据:
Dim sTDir : sTDir = goFS.GetAbsolutePathName( "..\data" )
Dim sTbl1 : sTbl1 = "agree.txt"
Dim sFSpec : sFSpec = goFS.BuildPath(sTDir, sTbl1)
Dim sTbl2 : sTbl2 = "agree2.txt"
WScript.Echo "src file with spaces:"
WScript.Echo goFS.OpenTextFile(sFSpec).ReadAll()
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 "trimmed automagically:"
WScript.Echo oTDb.Execute(Replace("SELECT * FROM [@T]", "@T", sTbl1)) _
.GetString( adClipString, , "|", vbCrLf, "" )
输出:
src file with spaces:
AGREE^NAME^ADD1^ADD2^ADD3^ADD4^PCODE^BAL^ARREARS
00010004000051162^MISS JENNIFER GRAY ^123 FAKE STREET ^ ^TOWN ^COUNTY ^POSTCODE ^ 004978.00^ 000188.72
trimmed automagically:
00010004000051162|MISS JENNIFER GRAY|123 FAKE STREET||TOWN|COUNTY|POSTCODE|4978|188,72
如您所见,如果您使用虚假空间,则没有任何问题 正确的工具。
要获得干净的副本,只需添加
即可 sFSpec = goFS.BuildPath(sTDir, sTbl2)
If goFS.FileExists(sFSpec) Then goFS.DeleteFile sFSpec
Dim sSQL : sSQL = Replace(Replace( _
"SELECT * INTO [@T2] FROM [@T1]" _
, "@T1", sTbl1), "@T2", sTbl2)
WScript.Echo "Copy statement"
WScript.Echo sSQL
oTDb.Execute sSQL
WScript.Echo "QED: no spurious whilespace"
WScript.Echo goFS.OpenTextFile(sFSpec).ReadAll()
输出:
Copy statement
SELECT * INTO [agree2.txt] FROM [agree.txt]
QED: no spurious whilespace
"AGREE";"NAME";"ADD1";"ADD2";"ADD3";"ADD4";"PCODE";"BAL";"ARREARS"
"00010004000051162";"MISS JENNIFER GRAY";"123 FAKE STREET";;"TOWN";"COUNTY";"POSTCODE";4978,00;188,72
驱动程序将添加
[agree2.txt]
ColNameHeader=True
CharacterSet=1252
Format=Delimited(;)
Col1=AGREE Char Width 255
Col2=NAME Char Width 255
Col3=ADD1 Char Width 255
Col4=ADD2 Char Width 255
Col5=ADD3 Char Width 255
Col6=ADD4 Char Width 255
Col7=PCODE Char Width 255
Col8=BAL Float
Col9=ARREARS Float
(德语语言环境默认值)到schema.ini;编辑此内容
[agree2.txt]
Format=Delimited(^)
ColNameHeader=True
DecimalSymbol=.
CharacterSet=ANSI
TextDelimiter=None
Col1=AGREE CHAR
...
重新创建原始格式:
QED: no spurious whilespace
AGREE^NAME^ADD1^ADD2^ADD3^ADD4^PCODE^BAL^ARREARS
00010004000051162^MISS JENNIFER GRAY^123 FAKE STREET^^TOWN^COUNTY^POSTCODE^4978.00^188.72
通过使用更复杂的SQL语句和/或增强表定义,您可以以一种直接的方式解决更复杂的任务。
<强> P.S。强>
查看here,如果
Dim sSQL : sSQL = Replace(Replace( _
"SELECT * INTO [@T2] FROM [@T1]" _
, "@T1", sTbl1), "@T2", sTbl2)
让你想知道(@T1和@T2是不 ADO命令参数;嵌套的Replace()调用在之前应用 ADO看到当时修改过的语句。)