Split 1 CSV file into multiple CSV files
SolvedHello,
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
-
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-
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
-
-
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.
-
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.
-
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
-
Hello,
Well, I won’t find anything better, I’m leaving the position.
Regards.
The Penguin -
Hello,
Thank you jee pee for this solution (*.bat) that works well!
And thanks to Le Pingou for your participation!
Have a great day!