Batch splitting a .txt file

Mael730 Posted messages 50 Status Member -  
cs-bilou Posted messages 836 Status Member -
Hello,

I'm new to batch and I'm stuck on a problem: I have a .txt file containing an arbitrary number of lines (for example 135000) and I would like to split this file into x files with the same name +/- a number (to differentiate them) each containing 40,000 lines.
Is this doable in batch??

Thanks in advance

Best regards
Configuration: Windows XP Firefox 3.5.7

3 answers

cs-bilou Posted messages 836 Status Member 164
 
Here you go, you can do something like this:
@echo off setlocal enableDelayedExpansion set nbrLigneMax=4 Set nbrLigne=0 set nbrFile=0 for /f "delims=""" %%i in ('type "In.txt"') do ( if !nbrLigne!==%nbrLigneMax% set /a nbrFile=!nbrFile!+1& set nbrLigne=0 echo %%i>>File-!nbrFile!.txt set /a nbrLigne=!nbrLigne!+1 ) pause


The file In.txt is the input file, the one you want to split.
The variable nbrLigneMax is the number of lines you want per file.

Bilou
--
Some days you just shouldn't mess with me.
And some days it's every day!
3
cs-bilou Posted messages 836 Status Member 164
 
Here you go:
@echo off setlocal enableDelayedExpansion set nbrLigneEnteteMax=14 set nbrLigneEntete=0 for /f "delims=" %%i in ('type "In.txt"') do ( if !nbrLigneEntete! LSS %nbrLigneEnteteMax% echo %%i>>"EnTete.txt" set /a nbrLigneEntete+=1 ) set nbrLigneMax=4 Set nbrLigne=4 set nbrFile=0 for /f "delims=" %%i in ('type "In.txt"') do ( if !nbrLigne!==%nbrLigneMax% ( set /a nbrFile+=1 set nbrLigne=0 for /f "delims=" %%j in ('type "EnTete.txt"') do echo %%j>>"File-!nbrFile!.txt") echo %%i>>"File-!nbrFile!.txt" set /a nbrLigne+=1 ) del /f /s /q "EnTete.txt" > NUL pause


Bilou.
--
There are days you shouldn't mess with me.
And there are days every day!
1
Mael730 Posted messages 50 Status Member 1
 
Thank you, it works perfectly!
I have another question:
I need the batch to copy the header contained in the file In.txt into all the generated File-* files.

P.S: This header is 14 lines long.

Thanks in advance.
0