File Paths in COBOL

The ENVIRONMENT DIVISION and DATA DIVISION in COBOL provide functionality similar to the filepath package in other languages. They allow us to work with file paths in a way that is portable between operating systems.

       IDENTIFICATION DIVISION.
       PROGRAM-ID. FILE-PATHS.
       ENVIRONMENT DIVISION.
       INPUT-OUTPUT SECTION.
       FILE-CONTROL.
           SELECT WORKFILE ASSIGN TO "dir1/dir2/filename"
           ORGANIZATION IS LINE SEQUENTIAL.
       DATA DIVISION.
       FILE SECTION.
       FD WORKFILE.
       01 WORK-RECORD PIC X(80).
       WORKING-STORAGE SECTION.
       01 WS-FILENAME PIC X(50).
       01 WS-DIRNAME  PIC X(50).
       01 WS-EXTENSION PIC X(10).
       
       PROCEDURE DIVISION.
           DISPLAY "File path: " FUNCTION CONCATENATE("dir1/", "dir2/", "filename")
           
           MOVE FUNCTION SUBSTITUTE(FUNCTION CONCATENATE("dir1//", "filename"), "//", "/")
               TO WS-FILENAME
           DISPLAY "Normalized path: " WS-FILENAME
           
           MOVE FUNCTION SUBSTITUTE(FUNCTION CONCATENATE("dir1/../dir1", "filename"), "../", "")
               TO WS-FILENAME
           DISPLAY "Resolved path: " WS-FILENAME
           
           MOVE FUNCTION REVERSE(FUNCTION REVERSE(WS-FILENAME))
               TO WS-DIRNAME
           DISPLAY "Directory: " WS-DIRNAME
           
           MOVE FUNCTION REVERSE(FUNCTION SCAN(FUNCTION REVERSE(WS-FILENAME), "/"))
               TO WS-FILENAME
           DISPLAY "Filename: " WS-FILENAME
           
           IF WS-FILENAME(1:1) = "/"
               DISPLAY "Is absolute path: TRUE"
           ELSE
               DISPLAY "Is absolute path: FALSE"
           END-IF
           
           MOVE "config.json" TO WS-FILENAME
           MOVE FUNCTION REVERSE(FUNCTION SCAN(FUNCTION REVERSE(WS-FILENAME), "."))
               TO WS-EXTENSION
           DISPLAY "Extension: " WS-EXTENSION
           
           MOVE FUNCTION SUBSTITUTE(WS-FILENAME, WS-EXTENSION, "")
               TO WS-FILENAME
           DISPLAY "Filename without extension: " WS-FILENAME
           
           STOP RUN.

In COBOL, we use the ENVIRONMENT DIVISION to define file paths and the DATA DIVISION to declare variables for storing file information.

The PROCEDURE DIVISION contains the main logic:

  1. We use FUNCTION CONCATENATE to join path components, similar to the Join function in other languages.

  2. FUNCTION SUBSTITUTE is used to normalize paths by removing superfluous separators.

  3. To get the directory and filename parts, we use FUNCTION REVERSE and FUNCTION SCAN to manipulate the string.

  4. Checking if a path is absolute is done by examining the first character of the filename.

  5. To extract the file extension, we use FUNCTION REVERSE and FUNCTION SCAN to find the last occurrence of a dot.

  6. Removing the extension is done with FUNCTION SUBSTITUTE.

Note that COBOL doesn’t have built-in functions for all file path operations, so we’ve simulated some behaviors using string manipulation functions. In a real COBOL program, you might use system-specific calls or additional libraries for more complex file path operations.

To run this COBOL program, you would typically compile it and then execute the resulting program. The exact commands may vary depending on your COBOL compiler and environment.

$ cobc -x file-paths.cob
$ ./file-paths

This example demonstrates basic file path manipulations in COBOL, adapting the concepts from the original example to COBOL’s syntax and available functions.