【问题标题】:vbscript to split very large text filesvbscript 拆分非常大的文本文件
【发布时间】: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


    【解决方案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    
    

    【讨论】:

      猜你喜欢
      • 2014-10-22
      • 2017-09-18
      • 2019-10-20
      • 2020-03-22
      • 1970-01-01
      • 2012-02-09
      • 1970-01-01
      • 1970-01-01
      • 2019-04-30
      相关资源
      最近更新 更多