Problem solve Get help with specific problems with your technologies, process and projects.

Generic routine to split up a file

Here's a basic code to split up a file for downloading and transmission.

We recently needed to split a large file into six pieces for downloading & transmission. After a bit of experimentation, I came up with this program that can split any file into any number of segments. Call FILESPLIT with the name of the file (10c), its library (10c), and the number of splits (2c).

The splits will be named with the first eight characters of the file, followed by a two digit number (00-99). The result files are placed in the same library as the original.

If the last split file is small (less that 1/2 the size of the others), its records are added to the last split instead.

** Since the program works by copying from record-number to record-number, the original file is reorganized to remove deleted records.

   /*  Split the library/file into #splits files, named      */       
 /*  filenameXX, where xx will be from 01 to 99. The result*/       
 /*  files will be in the same library as the original file*/       
 /*  The original file is reorganized to compress out      */       
 /*  deleted records.                                      */       
 /*  Split files have same text, with ", part #xx" appended*/       
 /*  filename must be 8 characters or less (due to suffix) */       
 /*  Assumes *first member                                 */       
pgm parm(&filename  &library &c#splits)                             
             dcl &filename *char 10                                 
             dcl &library  *char 10                                 
             dcl &c#splits *char 2     /* desired # result files */ 
             dcl &#splits *dec (10 0)  /* desired # result files */ 
             dcl &records *dec (10 0)                               
             dcl &low     *dec (10 0)                               
             dcl &high    *dec (10 0)                               
             dcl &splitsize *dec (10 0)                             
             dcl &i         *dec (2 0)                              
             dcl &c         *char (2)                               
             dcl &file      *char (10)                              
             dcl &desc      *char (50)                              
             chgvar &#splits (&c#splits)                            
/* split into multiple files */                                     
             rgzpfm &library/&filename                              
             RTVMBRD FILE(&library/&filename) nbRCURRCD(&RECORDS) - 
             chgvar  &desc (%sst(&desc 1 40))                       
             chgvar &splitsize (&records / &#splits)               
             chgvar &low (1)                                       
             chgvar &high (&splitsize)                             
             chgvar &i (0)                                         
loop:        if (&low *lt &records) do                             
             chgvar &i (&i + 1)                                    
             chgvar &c (&i)                                        
             chgvar &file (&filename *tcat &c)                     
             dltf &library/&file                                   
             monmsg cpf2105                                        
             CPYF       FROMFILE(&library/&filename) +             
                          TOFILE(&library/&FILE) MBROPT(*ADD) +    
                          CRTFILE(*YES) FROMRCD(&LOW) TORCD(&HIGH) 
             CHGOBJD    OBJ(&library/&FILE) OBJTYPE(*FILE) TEXT( + 
                          &desc *tcat ', part #' *tcat &c)         
             chgvar &low (&high + 1)                               
             /* if last split is small, roll into previous & leave */ 
             chgvar &high (&records - &low)                           
             if ((&low < &records) & (&high < (&splitsize / 2))) do   
             CPYF       FROMFILE(&library/&filename) +                
                          TOFILE(&library/&FILE) MBROPT(*ADD) +       
                          FROMRCD(&LOW) TORCD(&records)               
             chgvar &low (&records + 1)                               
             chgvar &high (&low + &splitsize)                         
             goto loop                                                


The Best Web Links: tips, tutorials and more.

Ask your programming questions--or help out your peers by answering them--in our live discussion forums.

Ask the Experts yourself: Our application development gurus are waiting to answer your programming questions.

Dig Deeper on iSeries CL programming