2013-10-08 138 views
1

我有一堆需要導入到MS Access(文本文件)的文本文件 - 可以使用2007或2010.文本文件的類別是在方括號中鑑定並具有類別之間的相關數據 - 例如:解析並將非結構化文本文件導入到Microsoft Access中(文件具有潛在分隔符)

[位置] Tenessee [位置] [模型] 042200 [模型] [PARTNO] 113342A69447B6 [PARTNO]。

我需要他們之間同時捕獲的類別和數據,並將它們導入到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 

回答

3

正則表達式是奇妙的東西,但在你的情況下,它看起來像他們可能是矯枉過正。以下代碼使用普通舊版InStr()來查找[Tags]並將文件解析爲單個CSV文件。也就是說,對於輸入文件

testfile1.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 
+0

謝謝你的答覆 - 非常感謝。我現在試着去實現它 - 因爲這是VBA這個東西,我可以把它放在Access窗體或Excel中的按鈕後面並讓它執行? – WCD

+0

@WCD當我在Excel模塊中測試它時,我只是將光標放在代碼的第一行,然後按'F5'運行它。但是,是的,您也可以將它附加到按鈕上。 –

相關問題