change dir for output file

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

change dir for output file

Post by sal21 »

this is a very old code to split big file into smaller files.
but instead to save into the same file the splited small file how to save into a sub dir from source?

i need to store the little files in: "C:\Lavori_Vb6\STRADARIO\FILE\SPLITED_FILE\....

Code: Select all

Option Explicit
Private Const INPUT_TEXT_FILE = "C:\Lavori_Vb6\STRADARIO\FILE\it_countrywide-addresses-country.TXT"  'The full path to the big file
Private Const REPEAT_HEADER_ROW = True                'Set to True to duplicate the header row in each part file
Private Const LINES_PER_PART = 30000                 'The number of lines per part file
Public Sub SPLITFILE()

    Dim oFileSystem, oInputFile, oOutputFile, iOutputFile, iLineCounter, sHeaderLine, SLINE, sFileExt, sStart

    sStart = Now()

    sFileExt = Right(INPUT_TEXT_FILE, Len(INPUT_TEXT_FILE) - InStrRev(INPUT_TEXT_FILE, ".") + 1)

    'Debug.Print sFileExt

    iLineCounter = 0
    iOutputFile = 1

    Set oFileSystem = CreateObject("Scripting.FileSystemObject")
    Set oInputFile = oFileSystem.OpenTextFile(INPUT_TEXT_FILE, 1, False)
    Set oOutputFile = oFileSystem.OpenTextFile(Replace(INPUT_TEXT_FILE, sFileExt, "_" & iOutputFile & sFileExt), 2, True)

    'Debug.Print INPUT_TEXT_FILE

    If REPEAT_HEADER_ROW Then
        iLineCounter = 1
        sHeaderLine = oInputFile.ReadLine()
        Call oOutputFile.WriteLine(sHeaderLine)
    End If

    Do While Not oInputFile.AtEndOfStream
        SLINE = oInputFile.ReadLine()
        Call oOutputFile.WriteLine(SLINE)
        iLineCounter = iLineCounter + 1
        If iLineCounter Mod LINES_PER_PART = 0 Then
            iOutputFile = iOutputFile + 1
            Call oOutputFile.Close
            Set oOutputFile = oFileSystem.OpenTextFile(Replace(INPUT_TEXT_FILE, sFileExt, "_" & iOutputFile & sFileExt), 2, True)
            If REPEAT_HEADER_ROW Then
                Call oOutputFile.WriteLine(sHeaderLine)
            End If
        End If
    Loop

    Call oInputFile.Close
    Call oOutputFile.Close

    Set oFileSystem = Nothing

    Call MsgBox("Done" & vbCrLf & "Lines Processed:" & iLineCounter & vbCrLf & "Part Files: " & iOutputFile & vbCrLf & "Start Time: " & sStart & vbCrLf & "Finish Time: " & Now())

End Sub


User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: change dir for output file

Post by HansV »

Code: Select all

Option Explicit

Private Const INPUT_TEXT_PATH = "C:\Lavori_Vb6\STRADARIO\FILE\"  'The path of the big file
Private Const INPUT_TEXT_FILE = "it_countrywide-addresses-country.TXT"  'The name of the big file
Private Const OUTPUT_TEXT_PATH = "C:\Lavori_Vb6\STRADARIO\FILE\SPLITED_FILE\"  'The path of the small files
Private Const REPEAT_HEADER_ROW = True                'Set to True to duplicate the header row in each part file
Private Const LINES_PER_PART = 30000                 'The number of lines per part file

