RE: Splitting a text file

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: J Ford (JFord_at_discussions.microsoft.com)
Date: 02/09/05


Date: Wed, 9 Feb 2005 13:41:02 -0800

Okay so I did it, any improvement/optimization suggestions would be
appreciated.

<code>
Const ForReading = 1

Dim arrFileLines()
Dim iLineVal

Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objReadFile = objFSO.OpenTextFile(wscript.Arguments(0), ForReading)

iLineVal = 1000

i = 0
 
Do Until objReadFile.AtEndOfStream
    Redim Preserve arrFileLines(i)
    arrFileLines(i) = objReadFile.ReadLine
    i = i + 1
Loop

n = 1

For l = Lbound(arrFileLines) to UBound(arrFileLines)
    
    sFileInfo = sFileInfo & arrFileLines(l) & vbcrlf
        
    If l + 1 = (n * iLineVal) And l <> 0 Then
        
        WriteIT sFileInfo, (l + 1)
        sFileInfo = ""
        n = n + 1
    
    ElseIf l = UBound(arrFileLines) Then
    
        WriteIT sFileInfo, l
        sFileInfo = ""
        n = n + 1
    
    End If
Next

wscript.Echo "Files Are Done!"

Sub WriteIT(sSomeData, sNo)

    Set objWriteFile = objFSO.CreateTextFile("FileChunk_" & sNo & ".txt",
True)

    objWriteFile.Write sSomeData

    Set objWriteFile = Nothing
    
End Sub
</code>

"J Ford" wrote:

> I would like to take a text file containing any given number of lines, read
> it, then write it out in smaller text files.
>
> Ex.
> FileA contains 10,003 lines, I want to create subsequent files with no
> greater then 1,000 lines. So File_01 thru _10 would contain 1000 lines of the
> orig and then File_11 would contain the last 3 lines.
>
> Hope that makes sense
> Thanks
> Jeremy



Relevant Pages