Temporary Files and Directories in COBOL

Throughout program execution, we often want to create data that isn’t needed after the program exits. Temporary files and directories are useful for this purpose since they don’t pollute the file system over time.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. TEMP-FILES-AND-DIRS.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT TEMP-FILE ASSIGN TO DYNAMIC WS-TEMP-FILE-NAME
           ORGANIZATION IS LINE SEQUENTIAL.
       DATA DIVISION.
       FILE SECTION.
       FD TEMP-FILE.
       01 TEMP-RECORD PIC X(4).
       WORKING-STORAGE SECTION.
       01 WS-TEMP-FILE-NAME PIC X(100).
       01 WS-TEMP-DIR-NAME  PIC X(100).
       01 WS-FILE-STATUS    PIC 99.
       01 WS-CURRENT-DATE.
          05 WS-YEAR        PIC 9(4).
          05 WS-MONTH       PIC 99.
          05 WS-DAY         PIC 99.
          05 WS-HOUR        PIC 99.
          05 WS-MINUTE      PIC 99.
          05 WS-SECOND      PIC 99.
          05 WS-MILLISECOND PIC 999.
       PROCEDURE DIVISION.
       MAIN-PROCEDURE.
           PERFORM CREATE-TEMP-FILE
           PERFORM CREATE-TEMP-DIR
           STOP RUN.

       CREATE-TEMP-FILE.
           MOVE FUNCTION CURRENT-DATE TO WS-CURRENT-DATE
           STRING "/tmp/sample" WS-YEAR WS-MONTH WS-DAY WS-HOUR 
                  WS-MINUTE WS-SECOND WS-MILLISECOND
                  DELIMITED BY SIZE
                  INTO WS-TEMP-FILE-NAME
           OPEN OUTPUT TEMP-FILE
           IF WS-FILE-STATUS NOT = 0
               DISPLAY "Error creating temp file"
               STOP RUN
           END-IF
           DISPLAY "Temp file name: " WS-TEMP-FILE-NAME
           MOVE X"01020304" TO TEMP-RECORD
           WRITE TEMP-RECORD
           CLOSE TEMP-FILE.

       CREATE-TEMP-DIR.
           MOVE FUNCTION CURRENT-DATE TO WS-CURRENT-DATE
           STRING "/tmp/sampledir" WS-YEAR WS-MONTH WS-DAY WS-HOUR 
                  WS-MINUTE WS-SECOND WS-MILLISECOND
                  DELIMITED BY SIZE
                  INTO WS-TEMP-DIR-NAME
           CALL "CBL_CREATE_DIR" USING WS-TEMP-DIR-NAME
           IF RETURN-CODE NOT = 0
               DISPLAY "Error creating temp directory"
               STOP RUN
           END-IF
           DISPLAY "Temp dir name: " WS-TEMP-DIR-NAME.

In COBOL, creating temporary files and directories is not as straightforward as in more modern languages. We have to manually generate unique names and use system calls for directory creation.

We start by defining a temporary file in the FILE-CONTROL section and a corresponding record in the FILE SECTION.

In the WORKING-STORAGE SECTION, we declare variables to store the temporary file and directory names, as well as a structure to hold the current date and time, which we’ll use to generate unique names.

The CREATE-TEMP-FILE paragraph creates a temporary file. We generate a unique name by concatenating “/tmp/sample” with the current date and time. We then open the file, write some data to it, and close it.

The CREATE-TEMP-DIR paragraph creates a temporary directory. Again, we generate a unique name, but this time we use the CBL_CREATE_DIR system call to create the directory.

Note that COBOL doesn’t have built-in functions for automatic cleanup of temporary files and directories. In a real-world scenario, you would need to implement this manually, possibly by keeping track of created temporary files and directories and deleting them at the end of the program.

To run the program:

$ cobc -x temp_files_and_dirs.cob
$ ./temp_files_and_dirs
Temp file name: /tmp/sample20230615120000123
Temp dir name: /tmp/sampledir20230615120000123

This example demonstrates how to work with temporary files and directories in COBOL, although the process is more manual compared to more modern programming languages.