TSO-ISPF JCL COBOL VSAM DB2 CICS Tools Articles Job Portal Forum Quiz Interview Q&A

Rexx File Operations


After completing this chapter, you will be able to perform file operations.


File operations:

REXX can perform file operations using TSO commands,

  • Datasets can be created, allocated, read and written using REXX.

  • ALLOC command is used to create / allocate datasets.

  • EXECIO command is used to read and write datasets.

  • FREE command is used to free the dataset allocation, so that the lock held by the REXX program on the particular dataset used will be released.


Create and Allocate Datasets:

Create Dataset: A new dataset can be created and allocated by using ALLOC with disposition as 'NEW'. Other dataset properties such as record length, record format, space parameters, type of dataset, block size, unit parameters and so on can be specified. Following is a sample ALLOC command to create a fixed block Partial Sequential dataset with record length as 80.

"TSO ALLOC F({DD name}) DA({dataset name}) NEW DIR(20)
SPACE(10,10) DSORG(PS) RECFM(FB) LRECL(80) BLKSIZE(0)"

An existing dataset can be allocated to a DD name by allocating it in share mode as,

"TSO ALLOC F({DD name}) DA({dataset name}) SHR"

Input – Output operations:

Input-Output file operations are performed through EXECIO command.

The read operation can be performed in the file by the command.

"EXECIO [linenum or *] DISKR or DISKRU ddname (FINIS STEM stemvar.”

The write operation can be performed in the file by the command

"EXECIO [linenum or *] DISKW ddname (FINIS STEM stemvar."

  • DISKR – Read operation

  • DISKW – Write Operation

  • DISKRU – Read operation for Update

  • Linenum – Record number from where the read operation should commence

  • FINIS – Close the file after Read/Write operation

  • STEM - Specifies the name of compound variable to which data is retrieved (or) from which data is read.

  • Stemvar. – Stem variable on to which the records read will be stored (or) or on to which the records to be written into the file will be present.

On execution of EXECIO command system variable RC will represent the status of the command execution.

Return Code (RC)Interpretation
0 Successful Execution
1 Data truncation
2 End of file is reached
20 Fatal Error

Sample EXECIO commands: To open a data set without reading any records, a zero has to be added immediately following EXECIO command and OPEN operand should be specified.

"EXECIO 0 DISKR mydd (OPEN"

To read a specific number of lines, number of lines to be read should be specified immediately following EXECIO command.

"EXECIO 25 DISKR mydd (FINIS STEM STEMVAR."

The above command will read first 25 records and store them in the stem variable STEMVAR.

To read the entire dataset, an asterisk immediate following the EXECIO command.

"EXECIO * DISKR TEMP (FINIS STEM A"

Here EXECIO performs a disk read operation of a pre-allocated file using a TSO ALLOC instruction and all the records are placed in the stem variable A. FINIS option should not be used if the next EXECIO statement continues reading at the line immediately following the last line read.

To read just 5 lines to the data stack starting at line 100, write the following:

"EXECIO 5 DISKR myindd 100 (FINIS"

To open a data set at line 100 without reading lines to the data stack, the following command can be used:

"EXECIO 0 DISKR myindd 100 (OPEN"

If the dataset is already open, no operation is performed for OPEN.

Note:

Users should be selective in using FREE ALL command, as it will free all existing dataset allocations.


De-allocate datasets:

Deallocation of files: Once the dataset is closed and no more operations need to be performed on the dataset, it can be de-allocated so that the lock held on the file can be released. The command used to de-allocate a dataset which was allocated to the ddname DDN is

“TSO FREE F(DDN)”

“TSO FREE ALL” command is used to free up all the dataset allocations including system dataset allocations.


Example 1:

Rename the input file name with .Bkup as a final qualifier using TSO command.

/* REXX */
/* Performing some basic TSO commands on PS file */
  SAY "ENTER THE HIGHLEVEL QUALIFIER"
  PULL HLQ
  SRCFILE = HLQ||'.SAMPLE.FILE'
  DSTFILE = HLQ||'.SAMPLE.FILE.BKUP'
  ADDRESS TSO
  "RENAME '"SRCFILE"' '"DSTFILE"' "
  EXIT 0

Explanation:

  • Address TSO command will transfer the control to TSO environment.

  • Rename command is used to rename the input file name.


Example 2:

Copy the contents of one file to another, using file operations.

/* REXX */
/* COPYING THE DATA FROM ONE FILE TO ANOTHER */
SAY "ENTER THE HIGHLEVEL QUALIFIER"
PULL HLQ
INFILE = HLQ||".SAMPLE.REXX"
OUTFILE = HLQ||".SAMPLE.OUTPUT"
ADDRESS TSO
"ALLOC F(INDD) DA('"||INFILE||"') SHR"
IF SYSDSN("'"OUTFILE"'") = 'OK' THEN
    DO
      "ALLOC F("OUTDD") DA('"OUTFILE"') SHR REUSE"
    END
ELSE
    DO
      "ALLOC F("OUTDD") DA("||"'"||OUTFILE||"'",
      ||") NEW SPACE (100,200) TRACK LRECL(80) RECFM(F) ",
      ||" BLKSIZE(0)"
    END
"EXECIO * DISKR INDD(FINIS STEM INREC."
"EXECIO * DISKW OUTDD(FINIS STEM INREC."
"FREE F(INDD)"
"FREE F(OUTDD)"
EXIT 0

Explanation:

  • Allocate the input file in share mode.

  • Check if the output file is present. If exists, allocate it in share mode. Otherwise, create and allocate.

  • Read the input file using EXECIO… DISKR command.

  • Write the output file using EXECIO DISKW command.

  • De-allocate input and output files.

  • Proper comments must be used for better understanding.

  • Indentation must be appropriate for better readability.


Summary:

  • Datasets can be created and / or allocated using ALLOC command.

  • Input-Output operations can be performed on the datasets through EXECIO command.

  • Datasets can be de-allocated using FREE command.


Test Your Understanding:

  • Which function is used to retrieve data from a particular address in Storage?

  • Which function is used to retrieve the detailed information about a dataset attributes?


If you have any doubts or queries related to this chapter, get them clarified from our Mainframe experts on ibmmainframer Community!

Are you looking for Job Change? Job Portal