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

Easytrieve – Variable declaration

This post will help you to understand the dataypes and how to declare a variable in Easytrieve (which is the first step in learning any new language. believe me, Easytrieve is the simplest language that you can learn in a day).

First, we will see the different datatypes available.

Datatypes

Alphanumeric A
Numeric N Maximum length 18
Binary (Comp) B Maximum length 8. i e upto 18 digits
Packed (COMP-3) Define as P 0 Maximum length 9. i e upto 18 digits

A – Alphanumeric – can be used for variables containing Alphabets, numbers, special characters or any data. This is similar to PIC X(nn) in Cobol.

N – Numeric – can be used to have unpacked signed (or unsigned) numbers. This is similar to PIC S9(nn) in Cobol.

B- Binary – can be used to store signed (or unsigned) numbers in binary format. Length of this must be either 2 or 4 or 8 bytes. (similar to PIC S9(nn) COMP in Cobol)

P – Packed – for Packed numeric variables (similar to PIC S9(nn) COMP-3 in Cobol)

Declaring a working storage variable

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 WS-VAR1 W 8 A 
 WS-VAR2 W 8 A VALUE 'KARTHIK ' 
 WS-VAR2 W 9 N VALUE ZEROES 
 WS-VAR3 W 7 N 2 
 WS-VAR4 W 9 P 0 
 WS-VAR5 W 9 P 2

Variable declaration  statement has 4 mandatory parts

1. Variable name

2. Start position of the variable (should be given as W for Working storage variables; in File layouts, use the starting position – no need to add 4 for VB files, start with 1)

3. Number of bytes the variable occupies (note that this is not number of digits but number of bytes)

4. The datatype – A (Alpha), N (Numeric), B (Binary) or P (Packed)

For binary and packed variables if you want to have an implied decimal point (say for two digits), that number of digits should immediately follow the character P (packed), B for Binary.

For eg, the below variable WS-VAR4 is a binary, occupies 4 bytes (total) and has 3 digits (not bytes) after the implied decimal point. and the Packed variable WS-VAR5 occupies 9 bytes (total) and has 2 digits (not bytes) after the implied decimal point.

 WS-VAR4 W 4 B 3 
 WS-VAR5 W 9 P 2

Important note: For Packed variables, even if you don’t want to use any decimal, please declare as P 0 (meaning 0 digits after decimal). If you don’t do that, you may possibly get some error.

 WS-VAR6 W 9 P 0 

Declaring Group variables

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 WS-GROUP W 15 A 
 WS-GR1 WS-GROUP 5 A 
 WS-GR2 WS-GROUP +5 10 A 

Declaring a Group variable is easy – just use the High lever variable (+ offset) as the starting position for the variables that come under the group. Hope the above example helps you to understand.

Using indexed variables

In the below example, you can see an array WS-ALP-ARR with 26 items in it. In addition, it also redefines WS-ALPHA which is of 26 bytes length. We are also using an index ALPHA-NDX for the array WS-ALP-ARR. (We don’t have to declare the index variable)

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 LIST ON 
 FILE INFILE 
 WS-ALPHA W 26 A VALUE 'ABCDEFGHIJKLMNOPQRSTUVWXYZ' 
 WS-ALP-ARR WS-ALPHA 1 A OCCURS 26 INDEX ALPHA-NDX 
* 
 JOB INPUT NULL 
* PRINT 16TH ALPHABET 
 ALPHA-NDX = 15 
 DISPLAY '16TH ALPHABET IS ' WS-ALP-ARR 
 STOP. 

Using arrays/tables

In the above example, we saw an array with Index. Below is a simple array with no index. Hope this example is self explanatory.

----+----1----+----2----+----3----+----4----+----5----+----6----+----7--
 LIST ON 
 FILE INFILE 
 WS-DAYS-OF-WEEK W 10 A OCCURS 7 
* 
 JOB INPUT NULL 
 MOVE 'SUNDAY' TO WS-DAYS-OF-WEEK(1) 
 MOVE 'MONDAY' TO WS-DAYS-OF-WEEK(2) 
 MOVE 'TUESDAY' TO WS-DAYS-OF-WEEK(3) 
 MOVE 'WEDNESDAY' TO WS-DAYS-OF-WEEK(4) 
 MOVE 'THURSDAY' TO WS-DAYS-OF-WEEK(5) 
 MOVE 'FRIDAY' TO WS-DAYS-OF-WEEK(6) 
 MOVE 'SATURDAY' TO WS-DAYS-OF-WEEK(7) 
 DISPLAY '4 TH DAY IS ' WS-DAYS-OF-WEEK(4) 
 STOP 