Public Sub SPLITFILE()

    Dim oFileSystem, oInputFile, oOutputFile, iOutputFile, iLineCounter, sHeaderLine, SLINE, sFileExt, sStart
    Dim OUTPUT_TEXT_FILE As String

    sStart = Now()

    sFileExt = Right(INPUT_TEXT_FILE, Len(INPUT_TEXT_FILE) - InStrRev(INPUT_TEXT_FILE, ".") + 1)

    'Debug.Print sFileExt

    iLineCounter = 0
    iOutputFile = 1

    Set oFileSystem = CreateObject("Scripting.FileSystemObject")
    Set oInputFile = oFileSystem.OpenTextFile(INPUT_TEXT_PATH & INPUT_TEXT_FILE, 1, False)
    OUTPUT_TEXT_FILE = Replace(INPUT_TEXT_FILE, sFileExt, "_" & iOutputFile & sFileExt)
    Set oOutputFile = oFileSystem.OpenTextFile(OUTPUT_TEXT_PATH & OUTPUT_TEXT_FILE, 2, True)

    'Debug.Print INPUT_TEXT_FILE

    If REPEAT_HEADER_ROW Then
        iLineCounter = 1
        sHeaderLine = oInputFile.ReadLine()
        Call oOutputFile.WriteLine(sHeaderLine)
    End If

    Do While Not oInputFile.AtEndOfStream
        SLINE = oInputFile.ReadLine()
        Call oOutputFile.WriteLine(SLINE)
        iLineCounter = iLineCounter + 1
        If iLineCounter Mod LINES_PER_PART = 0 Then
            iOutputFile = iOutputFile + 1
            Call oOutputFile.Close
            OUTPUT_TEXT_FILE = Replace(INPUT_TEXT_FILE, sFileExt, "_" & iOutputFile & sFileExt)
            Set oOutputFile = oFileSystem.OpenTextFile(OUTPUT_TEXT_PATH & OUTPUT_TEXT_FILE, 2, True)
            If REPEAT_HEADER_ROW Then
                Call oOutputFile.WriteLine(sHeaderLine)
            End If
        End If
    Loop

    Call oInputFile.Close
    Call oOutputFile.Close

    Set oFileSystem = Nothing

    Call MsgBox("Done" & vbCrLf & "Lines Processed:" & iLineCounter & vbCrLf & "Part Files: " & iOutputFile & vbCrLf & "Start Time: " & sStart & vbCrLf & "Finish Time: " & Now())

End Sub
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: change dir for output file

Post by sal21 »

HansV wrote:
05 Dec 2021, 11:01

Code: Select all

Option Explicit

Private Const INPUT_TEXT_PATH = "C:\Lavori_Vb6\STRADARIO\FILE\"  'The path of the big file
Private Const INPUT_TEXT_FILE = "it_countrywide-addresses-country.TXT"  'The name of the big file
Private Const OUTPUT_TEXT_PATH = "C:\Lavori_Vb6\STRADARIO\FILE\SPLITED_FILE\"  'The path of the small files
Private Const REPEAT_HEADER_ROW = True                'Set to True to duplicate the header row in each part file
Private Const LINES_PER_PART = 30000                 'The number of lines per part file

Public Sub SPLITFILE()

    Dim oFileSystem, oInputFile, oOutputFile, iOutputFile, iLineCounter, sHeaderLine, SLINE, sFileExt, sStart
    Dim OUTPUT_TEXT_FILE As String

    sStart = Now()

    sFileExt = Right(INPUT_TEXT_FILE, Len(INPUT_TEXT_FILE) - InStrRev(INPUT_TEXT_FILE, ".") + 1)

    'Debug.Print sFileExt

    iLineCounter = 0
    iOutputFile = 1

    Set oFileSystem = CreateObject("Scripting.FileSystemObject")
    Set oInputFile = oFileSystem.OpenTextFile(INPUT_TEXT_PATH & INPUT_TEXT_FILE, 1, False)
    OUTPUT_TEXT_FILE = Replace(INPUT_TEXT_FILE, sFileExt, "_" & iOutputFile & sFileExt)
    Set oOutputFile = oFileSystem.OpenTextFile(OUTPUT_TEXT_PATH & OUTPUT_TEXT_FILE, 2, True)

    'Debug.Print INPUT_TEXT_FILE

    If REPEAT_HEADER_ROW Then
        iLineCounter = 1
        sHeaderLine = oInputFile.ReadLine()
        Call oOutputFile.WriteLine(sHeaderLine)
    End If

    Do While Not oInputFile.AtEndOfStream
        SLINE = oInputFile.ReadLine()
        Call oOutputFile.WriteLine(SLINE)
        iLineCounter = iLineCounter + 1
        If iLineCounter Mod LINES_PER_PART = 0 Then
            iOutputFile = iOutputFile + 1
            Call oOutputFile.Close
            OUTPUT_TEXT_FILE = Replace(INPUT_TEXT_FILE, sFileExt, "_" & iOutputFile & sFileExt)
            Set oOutputFile = oFileSystem.OpenTextFile(OUTPUT_TEXT_PATH & OUTPUT_TEXT_FILE, 2, True)
            If REPEAT_HEADER_ROW Then
                Call oOutputFile.WriteLine(sHeaderLine)
            End If
        End If
    Loop

    Call oInputFile.Close
    Call oOutputFile.Close

    Set oFileSystem = Nothing

    Call MsgBox("Done" & vbCrLf & "Lines Processed:" & iLineCounter & vbCrLf & "Part Files: " & iOutputFile & vbCrLf & "Start Time: " & sStart & vbCrLf & "Finish Time: " & Now())

