Dynamic file creation

Web Programming Tutorial Part 4 - Dynamically Creating Files


This tutorial describes a technique that can be used to dynamically create files.



How can I dynamically create files?

To dynamically name files, try creating a variable that will hold your file name, and then wait to create the FD for you file until after you've generated your file name. This works because there really isn't any imposed order on statements in CobolScript programs like there is with regular COBOL. For instance:

      * file name gldi variable definition
        1  file_name_gldi.
           5 filler      pic x(n) value `file`.
           5 counter pic 99.
           5 filler      pic x(n) value `.dat`.

      * file name target variable
        1 file_name_var pic x(10).

      * file record definition
        1 file_record.
          5 field_1 pic 99.
          5 field_2 pic xx value `AB`.

        perform varying counter from 1 by 1 until counter > 8
           move file_name_gldi to file_name_var
           fd file_name_var record is 4 bytes
           open file_name_var for writing
           perform varying field_1 from 1 by 1 until field_1 > 10
              if field_1 > 5
                 move `CD` to field_2
              end-if
              write file_record to file_name_var
           end-perform
           close file_name_var
        end-perform.
        goback.

Alternatively, you could devise solutions to more complex scenarios using the EXECUTE statement; this statement is explained in the version 1.2 relase notes page here.