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

后端 未结 2 1415
鱼传尺愫
鱼传尺愫 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:11

    Use a dictionary to store the discriminating digits (keys) and the corresponding (open) files (values). Loop over the lines of the input file; for each line: cut the digit, write the line to 'the file for the digit'. Don't forget to close all files.

    In code:

      Const csDir = "..\data\splits"
      Const csInN = "splits.txt"
      Const csInF = "..\data\splits.txt" ' should be: Const csInF = goFS.BuildPath(csDir, csInN)
      Const cnPos = 47
      Dim aSplits   : aSplits       = Array("12", "456")
      Dim dicSplits : Set dicSplits = CreateObject("Scripting.Dictionary")
    
      If goFS.FolderExists(csDir) Then goFS.DeleteFolder csDir
      goFS.CreateFolder csDir
      Dim nSplit
      For nSplit = 0 To UBound(aSplits)
          Dim sDir : sDir = aSplits(nSplit)
          Dim nPos
          For nPos = 1 To Len(sDir)
              dicSplits(Mid(sDir, nPos, 1)) = nSplit
          Next
          sDir = goFS.BuildPath(csDir, sDir)
          goFS.CreateFolder sDir
          Set aSplits(nSplit) = goFS.CreateTextFile(goFS.BuildPath(sDir, csInN))
      Next
    
      Dim tsIn : Set tsIn = goFS.OpenTextFile(csInF)
      Do Until tsIn.AtEndOfStream
          Dim sLine : sLine = tsIn.ReadLine()
          Dim sKey  : sKey  = Mid(sLine, cnPos, 1)
          If dicSplits.Exists(sKey) Then
             aSplits(dicSplits(sKey)).WriteLine sLine
          End If
      Loop
      tsIn.Close
    
      For nSplit = 0 To UBound(aSplits)
          aSplits(nSplit).Close
      Next
    

提交回复
热议问题