2

我有一堆文本文件需要导入 MS Access(数千个)——可以使用 2007 或 2010。文本文件的类别在方括号中标识,并且类别之间有相关数据——例如:

[位置]田纳西州[位置][型号]042200[型号][零件编号]113342A69447B6[零件编号]。

我需要捕获类别和它们之间的数据并将它们导入 Access - 一个表的类别,另一个表的数据。一个文件中有数百个这样的类别,文本文件没有结构——它们都像上面的例子一样一起运行。括号中的类别是唯一明确的分隔符。

通过网络研究,我想出了一个 VBS 脚本(我没有锁定 VBS,愿意使用 VBA 或其他方法),但是当我运行它时,我得到一个 VBS 信息窗口,其中没有显示任何内容。任何建议或指导将不胜感激(我不倾向于使用 VBS 和 VBA),我谢谢你。

剧本:

Const ForReading = 1

    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set objFile = objFSO.OpenTextFile("C:\Users\testGuy\Documents\dmc_db_test\DMC-TEST-A-00-00-00-00A-022A-D_000 - Copy01.txt", ForReading)

    strContents = objFile.ReadAll
    objFile.Close

    Set objRegEx = CreateObject("VBScript.RegExp")

    objRegEx.Global = True   
    objRegEx.Pattern = "\[.{0,}\]"

    Set colMatches = objRegEx.Execute(strContents)  

    If colMatches.Count > 0 Then
       For Each strMatch in colMatches   
           strMatches = strMatches & strMatch.Value 
       Next
    End If

    strMatches = Replace(strMatches, "]", vbCrlf)
    strMatches = Replace(strMatches, "[", "")

    Wscript.Echo strMatches
4

1 回答 1

4

正则表达式是很棒的东西,但在你的情况下,它们看起来可能有点矫枉过正。以下代码使用普通旧代码InStr()来查找[Tags]并将文件解析为单个 CSV 文件。也就是说,对于输入文件

测试文件1.txt:

[Location]Tennessee[Location][Model]042200[Model][PartNo]113342A69447B6[PartNo]
[Location]Mississippi[Location][Model]042200[Model][SerialNo]3212333222355[SerialNo]

和 testfile2.txt:

[Location]Missouri[Location][Model]042200[Model][PartNo]AAABBBCCC111222333[PartNo]

...代码将写入以下输出文件...

"FileName","LineNumber","ItemNumber","FieldName","FieldValue"
"testfile1.txt",1,1,"Location","Tennessee"
"testfile1.txt",1,2,"Model","042200"
"testfile1.txt",1,3,"PartNo","113342A69447B6"
"testfile1.txt",2,1,"Location","Mississippi"
"testfile1.txt",2,2,"Model","042200"
"testfile1.txt",2,3,"SerialNo","3212333222355"
"testfile2.txt",1,1,"Location","Missouri"
"testfile2.txt",1,2,"Model","042200"
"testfile2.txt",1,3,"PartNo","AAABBBCCC111222333"

...然后您可以将其导入 Access(或其他)并从那里继续。这是 VBA 代码,但可以很容易地对其进行调整以作为 VBScript 运行。

Sub ParseSomeFiles()
Const InFolder = "C:\__tmp\parse\in\"
Const OutFile = "C:\__tmp\parse\out.csv"
Dim fso As FileSystemObject, f As File, tsIn As TextStream, tsOut As TextStream
Dim s As String, Lines As Long, Items As Long, i As Long
Set fso = New FileSystemObject
Set tsOut = fso.CreateTextFile(OutFile, True)
tsOut.WriteLine """FileName"",""LineNumber"",""ItemNumber"",""FieldName"",""FieldValue"""
For Each f In fso.GetFolder(InFolder).Files
    Debug.Print "Parsing """ & f.Name & """..."
    Set tsIn = f.OpenAsTextStream(ForReading)
    Lines = 0
    Do While Not tsIn.AtEndOfStream
        s = Trim(tsIn.ReadLine)
        Lines = Lines + 1
        Items = 0
        Do While Len(s) > 0
            Items = Items + 1
            tsOut.Write """" & f.Name & """," & Lines & "," & Items
            i = InStr(1, s, "]", vbBinaryCompare)
            ' write out FieldName
            tsOut.Write ",""" & Replace(Mid(s, 2, i - 2), """", """""", 1, -1, vbBinaryCompare) & """"
            s = Mid(s, i + 1)
            i = InStr(1, s, "[", vbBinaryCompare)
            ' write out FieldValue
            tsOut.Write ",""" & Replace(Mid(s, 1, i - 1), """", """""", 1, -1, vbBinaryCompare) & """"
            s = Mid(s, i)
            i = InStr(1, s, "]", vbBinaryCompare)
            ' (no need to write out ending FieldName tag)
            s = Mid(s, i + 1)
            tsOut.WriteLine
        Loop
    Loop
    tsIn.Close
    Set tsIn = Nothing
Next
Set f = Nothing
tsOut.Close
Set tsOut = Nothing
Set fso = Nothing
Debug.Print "Done."
End Sub
于 2013-10-08T21:24:10.253 回答