Easytrieve – File Handling and File Declaration

This post will help you in how to Declare a File, Read / Write.

File declaration examples

File declaration statement will have the keyword ‘FILE’ and then the DD Name of the file.

 FILE FILE1

In addition to the Keyword ‘FILE’ and the DD Name, mention the file type VB – variable block, FB – fixed block, VS for VSAM. Also, provide the Record length and blocksize in the format (<LRECL>, <Block Size>)

Fixed block files

 FILE FILE1 FB(80 200)
* 80 is LRECL and 200 is blocksize

Variable block Files

 FILE FILE1 VB(80 200)

VSAM files

 FILE FILE1 VS

File record definition

The below example shows how to declare file variables. Also, you can read about Macros (Copybooks) in Easytrieve.

 FILE FILE1 FB(80 200)
   IN-REC 1 80 A
   IN-FIELD1 IN-REC 40 A
   IN-FIELD2 IN-REC +40 20 A
   IN-FIELD3 IN-REC +60 20 A

(OR)

 FILE FILE1 FB(80 200)
   IN-REC 1 80 A
   IN-FIELD1 1 40 A
   IN-FIELD2 41 20 A
   IN-FIELD3 61 20 A

If field1 and Field2 are not used, then just Field3 can be defined

 FILE FILE1 FB(80 200)
   IN-REC 1 80 A
   IN-FIELD3 61 20 A

Simply use * as starting position, if you declare all the variables in contiguous locations, and you are not using any group variables.

 FILE FILE1 FB(80 200)
   IN-FIELD1 * 40 A
   IN-FIELD2 * 20 A
   IN-FIELD3 * 20 A

Reading File record by record and copy it to another file

EOF <DD NAME> checks the End of File, and returns true when there are no more records to fetch.

GET <DD NAME> statement is used to read one record from the file

PUT <DD NAME> statement is used to write one record to the file

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 LIST ON
 FILE INFILE
 FILE OUTFILE
 JOB INPUT NULL
 GET INFILE
 DO WHILE NOT EOF INFILE
   PUT OUTFILE FROM INFILE
   GET INFILE
 END-DO
 STOP 

(Or)

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 LIST ON
 FILE INFILE FB(80 200)
   IN-REC 1 80 A
 FILE OUTFILE FB(80 200)
   OUT-REC 1 80 A
*
 WS-COUNT W 4 N VALUE 0
*
 JOB INPUT NULL
 GET INFILE
 DO WHILE NOT EOF INFILE
   MOVE IN-REC TO OUT-REC
   PUT OUTFILE
   WS-COUNT = WS-COUNT + 1
   GET INFILE
 END-DO
 DISPLAY WS-COUNT ' RECORDS WRITTEN'
 STOP 

(Or Simply,)

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 LIST ON
 FILE INFILE FB(80 200)
   IN-REC 1 80 A
 FILE OUTFILE FB(80 200)
   OUT-REC 1 80 A
*
 WS-COUNT W 4 N VALUE 0
*
 JOB INPUT INFILE
    PUT OUTFILE FROM INFILE
 END-JOB

Writing to Variable block files

For Variable block files, in addition to writing the record, we also need to specify the Record length of the record. Below example will help you understand on how to do that. (RECORD-LENGTH keyword is the one that you need to note)

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 LIST ON 
 FILE INFILE VB(80 200)
   IN-REC 1 80 A
 FILE OUTFILE VB(80 200)
   OUT-REC 1 80 A  
 JOB INPUT NULL 
 GET INFILE 
 DO WHILE NOT EOF INFILE
   MOVE IN-REC TO OUT-REC
   OUTFILE : RECORD-LENGTH = INFILE : RECORD-LENGTH
   PUT OUTFILE
   GET INFILE 
 END-DO 
 STOP  

Reading VSAM file by Key

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 LIST ON
 FILE FILE1 FB(80 200)
   IN-REC 1 80 A
   IN-KEY 1 8 A  
 FILE FILE2 VS
   F2-REC 1 80 A
   F2-KEY 1 8 A
   F2-VALUE 15 30 A  
 FILE OUTFILE FB(80 200)
   OUT-REC 1 80 A
   OUT-KEY 1 8 A
   OUT-VALUE 10 30 A  
 JOB INPUT NULL 
 GET FILE1 
 DO WHILE NOT EOF INFILE
   READ FILE2 KEY IN-KEY STATUS
   IF FILE2 : STATUS EQ 0      
      MOVE IN-KEY TO OUT-KEY     
      MOVE F2-VALUE TO OUT-VALUE     
      PUT OUTFILE  
   ELSE     
      DISPLAY IN-KEY ' NOT PRESENT IN VSAM FILE'   
   END-IF 
   GET INFILE
 END-DO 
 STOP  

Also, you can read Writing / Updating VSAM records

Advertisement

5 thoughts on “Easytrieve – File Handling and File Declaration”

  1. Could you please share PDS dataset handling . Primary requirement is to write to a REXX program that:
    1. Reads a PS record-by-record.
    2. Searches the string in a PDS
    3. Then copies the member containing that string into another PDS

    Is this possible ?

    Really appreciate it .

  2. Informative one…..Thanks a lot…. I am a beginner to easytrive………Keep Up!!!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.