【发布时间】:2012-03-01 01:48:42
【问题描述】:
我是 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 行,然后在“Set newFile = fso.CreateTextFile(newFilePath, True)”行上出现 Permission denied 运行时错误。我确定我错过了一种更简单的方法。
谢谢!
【问题讨论】:
标签: vbscript