2011-01-05 61 views
1

我是新來的vbscript,我遇到了一些應該相對容易的麻煩。我有兩個非常大的文本文件,我必須根據文件中的某些行分割成許多不同的文本文件。這裏是文本文件的樣本:vbscript分裂非常大的文本文件

F1 SA1056-540X0 
F21 All_Tools 
F3 123229 99819 30MIL 
F3 317229 99819 30MIL 
F5 0 0 
F51 0 0 
F6 136103 204045 
F7 0 0 
F8 265094 249728 90000 1 N N 455229 
F9 C1A 
F8 265094 208328 90000 1 N N 455229 
F9 C1B 
F12 0.125000 250 0 
F1 SA1056-550X1 
F21 All_Tools 
F3 123229 99819 30MIL 
F3 317229 99819 30MIL 
F5 0 0 
F51 0 0 
F6 136103 204045 
F7 0 0 
F8 265094 249728 90000 1 N N 455229 
F9 C1A 
F8 265094 208328 90000 1 N N 455229 
F9 C1B 
F12 0.125000 250 0 

的F1線將提供新的文本文件的名稱,而F12線將在新的文本文件的最後一行。下一個F1將成爲下一個文件,依此類推。這是我的代碼。 ReadLine方法不會前進到下一行嗎?

Dim fso, newFile, folderPath 

folderPath = "C:\MyDataTest" 

Set fso = CreateObject("Scripting.FileSystemObject") 

For Each file In fso.GetFolder(folderPath).Files 

    Do While Not file.OpenAsTextStream.AtEndOfStream 
     strLine = file.OpenAsTextStream.ReadLine 
     strChar = file.OpenAsTextStream.Read(3) 
     Do While strChar <> "F12" 
      If strChar = "F1 " Then 
       fileName = Replace(strLine, "F1 ", "") 
       newFilePath = folderPath + "\" + fileName + ".txt" 
       Set newFile = fso.CreateTextFile(newFilePath, True) 
       newFile.WriteLine(strLine) 
       strLine = file.OpenAsTextStream.ReadLine 
       strChar = file.OpenAsTextStream.Read(3) 
      Else 
       newFile.WriteLine(strLine) 
       strLine = file.OpenAsTextStream.ReadLine 
       strChar = file.OpenAsTextStream.Read(3) 
      End If 
     Loop 
     newFile.WriteLine(strLine) 
     newFile.Close 
    loop 

Next  

這將創建文件,並把第一場F1行,然後我得到的「設置NEWFILE = fso.CreateTextFile(newFilePath,真)」行權限被拒絕運行時錯誤。我確信我錯過了一個更簡單的方法。

謝謝!

回答

1

我能弄明白。以下是任何有興趣的代碼:

Dim fso, fs, newFile, folderPath 

folderPath = "C:\MyDataTest" 

Set fso = CreateObject("Scripting.FileSystemObject") 
Set fs = CreateObject("Scripting.FileSystemObject") 


For Each file In fso.GetFolder(folderPath).Files 
    Set fileSplit = fso.OpenTextFile(file) 
    Do While Not fileSplit.AtEndOfStream 
     strLine = fileSplit.ReadLine 
     testStr = Left(strLine, 3) 
     If Left(strLine, 3) <> "F12" Then 
      If Left(strLine, 3) = "F1 " Then 
       fileName = Replace(strLine, "F1 ", "") 
       newFilePath = (folderPath + "\" + fileName + ".txt") 
       Set newFile = fs.CreateTextFile(newFilePath, True) 
       newFile.WriteLine(strLine) 
      Else 
       newFile.WriteLine(strLine) 
      End If 
     Else 
      newFile.WriteLine(strLine) 
      newFile.Close 
     End If 

    loop 
    fileSplit.Close 
Next