-1

I have written a program for export some text files to a specific directory. So i preferred using MTIME is the best way to have a unique name but this will be a problem when multiple process exporting same file name using MTIME. Could you please tell me the best way to have a unique file name? Let me share some sample.

DEFINE INPUT PARAMETER ipData1 AS CHARACTER NO-UNDO.
DEFINE INPUT PARAMETER ipData2 AS CHARACTER NO-UNDO.
DEFINE INPUT PARAMETER ipData3 AS CHARACTER NO-UNDO.

DEFINE VARIABLE cExportData AS CHARACTER NO-UNDO FORMAT 'X(250)'.
DEFINE VARIABLE cPath AS CHARACTER NO-UNDO.
DEFINE VARIABLE cExt  AS CHARACTER NO-UNDO.
DEFINE VARIABLE cSFTL AS CHARACTER NO-UNDO FORMAT 'X(150)'.
DEFINE VARIABLE cMessageDateTime AS CHARACTER NO-UNDO.


ASSIGN
  cPath  = "R:\Downloads\progress\" 
  cExt   = ".Txt"
  cMessageDateTime = "123456789".

OUTPUT TO VALUE (cPath + cMessageDateTime + STRING(MTIME) + cExt ).   

put unformatted ipData1 skip ipData2 skip ipData3 skip "End."

OUTPUT CLOSE.
Thiru
  • 231
  • 6
  • 20

3 Answers3

5

You have several options:

  1. Use the program that Progress has supplied: adecomm/_tmpfile.p

    define variable fname as character no-undo format "x(30)". run adecomm/_tmpfile.p ( "xxx", ".tmp", output fname ). display fname.

  2. Use a GUID:

    define variable fname as character no-undo format "x(30)". fname = substitute( "&1&3&2", "xxx", ".tmp", GUID( GENERATE-UUID )). display fname.

  3. Ask Windows to do it (if you are always running on Windows):

    define variable fname as character no-undo format "x(30)". fname = System.IO.Path:GetTempFileName(). display fname.

  4. Trial and error:

    define variable fname as character no-undo.

    do while true:

    fname = substitute( "&1&3&2", "xxx", ".tmp", string( random( 1, 1000 ), "9999" )). file-info:filename = fname. if file-info:full-pathname = ? then leave. /* if the file does NOT exist it is ok to use this name */

    end.

    display fname.

Tom Bascom
  • 13,405
  • 2
  • 27
  • 33
  • 2
    3 B) Ask Linux to do it. That would be checking out `mktemp` that can create temporary files and directories. – Jensd Jun 05 '19 at 12:34
  • @Tom - Sorry to ask this question as I am new to this language. What does it mean "&1&3&3", "xxx", ".tmp"? – Thiru Aug 02 '20 at 01:47
  • SUBSTITUTE() replaces &1 etc with the arguments that come after the mask string. – Tom Bascom Aug 03 '20 at 01:09
0

You'll probably also need to pass in a token or an identifier of some sort to make this truly unique. Maybe username, or the machine's up, something like that. Then my advice would be concatenating that with

REPLACE (STRING(TODAY),'/','') + STRING(MTIME).

Edit: even though OP has flagged my answer as correct, it's not. Check Tom's answer to this for better options.

bupereira
  • 1,463
  • 8
  • 13
  • No.. This case also available – Thiru Jun 04 '19 at 05:54
  • No, this is wrong. It will NOT always be unique. Any system with multiple processes running could create two or more identically named files in this way. – Tom Bascom Jun 04 '19 at 19:01
  • 1
    My apologies, Tom is correct. I missed the part where you already have conflicting names, and I assumed this would only be spawned by the server. My mistake. You'll probably want to pass some kind of token that makes it unique along with what's in the answer. – bupereira Jun 04 '19 at 20:48
0

i have used this before.

("Filename" + STRING(TODAY,"999999") + ".csv").

Duke578
  • 71
  • 1
  • 9