Filenames

This chapter covers:

In this Chapter

Filename Conventions

This COBOL system, and applications created with it, use the standard filename conventions of the operating system on which it runs.

This COBOL system also supports the New Technology File System (NTFS) file naming conventions. This means that each file or directory can consist of up to 254 characters and contain both space characters, other special characters and any number of periods (.). This COBOL system regards any text following the final period as an extension (or a trailing period as a space extension) and may remove it when creating its own names.

If you are thinking of using the new file naming conventions, you should be aware of the following:

Assigning Filenames

The ASSIGN clause in the SELECT clause is used to specify either a physical filename or a logical name which can be mapped to a physical name at run time.

There are three types of filename assignment:

Run-time mapping of filenames is available for all three types of filename assignment.

Static Filename Assignment

With static filename assignment, the filename is specified in the SELECT clause as a literal:

 select filename
     assign to literal.

If the filename of the physical file that you are creating contains spaces, the filename is automatically surrounded by quotes for you.

In the following example, opening stockfile causes the file warehs.buy in the current directory to be opened:

 select stockfile
     assign to "warehs.buy".

In this example, opening input-file opens the file prog in the directory data (relative to the current directory):

 select input-file
     assign to "data/prog".

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.
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.

Note: If you use the ASSIGN"DYNAMIC" Compiler directive, you can omit the word DYNAMIC from the ASSIGN clause.

External Filename Assignment

With external filename assignment, the filename is specified in the SELECT clause as follows:

 select filename
     assign to external external-file-reference

where the parameters are:

filename The filename of the file that is to be assigned.
external-file-reference A COBOL word that identifies the specified file to the external environment for possible further mapping. If external-file-reference contains one or more hyphens, all characters up to and including the last hyphen are ignored.

For further details on run-time system filename mapping, see the section Filename Mapping.

Library Names in Paths

You can include a library name in the name of a file you want to open. The file specification is of the form:

 /library-name.lbr/filename.ext 
Example
/appdir/applib.lbr/app.dat

This type of filename is only valid when used as the name of a data file being opened in read-only mode, using either OPEN INPUT syntax or the CBL_OPEN_FILE byte stream routine. If the library is not already open, this call will open that library on the given device and leave it open after the call. If the library does not exist, or if the given filename.ext does not exist in that library, a "file not found" error is returned.

Filename Mapping

This COBOL system provides several ways of mapping the filename supplied by the program via the ASSIGN clause onto a different name, for greater flexibility at run time. These rely on the use of environment variables.

Before you run the program, you should give any environment varables you are using an appropriate value using the operating system SET command, for example:

set dir=d2

When presented with a filename (which may be a literal, the contents of a data item, or, in the case of the ASSIGN TO EXTERNAL syntax, an external reference), the File Handler follows this procedure:

  1. Isolates the first element of the filename, that is, all the text before the first slash character (/), all the text if the name does not include such a character or nothing if the filename starts with a slash character.
  2. If the first element begins with two dollar characters ($$), indicating that the file is located on a Fileshare server, switches the search process to that Fileshare server, and continues the filename mapping process with the next element.
  3. Removes the leading dollar character ($) if it is present, prefixes the characters "dd_" to this first element and searches for an environment variable with this name.
  4. If it does not find this environment variable and either the ASSIGN EXTERNAL syntax was used or the element began with a dollar character, looks for an environment variable with the same name as the element (minus the leading dollar character if present).
  5. If the search is unsuccessful, leaves the element unchanged, except in the case where the element begins with a dollar character and the whole filename contains at least one slash character. In this case, the whole of the first element, together with the first slash, is removed from the name.

This procedure is then repeated for the next element in the filename, and continues until all the elements in the name have been processed. The result is then considered to be the filename of the physical file.

Consider the following examples:

Filename in ASSIGN Clause Environment Variables Searched For Contents of Environment Variable Filename of Physical File
dir/file1 dd_dir d2 d2/file1
$dir/file1 dd_dir;dir d2/d4 d2/d4/file1
dir1/dir2/file1 dd_dir1 d4 d4/dir2/file1
$dir1/$dir2/file1 First iteration: dd_dir1;dir1 Second iteration: dd_dir2;dir2 dd_dir1 or dir1: d2/d4 dd_dir2 or dir2: d3 d2/d4/d3/file1
file1 dd_file1 d2 d2

