I have a text file that I need to split according to values in the 4th column of information. The script would need to split the lines of text according the the value of the fir
I'd suggest to do the discrimination with a Select
statement, because I consider that a lot easier to understand. A dictionary I'd use to manage the output files.
Const ForReading = 1
Const ForWriting = 2
Const keyPos = 47
Const inputFileName = "input.txt"
Const outputFileName = "input.txt"
outputFolders = Array("foo", "bar")
Sub WriteOutput(data, fldr)
If Not fso.FolderExists(fldr) Then fso.CreateFolder(fldr)
If Not outputFiles.Exists(fldr) Then outputFiles.Add fldr, fso.OpenTextFile(fso.BuildPath(fldr, outputFileName), ForWriting, True)
outputFiles(fldr).WriteLine data
End Sub
Set fso = CreateObject("Scripting.FileSystemObject")
Set outputFiles = CreateObject("Scripting.Dictionary")
Set inputFile = fso.OpenTextFile(inputFileName, ForReading, True)
Do Until inputFile.AtEndOfStream
line = inputFile.ReadLine
Select Case Mid(line, keyPos, 1)
Case 1, 2:
WriteOutput line, outputFolders(0)
Case 4, 5, 6:
WriteOutput line, outputFolders(1)
End Select
Loop
inputFile.Close
For Each f In outputFiles.Items
f.Close
Next