ILE RPG function to split a filename into name and extension

This function splits a filename (must be without path name) into the name and the extension ūüôā

If the filename has more than one dot, the name and extension is split at the last dot.

The four source members are available in an installation package you can download here. You will get a zip file containing a executable JAR file (requires Java V1.8 or newer). Unzip the JAR file and run it. You will be presented for a series of screen that will install the four members to the file QRPGLESRC in library JWTOOLS. If the library and/or source file does not exist, they will created.

Have fun ūüôā

Listing 1, member SPLITFN_PR, type RPGLE:

      *                                                                
      * SplitFN_Pr                                                     
      * ----------                                                     
      * Prototype for split a filename (no path) into name and extension.
      *                                                                
     d SplitFN         Pr                                              
     d   iFileName                  100a   Const                       
     d   oName                       50a                               
     d   oExt                        50a                               
      *

Listing 2, member SPLITFN, type RPGLE:

     h NoMain                                              
      *                                                    
      * SplitFN                                            
      * -------                                            
      * Split a filename (no path) into name and extension.
      *                                                    
      * Jesper Wachs, August 2016.
      *
      * Copymember for exported functions.                 
      * ----------------------------------                 
     d/copy qrpglesrc,splitfn_pr                           
      *                                                    
      * Exported funtions.                                 
      * ------------------                                 
      *                                                    
      * SplitFN                                            
      * -------                                            
      * Split filename into name and extension.   
      *                                           
     P SplitFN         B                   Export 
     d SplitFN         Pi                         
     d   iFileName                  100a   Const  
     d   oName                       50a          
     d   oExt                        50a          
      *                                           
     d X               s              3p 0        
     d Pos             s              3p 0        
     d Len             s              3p 0        
      *                                           
      * Find last dot.                            
      *                                           
     c                   EVal      X = *Zeros     
     c                   EVal      Pos = *Zeros   
     c                   EVal      oName = *Blanks
     c                   EVal      oExt = *Blanks                  
      *                                                            
     c                   DoU       X = *Zeros                      
      *                                                            
     c                   EVal      X = %Scan('.': iFileName: X + 1)
     c                   If        X <> *Zeros                     
     c                   EVal      Pos = X                         
     c                   EndIf                                     
      *                                                            
     c                   EndDo                                     
      *                                                            
      * If dot found, then isolate the name and the extension.     
      *                                                            
     c                   If        Pos > *Zeros                    
      *                                                            
      * Isolate name                                               
      *
     c                   If        Pos > 1                             
     c                   EVal      oName = %SubSt(iFileName: 1: Pos - 1)
     c                   EndIf                                         
      *                                                                
      * Isolate extension.                                             
      *                                                                
     c                   EVal      Len = %Len(iFileName) - Pos
     c                   If        Len > *Zeros                        
     c                   EVal      oExt = %SubSt(iFileName: Pos + 1: Len)
     c                   EndIf                                         
      *                                                                
     c                   Else                                          
      *                                                                
      * If there is no dot, we put the filename into Name.            
      *                                                                
     c                   EVal      oName = iFileName
      *
     c                   EndIf 
      *                        
     c                   Return
      *                        
     P SplitFN         E       
      *

Listing 3, member SPLITFN_EX, type RPGLE:

      *
      * SplitFN_Ex
      * ----------
      * Example program for use of SplitFN() function.
      *
      * Jesper Wachs, August 2016.
      *
      *
      * Copymember for imported functions.
      * ----------------------------------
     d/copy qrpglesrc,splitfn_pr
      *
      * Work fields.
      * ------------
     d Message         s             25
     d Reply           s             25
     d wName           s             50
     d wExt            s             50
      *
     c                   EVal      Message = 'Enter a filename:'
     c     Message       Dsply                   Reply
      *
     c                   CallP     SplitFn(Reply: wName: wExt)
      *
     c                   EVal      Message = 'Name ....: ' + wName
      *
     c     Message       Dsply
      *
     c                   EVal      Message = 'Extension: ' + wExt
      *
     c     Message       Dsply
      *
     c                   EVal      Message = 'Press ENTER'
      *
     c     Message       Dsply
      *
     c                   EVal      *InLR = *On
     c                   Return
      *

If you have installed Jesper’s ToolBox for IBM i you can use the Make member in Listing 4 to compile the module and the example program.

Listing 4, member SPLITFN_MK, type TXT:

;
; SplitFN_Mk
; ----------
; MAKE member for SplitFN() module and example program.
;
; Jesper Wachs, August 2016.
;
;
; Variables defined once and used though out make member.
;
v &lib    jwtools
v &srclib jwtools
v &debug  *source
;
; Check and possibly build LUCase module.
;
v &mod splitfn
o &srclib &mod *module
u &srclib qrpglesrc *file &mod
ci dltmod &srclib/&mod
c crtrpgmod &srclib/&mod srcfile(&srclib/qrpglesrc) dbgview(&debug)
;
; Check and possibly build LUCase_Ex module.
;
v &mod splitfn_ex
o &srclib &mod *module
u &srclib qrpglesrc *file &mod
ci dltmod &srclib/&mod
c crtrpgmod &srclib/&mod srcfile(&srclib/qrpglesrc) dbgview(&debug)
;
; Build main program.
;
v &pgm splitfn_ex
o &lib &pgm *pgm
u &srclib splitfn_ex *module
u &srclib splitfn     *module
ci DltPgm &lib/&pgm
c CrtPgm &lib/&pgm module(&srclib/&pgm &srclib/splitfn)