Batch script to split a file into 4 files

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ramesh
Posts: 2
Joined: 15 Jun 2009 06:20

Batch script to split a file into 4 files

#1 Post by ramesh » 15 Jun 2009 07:23

hi,

i need a batch script to spilt a single file into 4 files.
I would really appreciate if anyone can anyone help me with that?

Thanks,
Ramesh

RElliott63
Expert
Posts: 80
Joined: 04 Feb 2009 10:03

#2 Post by RElliott63 » 15 Jun 2009 08:10

Ramesh,

You have to have some sort of criteria on breaking the file down. Number of records? Key field information? They way the sun sets over Bangor? Some sort of requirement. We can give you numerous ways to split a file.

ramesh
Posts: 2
Joined: 15 Jun 2009 06:20

#3 Post by ramesh » 16 Jun 2009 00:45

RElliott63 wrote:Ramesh,

You have to have some sort of criteria on breaking the file down. Number of records? Key field information? They way the sun sets over Bangor? Some sort of requirement. We can give you numerous ways to split a file.



We will be getting a vendor file daily and number of records will varyon daily basis. I wold like to split the file on record count.
Plese let me kow if you need anymore info

RElliott63
Expert
Posts: 80
Joined: 04 Feb 2009 10:03

#4 Post by RElliott63 » 16 Jun 2009 10:47

Here's a Start for you:

Change "xxLimit" to how many records you want per file. %1 is the name of the file being sent from vendor.

Code: Select all

@Echo off

SETLOCAL ENABLEEXTENSIONS
SETLOCAL ENABLEDELAYEDEXPANSION
 
Set /a "xxFileNo=1"
Set /a "xxLimit=10"
Set /a "xxRecCount=0"
Set "xxOutFile=OutFile.%xxFileNo%"
Set "xxInFile=%1"

for /f "delims=" %%L in (%xxInFile%) Do (

   Echo %%L >> !xxOutFile!
   Set /a xxRecCount+=1

   If /I !xxRecCount! GEQ %xxLimit% (
    Set /a xxFileNo+=1
    Set "xxOutFile=OutFile.!xxFileNo!"
    Set /a xxRecCount=0
   )

)

ghostmachine4
Posts: 319
Joined: 12 May 2006 01:13

#5 Post by ghostmachine4 » 18 Jun 2009 22:15

if you can get GNU tools, you can use the split command.
eg assuming split by number of lines

Code: Select all

c:\workspace\> split -l 100 myfile

Post Reply