我是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运行时错误。我确信我错过了一种更简单的方法。
谢谢!
答案 0 :(得分: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