ILE RPG procedure to split a filename into name and extension

This procedure 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.

This is version 2 of the SplitFn() procedure.

Download:

  • Source members as text files: SplitFnSourcesV2
    The zip file contains the source members. Extract the source files and transfer them to your IBM i.

The four source members, coded in Totally Free RPG, 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.

If you prefer a zip file with the five source members as text files, you can download that from here.

Have fun 🙂

Listing 1, member SPLITFN_PR, type RPGLE:

**Free
// SplitFN_Pr
// ----------
// Prototype for split a filename (no path) into name and extension.
//
// Jesper Wachs, Version 2, August 2018.
//   Converted to Totally Free RPG.
//
// Jesper Wachs, Version 1, August 2016.
//
// The source member are delivered 'as is'.
//
// Neither Jesper Wachs nor anyone else who has been involved in
// the creation, production or delivery of this product shall be
// liable for any direct, indirect, consequential or incidental
// damages (including damages for loss of business profits,
// business interruption, loss of business information, and the
// like) arising out of the use or inability to use such product
// even if Jesper Wachs has been advised of the possibility of
// such damages.
//
Dcl-PR SplitFN;
  iFileName Char(100) Const;
  oName Char(50);
  oExt Char(50);
End-PR;

Listing 2, member SPLITFN, type RPGLE:

**Free
Ctl-Opt NoMain;
//
// SplitFN
// -------
// Split a filename (no path) into name and extension.
//
// Jesper Wachs, Version 2, August 2018.
//   Converted to Totally Free RPG.
//
// Jesper Wachs, Version 1, August 2016.
//
// The source member are delivered 'as is'.
//
// Neither Jesper Wachs nor anyone else who has been involved in
// the creation, production or delivery of this product shall be
// liable for any direct, indirect, consequential or incidental
// damages (including damages for loss of business profits,
// business interruption, loss of business information, and the
// like) arising out of the use or inability to use such product
// even if Jesper Wachs has been advised of the possibility of
// such damages.
//
// Copymember for exported functions.
// ----------------------------------
/copy qrpglesrc,splitfn_pr

// Exported funtions.
// ------------------
//
// SplitFN
// -------
// Split filename into name and extension.
//
Dcl-Proc SplitFN Export;
Dcl-PI SplitFN;
  iFileName Char(100) Const;
  oName Char(50);
  oExt Char(50);
End-PI;

Dcl-S X Packed(3:0);
Dcl-S Pos Packed(3:0);
Dcl-S Len Packed(3:0);

  // Find last dot in the filename.

  X = *Zeros;
  Pos = *Zeros;
  oName = *Blanks;
  oExt = *Blanks;

  DoU X = *Zeros;

     X = %Scan('.': iFileName: X + 1);
     If X <> *Zeros;
        Pos = X;
     EndIf;

  EndDo;

  // If dot found, then isolate the name and the extension.

  If Pos > *Zeros;

     // Isolate name

     If Pos > 1;
        oName = %SubSt(iFileName: 1: Pos - 1);
     EndIf;

     // Isolate extension.

     Len = %Len(iFileName) - Pos;
     If Len > *Zeros;
        oExt = %SubSt(iFileName: Pos + 1: Len);
     EndIf;

  Else;

     // If there is not dot, we put the filename into Name.

     oName = iFileName;

  EndIf;

  Return;

End-Proc SplitFN;

Listing 3, member SPLITFN_EX, type RPGLE:

**Free
//
// SplitFN_Ex
// ----------
// Example program for use of SplitFN() function.
//
// Jesper Wachs, Version 2, August 2018.
//   Converted to Totally Free RPG.
//
// Jesper Wachs, Version 1, August 2016.
//
// The source member are delivered 'as is'.
//
// Neither Jesper Wachs nor anyone else who has been involved in
// the creation, production or delivery of this product shall be
// liable for any direct, indirect, consequential or incidental
// damages (including damages for loss of business profits,
// business interruption, loss of business information, and the
// like) arising out of the use or inability to use such product
// even if Jesper Wachs has been advised of the possibility of
// such damages.
//
// Copymember for imported functions.
// ----------------------------------
/copy qrpglesrc,splitfn_pr

// Work fields.
// ------------
Dcl-S Message Char(25);
Dcl-S Reply Char(25);
Dcl-S wName Char(50);
Dcl-S wExt Char(50);

  Message = 'Enter a filename:';
  Dsply Message '' Reply;

  CallP SplitFn(Reply: wName: wExt);

  Message = 'Name ....: ' + wName;

  Dsply Message;

  Message = 'Extension: ' + wExt;

  Dsply Message;

  Message = 'Press ENTER';

  Dsply Message;

  *InLR = *On;
  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)