Split text fiel searching for specific string of text and saving in mutltiple directories

后端 未结 2 1413
鱼传尺愫
鱼传尺愫 2021-01-27 11:04

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

2条回答
  •  -上瘾入骨i
    2021-01-27 11:24

    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
    

提交回复
热议问题