Split 1 CSV file into multiple CSV files

Solved

Hello,

I want to split one file with several lines (30,000 for example) into several files of about 300 lines each.

(file in csv format)

In VBA, if possible: (an example of what I want)

Please select the file to split.

How many lines per file would you like?

Once the answers to the 2 questions are provided, the split should be done while keeping the format in each column (example column A = Text format).....

The first line (header) will be the same for all files.

Thank you in advance for your help! I haven't found a solution online.....

Best regards


6 answers

  1. Hello,

    So each cut is saved as a new CSV file.... Yes / No?

    Please provide at least 5 to 10 lines of the file to be split!

    To attach the file via https://www.cjoint.com/

     follow the instructions and paste the link in a new post.


    Best regards.
    The Penguin

    1. Hello Le Pingou,

      Yes, for each cut a different name is required

      For example, the file to be split is named MTT.csv

      The result will be:

      MTT01.CSV

      MTT02.CSV

      MTT03.CSV .......etc

      https://www.cjoint.com/c/LIfo2GzKsdV

      Best regards

  2. Moderator

    Hello,

    7zip allows you to split a file, in 7zip, on the file list, right-click split by, according to the size, I don't know if it respects line breaks.

    Or a small batch command line: https://www.developpez.net/forums/d1502877/general-developpement/programmation-systeme/windows/scripts-batch/diviser-fichier-texte-sous-windows/#post8153273
    easily adaptable to create a first line with column headers.


    1. Hello jee pee,

      Right-click on 7zip, I couldn't find the divide into ...

      For the 2nd solution, I created a .bat without success...

      Thank you anyway!

      Best regards

  3. Moderator

    The script works, it just had a line counter error in the 1 file

    @echo off setLocal EnableDelayedExpansion set limit=10000 set file=splitA.txt set lineCounter=1 set filenameCounter=1 set name= set extension= for %%a in (%file%) do ( set "name=%%~na" set "extension=%%~xa" ) set splitFile=!name!!filenameCounter!!extension! for /f "tokens=*" %%a in (%file%) do ( if !lineCounter! geq !limit! ( set /a filenameCounter=!filenameCounter! + 1 set splitFile=!name!!filenameCounter!!extension! echo New file !splitFile! set lineCounter=0 ) echo %%a>> !splitFile! set /a lineCounter=!lineCounter! + 1 ) pause

    Otherwise, you would need to give us a sample file.

    For 7zip, you need to launch 7zip first, then in 7zip, browse the files + right-click.


    1. Moderator

      Here is by duplicating the first record containing the column names on each file

      @echo off setLocal EnableDelayedExpansion set limit=2 set file=splitcsv.csv set lineCounter=0 set filenameCounter=1 set name= set extension= for %%a in (%file%) do ( set "name=%%~na" set "extension=%%~xa" ) set col= set splitFile=!name!!filenameCounter!!extension! for /f "tokens=*" %%a in (%file%) do ( if "!col!" == "" ( set col=%%a) if !lineCounter! gtr !limit! ( set /a filenameCounter=!filenameCounter! + 1 set splitFile=!name!!filenameCounter!!extension! echo !col! > !splitFile! echo New file !splitFile! set lineCounter=1 ) echo %%a>> !splitFile! set /a lineCounter=!lineCounter! + 1 ) pause

      1. Hello,

        Well, I won’t find anything better, I’m leaving the position.


        Regards.
        The Penguin

        1. Hello,

          Thank you jee pee for this solution (*.bat) that works well!

          And thanks to Le Pingou for your participation!

          Have a great day!