Multiple Paths

An environment variable used for filename mapping can specify multiple pathnames. This causes the system to search for subsequent files if a "file not found" condition is returned for the first path specified by the environment variable.

Consider the following example contents of an environment variable named dd_dir:

dd_dir=/a/b;/c/d;

This causes the system to search /c/d for the assigned file if a "file not found" condition is returned on /a/b.

Notes:

Library Names

You can include library names in a multiple path environment variable. For example, if you have issued the command:

set test=/apdir;/aplbr/aplbr.lbr;/aplbr/aplbr1.lbr

the filename $test/app.dat would be resolved to /aplbr/aplbr.lbr/app.dat if /apdir/app.dat did not exist.

The libraries are searched in the order they appear in the environment variable.

You can also specify a library as part of the search path for program calls. For example, if you have issued the command

set appath=/apdir/progs1.lbr;/appdir/progs2.lbr 

you can use the format:

call "$appath/prog"

Your COBOL development system will search the libraries progs1.lbr and progs2.lbr for the program prog1. The libraries are searched in the order they appear in the environment variable. Any library searched remains open during the execution of the application, unless the application itself explicitly closes it.

Assigning Device Names

You can write a COBOL program to send a report directly to the printer or to transfer data across a communications port. To do this, you need to assign a device-name to your COBOL filename.

You can specify the following device-names using static, dynamic or external filename assignment:

Device-name Description
CON Console keyboard or screen
PRN First parallel printer
LPT1 First parallel printer
LPT2 Second parallel printer
LPT3 Third parallel printer
COM1 First asynchronous communications port
COM2 Second asynchronous communications port

When specifying any of these device-names, a trailing colon(:) is optional.

In the following example, read or write operations on fd-name cause data to be read from or written to the console screen:

select fd-name
   assign to "con".

In the next example, write operations on fd-name cause data to be output to lpt1:, the first parallel printer:

select fd-name
   assign to dynamic ws-filename.
   ...
   move "lpt1:" to ws-filename.

Setting Up Pipes

You can use COBOL file syntax to launch another process (such as the dir command) and either write data to the standard input of that other process or read data coming from the standard output of the other process. The COBOL file organization must be either LINE SEQUENTIAL or RECORD SEQUENTIAL.

Output Pipes

To launch a process and write data to its standard input, the filename must consist of the > sign followed by the name of the command. The file should be opened for output.

For example:

 select output-file
     assign to ">cmd /c sort"
     organization is line sequential.
 fd output-file. 
 01 output-file-record pic x(10).
 procedure division.
     open output output-file
     write output-file-record from "Charles"
     write output-file-record from "Bill"
     write output-file-record from "Alan"
     close output-file.

Input Pipes

To launch a process and read data from its standard output, the filename must consist of the "lt" symbol followed by the name of the command. The file should be opened for input.

For example:

 select input-file
     assign to "<cmd /c dir"
     organization is line sequential.
  ...
     open input input-file
     read input-file

In this example the program launches the dir process and reads in the first line that it writes to its standard output.

Two-way Pipes

Two-way pipes combine the functions of input and output pipes. To use a two-way pipe, the filename must consist of the pipe symbol (|) followed by the name of the command. The file should be opened for inpit-output i-o.

For example:

 select i-o-file
     assign to "| cmd /c sort"
     organization is line sequential.
 fd i-o-file.
 01 i-o-file-record pic x(20).
 procedure division.
     open i-o i-o-file
     write i-o-file-record from "Hello world"
     write i-o-file-record from all "A"
     write i-o-file-record from all "Z"
     write i-o-file-record from x"1a"
     perform until exit
         read i-o-file
          at end
             exit perform
         end-read
         display i-o-file-record
     end-perform
     close i-o-file

In this example, the program launches the sort process and passes it three lines of text, followed by an end-of-file marker. The program then reads all three lines back from the standard output of the sort process.