REXX Tips & Tricks, Version 2.80


Inf-HTML [About][Toc][Index] 0.9b (c) 1995 Peter Childs


Get a name for a temporary file





 
/* routine to get an unique name for a temporary file                 */

                        /* example call                               */
  tempFile = GetTempFile()
  if tempFile <> "" then
    say "The name of the temporary file is " || tempFile
  else
    say "Error: Cannot find a name for a temporary file!"

                        /* close & delete the temporary file          */
  if tempFile <> "" then
  do
    call stream tempFile, "C", "CLOSE"
    "@del " tempFile "2>NUL 1>NUL"
  end /* if tempFile <> "" then */

exit 0

/* ------------------------------------------------------------------ */
/* function: Get an unique name for a temporary file                  */
/*                                                                    */
/* call:     GetTempFile                                              */
/*                                                                    */
/* returns:  name of the file                                         */
/*           or "" if no name was found                               */
/*                                                                    */
/* note:     If GetTempFile finds a name for a new temporary file,    */
/*           it opens this file to prevent it from being used by      */
/*           another process!                                         */
/*           The name of the file is in the format $$nnn$$.TMP where  */
/*           nnn is a number between 000 and 999.                     */
/*                                                                    */
GetTempFile: PROCEDURE

                        /* get the path for the temporary file        */
  tPath = value( "TEMP", , prog.__Env )
  if tPath = "" then
    tPath = value( "TMP", , prog.__Env ) )
  if tPath = "" then
    tPath = directory()

  tPath = translate( tPath )                                 /* v2.20 */
  tName = ""
                        /* save the current drive and directory       */
  CurDir = directory()

                        /* get the drive with the directory for the   */
                        /* temporary files                            */
  CurTDrive = filespec( "Drive", tPath )

                        /* save the current directory of the drive    */
                        /* with the directory for temporary files!    */
  CurTDir = directory( curTDrive )

  if directory( tPath ) = tPath then
  do

                        /* restore the current directory of the drive */
                        /* with the directory for temporary files!    */
    call directory CurTDir
                        /* restore the current drive and directory    */
    call directory CurDir

    tPath = strip( tPath, "B", '"' )
    if right( tPath, 1 ) <> "\" then
      tPath = tPath || "\"

    do i=0 to 999
      tName = tPath || "$$" || right( "000" || i, 3 ) || "$$" || ".TMP"
      if stream( tName, "C", "QUERY EXISTS" ) = "" then
        if stream( tName, "C",,
                   "OPEN WRITE" ) = "READY:" then            /* v2.20 */
          leave i

      tName = ""
    end /* do i=1 to 999 ... */
  end /* if directory() = ... */

RETURN tName

  

Inf-HTML End Run - Successful