Dynamic Filename Assignment

With dynamic filename assignment, the filename is specified in the SELECT clause as a COBOL data item:

 select filename
     assign to dynamic data-item
where the parameters are:
filename The filename of the file that is to be assigned.
data-item The name of a COBOL data item. If the data item is not explicitly declared in your program, the Compiler creates one for you, with a picture of PIC X(255). Before the OPEN statement for the file is executed, the program must give a value to the data item.

If the filename of the physical file that you are creating contains spaces, you should surround the filename with quotes - see Example 2 below (applies to Windows environments only).

Example 1

In the following example, the file input.dat is created in the current directory:

   ...
 select fd-in-name
     assign to dynamic ws-in-file.
       ...
 working-storage section. 
 01 ws-in-file     pic x(30).
       ...
     move "input.dat" to ws-in-file.
       ...
     open output fd-in-name.

Example 2

The followingWindows example shows how to use quotes to create the file spacey filename.dat, where the filename contains spaces:

 select f1
      assign to dynamic f1-name.
... 
working-storage section.
01 f1     pic x(30).
...
move """spacey  filename.dat""" to f1-name
....
open output f1.
Note: If you use the ASSIGN"DYNAMIC" Compiler directive, you can omit the word DYNAMIC from the ASSIGN clause.