vb6text-filesunicode-string

vb6 : split unicode text file to smaller files of maximum specified size


I have a big file of text, lines by lines ended with a chr(13) in Windows. I want to store the text in the access mdb file memo field which has a max size limit of 64K only.

The text file contain unicode characters.

How can I use vb6 the line Input method to split the text into multiple files? or is there better alternative to do this instead of writing a program?

Example of text:

1 00:00:01,960 --> 00:00:04,880 哦哦,向前一迎就上

2 00:00:04,880 --> 00:00:07,600 去。我都跟你讲,拉球摩擦是需要

3 00:00:07,600 --> 00:00:10,480 顶,没有顶它,它不是一个

4 00:00:10,480 --> 00:00:13,280 单纯的往上的动作,它需要一边

5 00:00:13,280 --> 00:00:16,240 往前顶,找到球的时候给一个小的动

Public Sub splitFile()

  Dim i, t1, t2, c1, c2, k As Long
  Dim TextLine, OutLine
  Dim thisfile
  Dim partCount

  thisfile = "speechByParts.txt"
  
  partCount = 0
  OutLine = ""
  Open thisfile For Input As #1   ' Open file.
  Do While Not EOF(1)   ' Loop until end of file.
    Line Input #1, TextLine   ' Read line into variable.
    
    Debug.Print TextLine
    OutLine = OutLine & TextLine & Chr(13)

    If Len(OutLine) > 60000 Then
      partCount = partCount + 1
      WriteFile "speechByParts" & partCount & ".txt", OutLine
      OutLine = ""

    End If

  Loop
  
  If OutLine > "" Then
    partCount = partCount + 1
    WriteFile "speechByParts" & partCount & ".txt", OutLine
  End If

  Close #1   ' Close file

End Sub

Solution

  • There are 2 common ways of reading and writing text files containing Unicode:

    Here is an example using ADO. For simplicity, the code writes each line in the input file to a new file. You will need to set a reference to Microsoft ActiveX Data Objects 2.8 Library.

    Public Sub splitFile()
       Dim inputStream As ADODB.Stream
       Dim outputStream As ADODB.Stream
       Dim data As String
       Dim i As Integer
       
       Set inputStream = New ADODB.Stream
       inputStream.Open
       inputStream.LoadFromFile "C:\TEMP\speechByParts.txt"
       
       Do Until inputStream.EOS
          data = inputStream.ReadText(adReadLine)
          Debug.Print data
          
          i = i + 1
          Set outputStream = New ADODB.Stream
          outputStream.Open
          outputStream.WriteText data, adWriteLine
          outputStream.SaveToFile "C:\TEMP\speechByParts" & i & ".txt", adSaveCreateOverWrite
          outputStream.Close
       Loop
    
       inputStream.Close
    End Sub