Vbscript 按文件大小剥离文本文件而不丢失最后一行的内容

问题描述 投票:0回答:1

我有以下 Vbscript 代码,当前通过 MB 中的文件大小参数将大文本文件拆分为较小的文件:

Sub Split
  Dim iFile, oFile, iStream, Data
  Dim Ext, e, offset, length, NewName
  
  Info (" ==> Splitting " & sFile & " by " & Size & "B files and saving to " & dPath)
  
  if not oFSO.FileExists(sFile) then Error _
  ("Cannot locate the file """ & sFile & """")
  Set iFile = oFSO.GetFile(sFile)
  
  On Error Resume Next
  Set iStream = iFile.OpenAsTextStream(1)

  if err.number > 0 then Error("Cannot open the file """ & sFile _
  & """" & LF & Err.Description)

  On Error Goto 0
  Data = iStream.Read(iFile.Size)
  iStream.close
  
  Ext = 0
  offset = 1
  
  Do
    Ext = Right("00" & Ext + 1, 3)
    if ext > "999" then Error ("Too many files - maximum is 999!")
    
    NewName = dPath & prefix & datetype & suffix & "_" & Ext & ".txt"
    Info "Writing """ & NewName & """"

    On Error Resume Next
  
    ' ---> Write file
    Set oFile = oFSO.CreateTextFile(NewName, 2)

    if err.number > 0 then Error("Cannot open the file """ _
    & NewName & """" & LF & Err.Description)
    On Error Goto 0
    
    length = Size
    If length > Len(data)+1 - offset Then length = Len(data) + 1 - offset
    
    oFile.Write Mid(Data, offset, length)
    offset = offset + length
    oFile.Close
  Loop Until offset >= Len(data)

End Sub

上面的代码可以工作,但是,当大文件被分割成更小的文件时,它也会分割每个文件最后一行的内容,但我想保留该行上的所有内容并在行尾分割根据我当前的文件大小功能。

例如行尾(较小文件中的最后一行)如下:

这是一个测试

最后一行存储了每个被剪切的文件,如下所示:

这是一个

是否可以保留行中的所有内容并在行尾以及当前文件大小进行分割,如果可以,我该如何实现这一点?谢谢

vbscript
1个回答
0
投票

要按大小分割文件,但也只能在行尾分割,可以通过将每个块的结束点移动到下一个 CRLF 来实现。这是带有该增强功能的 Split Sub。

Sub Split
  Dim iFile, oFile, iStream, Data
  Dim Ext, e, offset, length, NewName
  
  Info (" ==> Splitting " & sFile & " by " & Size & "B files and saving to " & dPath)
  
  if not oFSO.FileExists(sFile) then Error _
  ("Cannot locate the file """ & sFile & """")
  Set iFile = oFSO.GetFile(sFile)
  
  On Error Resume Next
  Set iStream = iFile.OpenAsTextStream(1)

  if err.number > 0 then Error("Cannot open the file """ & sFile _
  & """" & LF & Err.Description)

  On Error Goto 0
  Data = iStream.Read(iFile.Size)
  iStream.close
  TotalSize = Len(Data)
  
  Ext = 0
  offset = 1
  
  Do
    Ext = Right("00" & Ext + 1, 3)
    if ext > "999" then Error ("Too many files - maximum is 999!")
    
    NewName = dPath & prefix & datetype & suffix & "_" & Ext & ".txt"
    Info "Writing """ & NewName & """"

    On Error Resume Next
  
    ' ---> Write file
    Set oFile = oFSO.CreateTextFile(NewName, 2)

    if err.number > 0 then Error("Cannot open the file """ _
    & NewName & """" & LF & Err.Description)
    On Error Goto 0
    
    length = Size
    If length > TotalSize+1 - offset Then length = TotalSize + 1 - offset

    'Shift the end point to the next CRLF
    Do While Mid(Data, offset+length, 2)<>VBCRLF And offset+length<TotalSize
      length = length + 1
    Loop
    If Mid(Data, offset+length, 2)=VBCRLF Then length = length + 2
    
    oFile.Write Mid(Data, offset, length)
    offset = offset + length
    oFile.Close
  Loop Until offset >= TotalSize

End Sub
© www.soinside.com 2019 - 2024. All rights reserved.