Skip Navigation Links.
Create a unique temporary file name.
Language(s):I-Series - CLP
Category(s):Utilities

Demonstrates: 1) How to tell if a file exists. 2) How get the length of a string. 3) How to get the number of digits in a number. 4) Concatonating strings. 5) Converting a number to a string.

/* +
   *************************************************************** +
   ** - Source Member: TMPFILEC                                    +
   *************************************************************** +
   ** - CL Report Example: Create Unique File Name                 +
   ** - Jon Vote 09/2002                                           +
   ** - www.idioma-software.com                                    +
   *************************************************************** +
   **                                                              +
   ** - Returns a unique file name in the form: pppzzz##           +
   **     where ppp is the prefix, zzz is leading zeros and        +
   **     ## is a number that makes the file unique.               +
   **                                                              +
   ** - Note!! It is up to the calling program to delete the       +
   ** - file when done, or to change it's attributes.              +
   **                                                              +
   ** - This program demonstrates:                                 +
   ** - 1) How to tell if a file exists.                           +
   ** - 2) How get the length of a string.                         +
   ** - 3) How to get the number of digits in a number.            +
   ** - 4) Concatonating strings.                                  +
   ** - 5) Converting a number to a string.                        +
   **                                                              +
   ** - This program accepts a Library, Prefix and record length   +
   ** - A unique filename is found and a file with this name       +
   ** - is created. The file name is returned to the caller        +
   ** - which can then either use the file is is or delete and     +
   ** - re-create it with the desired attributes.                  +
   **                                                              +
   ** - This is handy when you need to use a temp file             +
   ** - with a unique file name. This file name could be used      +
   ** - as is, deleted and recreated with different attributes by  +
   ** - the calling program, or used as a dummy place holder for   +
   ** - other objects that will use the same unique name - like a  +
   ** - job queue or data queue.                                   +
   **                                                              +
   ** - Parameters:                                                +
   **                                                              +
   ** -   lib - Input to this routine: Library for created file    +
   **     pfx - Input to this routine: up to 4 character prefix.   +
   **           may be left blank.                                 +
   **     recl - Input to this routine: Record length of           +
   **            created file.                                     +
   **     file - Output by this routine: Created File name.        +
   **                                                              +
*/
  pgm parm(&Lib &Pfx &Recl &File)

/* +
 ** - &FnmLlen is the number of characters in the returned file name +
 ** - Change this to as needed +                                   +
*/
  dcl &FnmLen *dec (1 0) value(8)
  dcl &Lib *char 10
  dcl &Pfx *char 4
  dcl &RecL *dec (5 0)
  dcl &File *char 10
  dcl &PfxLen *dec (1 0)
  dcl &NumDig *dec (1 0)
  dcl &StartAt *dec (1 0)
  dcl &FileNum *dec (9 0)
  dcl &cFileNum *char 9

/* +
 ** - If the prefix is blank, we'll use our own.             +
*/
  if (&Pfx *eq ' ') then(chgvar &Pfx 'TMP')

/* +
 ** - Find the length of the prefix and see how many digits  +
 ** - are available for the created file name.               +
*/

  chgvar &pfxlen 4
Loop:
  if (%sst(&Pfx &PfxLen 1) *ne ' ') then(goto EndLoop)
  chgvar &PfxLen (&PfxLen -1)
  goto Loop
EndLoop:
/* +
 ** - &PfxLen has the length of the prefix here. +
 ** - Determine the number of digits to use in the name +
*/

  chgvar &NumDig (&FnmLen - &PfxLen)

/* +
 ** - This should never happen, but let's be robust!        +
*/
  if (&NumDig = 4) then(chgvar &NumDig 3)

/* +
 ** - &FileNum will contain the number used to make the file name +
*/
 chgvar &FileNum 0

/* +
 ** - Suggest a filename - we'll use pfxddddd, where          +
 ** - ddddd will be &FileMum padded on the left with zeros    +
*/

TryFile:
  chgvar &FileNum (&FileNum + 1)

/* +
 ** - Whew! Ok, now let's make a file name...we'll use:         +
 ** - prefix + leading zeros + file number (pay attention now!) +
 ** - This gives us a file name in the form tmp00015.           +
*/
  chgvar &cFileNum &FileNum
  chgvar &StartAt (10 - &NumDig)
  chgvar &File (&PFx *tcat %sst(&cFileNum &StartAt &NumDig))

/* +
 ** Ok, let's see if this file exists...if id does we are       +
 ** going to loop back and do it allllll over again.            +
*/
  rtvmbrd file(&Lib/&File)
  monmsg msgid(CPF9812)                                         +
       exec(goto cmdlbl(NewFile))
  goto TryFile

/* +
 ** - We have a unique file name here - create the file         +
 ** - But...just in case another process has just grabbed       +
 ** - this name in the mean time, monitor for file exists       +
 ** - message and loop back if it happens.                      +
*/
NewFile:
             CRTPF      FILE(&LIB/&FILE) RCDLEN(&RECL)
  monmsg msgid(CPF5813)                                         +
       exec(goto cmdlbl(NewFile))

/* +
 ** - Just like downtown! We're done here. Beer thirty.         +
*/

endpgm

This article has been viewed 5703 times.
The examples on this page are presented "as is". They may be used in code as long as credit is given to the original author. Contents of this page may not be reproduced or published in any other manner what so ever without written permission from Idioma Software Inc.