End Sub
TKS! bro.

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: change dir for output file

Post by sal21 »

sal21 wrote:
05 Dec 2021, 11:28
HansV wrote:
05 Dec 2021, 11:01

Code: Select all

Option Explicit

Private Const INPUT_TEXT_PATH = "C:\Lavori_Vb6\STRADARIO\FILE\"  'The path of the big file
Private Const INPUT_TEXT_FILE = "it_countrywide-addresses-country.TXT"  'The name of the big file
Private Const OUTPUT_TEXT_PATH = "C:\Lavori_Vb6\STRADARIO\FILE\SPLITED_FILE\"  'The path of the small files
Private Const REPEAT_HEADER_ROW = True                'Set to True to duplicate the header row in each part file
Private Const LINES_PER_PART = 30000                 'The number of lines per part file

Public Sub SPLITFILE()

    Dim oFileSystem, oInputFile, oOutputFile, iOutputFile, iLineCounter, sHeaderLine, SLINE, sFileExt, sStart
    Dim OUTPUT_TEXT_FILE As String

    sStart = Now()

    sFileExt = Right(INPUT_TEXT_FILE, Len(INPUT_TEXT_FILE) - InStrRev(INPUT_TEXT_FILE, ".") + 1)

    'Debug.Print sFileExt

    iLineCounter = 0
    iOutputFile = 1

    Set oFileSystem = CreateObject("Scripting.FileSystemObject")
    Set oInputFile = oFileSystem.OpenTextFile(INPUT_TEXT_PATH & INPUT_TEXT_FILE, 1, False)
    OUTPUT_TEXT_FILE = Replace(INPUT_TEXT_FILE, sFileExt, "_" & iOutputFile & sFileExt)
    Set oOutputFile = oFileSystem.OpenTextFile(OUTPUT_TEXT_PATH & OUTPUT_TEXT_FILE, 2, True)

    'Debug.Print INPUT_TEXT_FILE

    If REPEAT_HEADER_ROW Then
        iLineCounter = 1
        sHeaderLine = oInputFile.ReadLine()
        Call oOutputFile.WriteLine(sHeaderLine)
    End If

    Do While Not oInputFile.AtEndOfStream
        SLINE = oInputFile.ReadLine()
        Call oOutputFile.WriteLine(SLINE)
        iLineCounter = iLineCounter + 1
        If iLineCounter Mod LINES_PER_PART = 0 Then
            iOutputFile = iOutputFile + 1
            Call oOutputFile.Close
            OUTPUT_TEXT_FILE = Replace(INPUT_TEXT_FILE, sFileExt, "_" & iOutputFile & sFileExt)
            Set oOutputFile = oFileSystem.OpenTextFile(OUTPUT_TEXT_PATH & OUTPUT_TEXT_FILE, 2, True)
            If REPEAT_HEADER_ROW Then
                Call oOutputFile.WriteLine(sHeaderLine)
            End If
        End If
    Loop

    Call oInputFile.Close
    Call oOutputFile.Close

    Set oFileSystem = Nothing

    Call MsgBox("Done" & vbCrLf & "Lines Processed:" & iLineCounter & vbCrLf & "Part Files: " & iOutputFile & vbCrLf & "Start Time: " & sStart & vbCrLf & "Finish Time: " & Now())

End Sub
TKS! bro.
HUMMMM....
error in
Set oOutputFile = oFileSystem.OpenTextFile(OUTPUT_TEXT_PATH & OUTPUT_TEXT_FILE, 2, True)
possible?

see image
You do not have the required permissions to view the files attached to this post.

User avatar
HansV
Administrator
Posts: 78235
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: change dir for output file

Post by HansV »

Make sure that OUTPUT_TEXT_PATH has the correct value. The folder must already exist.
Best wishes,
Hans

User avatar
sal21
PlatinumLounger
Posts: 4334
Joined: 26 Apr 2010, 17:36

Re: change dir for output file

Post by sal21 »

HansV wrote:
05 Dec 2021, 11:38
Make sure that OUTPUT_TEXT_PATH has the correct value. The folder must already exist.
INFACT!!!!

i have used...FILE instead FILES
Lost one "S"

sorry me.