Alos, read about REDEFINES in Easytrieve.

Input fields in Java Swing

Including a JButton:

 

// Name of this file should be simplejbutton.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class simplejbutton extends JApplet
{
JButton B = new JButton(“Click”);

public void init()
{
Container cp = getContentPane();
cp.setLayout(null);

cp.add(B);
B.setBounds(10,10,100,20);
B.setActionCommand(“B”);
B.addActionListener(mylistener);
}
// ActionListener for Button
private ActionListener mylistener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand() == “B”)
B.setText(“Clicked”);
}
};// End of ActionListener
} // End of the Class

Including a JLabel:

 

// This file should have name simplejlabel.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class simplejlabel extends JApplet
{
JLabel L = new JLabel(“Label”);
JButton B = new JButton(“Click”);

public void init()
{
Container cp = getContentPane();
cp.setLayout(null);

cp.add(L);
L.setBounds(10,10,100,20);

cp.add(B);
B.setBounds(120,10,100,20);
B.setActionCommand(“B”);
B.addActionListener(mylistener);
}
// ActionListener for Button
private ActionListener mylistener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand() == “B”)
L.setText(“Clicked”);
}
};// End of ActionListener
} // End of the Class

Including a JTextField

// This file should have name simplejtext.java
// Type something in Text Box and press Enter
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class simplejtext extends JApplet
{
JTextField T = new JTextField(“”);
JLabel L = new JLabel(“Label”);

public void init()
{
Container cp = getContentPane();
cp.setLayout(null);

cp.add(T);
T.setBounds(10,10,100,20);
T.setActionCommand(“T”);
T.addActionListener(mylistener);

cp.add(L);
L.setBounds(120,10,100,20);

}
// ActionListener for Button
private ActionListener mylistener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand()==”T”)
L.setText(T.getText());
}
};// End of ActionListener
} // End of the Class

Including a JCheckBox:

// This file should have name simplejcheckbox.java
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class simplejcheckbox extends JApplet {

JCheckBox C = new JCheckBox(“Click”);
JLabel L = new JLabel(“Label”);

public void init()
{
Container cp = getContentPane();
cp.setLayout(null);

cp.add(C);
C.setBounds(10,10,100,20);
C.setActionCommand(“C”);
C.addActionListener(mylistener);

cp.add(L);
L.setBounds(120,10,100,20);

}
// ActionListener for Button
private ActionListener mylistener = new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if (e.getActionCommand()==”C”)
{
if (C.isSelected() == true)
L.setText(“True”);
else
L.setText(“False”);
}
}
};// End of ActionListener
} // End of the Class

The above code is highlighted using Google Code Prettify.

Files in a Folder (VBA)

This Excel sheet can be used to get a list of files in a folder into a column of the Excel sheet.

Preview:

You just need to give the path of the folder in the text box and press enter. This works as simple as a browser.

Download:

Code for getting the file and folder names into excel cells is below:


Dim fs, f, f1, ffiles, ffolds
Set fs = CreateObject(“Scripting.FileSystemObject“)

If fs.folderexists(TextBox1.Text) Then
Else

a = MsgBox(“Folder Does not Exist“, , “Give Valid Path“)
GoTo ending

End If

Set f = fs.GetFolder(TextBox1.Text)
Set ffiles = f.Files
Set ffolds = f.subfolders

‘ Adding files to the column
For Each f1 In ffiles

Sheet1.Cells(i, 4).Value = f1.Name
Sheet1.Cells(i, 5).Value = “Go

With Sheet1

.Hyperlinks.Add .Range(Sheet1.Cells(i, 5).Address()), TextBox1.Text + “” + f1.Name

End With
i = i + 1

Next
j = 6

‘ Adding folders to the column
For Each f1 In ffolds

Sheet1.Cells(j, 7).Value = f1.Name
Sheet1.Cells(j, 8).Value = “Go
With Sheet1

.Hyperlinks.Add .Range(Sheet1.Cells(j, 8).Address()), TextBox1.Text + “” + f1.Name
End With
j = j + 1

Next


Creating a File System object:

Dim fs

Set fs = CreateObject(“Scripting.FileSystemObject“)


Operations with the file system object:

Opening an existing file:

Dim a, ForAppending=8

a = fs.OpenTextFile(“c:testfile.txt“, ForAppending, false);

This will open the file c:testfile.txt (Which is already existing) into the object “a” for Appending.

The second parameter to OpenTextFile method can be 1(ForReading), 2(ForWriting) or 8(ForAppending)