title: vbscript to split very large text files
body: <p>I'm new to vbscript and I'm having trouble with something that should be relatively easy.  I have two very large text files that I have to split into many different text files based on certain lines in the files.  Here is a sample of the text file:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>The F1 line will provide the name of the new text file while the F12 line will be the last line in that new text file.  The next F1 will be the next file and so on.  Here is my code.  Does the ReadLine method not advance to the next line?  </p>
  
  <pre><code>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 &lt;&gt; "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    
  </code></pre>
  
  <p>This creates the file and writes the first F1 line and then I get a Permission denied runtime error on the "Set newFile = fso.CreateTextFile(newFilePath, True)" line.  I'm sure that I'm missing a much easier way of doing this.</p>
  
  <p>Thank you!</p>