problem: how to check if file exist

as topic. as you do in Natural?
I have a batch that generates a text file. sometimes this batch does not generate any text file.
my batch has to check if the text file exists! But I do not find any command that does this thing
I have to use the “on error” command? is there a better way?
thank you.
Tagan

Set a flag within the code that generates a text file

unfortunately I can not change the first batch.
But I know that the first batch has the define work files without the append attribute.
if I do so?


DEFINE DATA LOCAL
1 READLINE (A100)
END-DEFINE
*
DEFINE WORK FILE 1 'file.txt' ATTRIBUTES 'append'
WRITE WORK 1 VARIABLE '-no-data-content-'
CLOSE WORK 1
*
/* if file non exist, i create the file with "no data content" string and than i delete the file
READ WORK 1 READLINE
  IF READLINE = '-no-data-content-'
    WRITE 'NO DATA'
    PERFORM DELETE-FILE
    STOP
  ELSE
    CLOSE WORK 1
    ESCAPE BOTTOM
  END-IF
END-WORK
*
* if file exist, i work the file
READ WORK 1 READLINE
  IF READLINE = '-no-data-content-' /* this line exist because i create it in prev code
    ESCAPE TOP
  END-IF
  ....
  ....
  ....
  ....
END-WORK
*
DEFINE SUBROUTINE DELETE-FILE
DEFINE WORK FILE 1 'file.txt' ATTRIBUTES 'delete'
WRITE WORK 1 VARIABLE  'X'
CLOSE WORK 1
END-SUBROUTINE
END

this code don’t work if teh first batch, has the append attribute, but it isn’t this case.
I know that this code is not nice but I can not think of anything else! :cry:
do you think I might have some other side effects?
thank you ,
Tagan

Tagan,

You can call a subprogram that does a read against the work file. Put an ON ERROR block in it. If there is an error, return a error code to your calling program from the error block. If no error, your subprogram simply exits without an error code and you process the file in your program.

Alternatively, at least if the system is on Unix, you can issue a shell command to check for the files existence, then interrogate the return code from the shell call.

Hope one of these ideas helps.

In our Unix scripts we routinely touch each new file to create an empty instance before invoking Natural.

It’s a waste of CPU to check for a dummy record (-no-data-content-) on each iteration of the READ WORK, especially if the work file could contain millions of records.

You can still delete the file if the record count is zero.

If you are executing a separate Natural program for the read of the workfile it would be the easiest, as some people have already stated, to just let the JCL/script take care of the checking for you. Just don’t execute this program if the file does not exist, or create an empty file in the JCL if needed.

but it all depends on your requirements and maybe also on what OS you are running on.

thank you all.
I prefered create a subprogram of Todd Beckwith, so to use it in other cases.