Saturday, 29 July 2017

SQLRPGLE UPPER (or LOWER) function: An alternative to %XLATE

In RPGLE, %XLATE is used to convert given string to upper or lower case (see below). Please note that this is just a work around, there is no inbuilt function to do this directly in RPGLE. To do this, two variables are defined with alphabets in upper and lower case which then be used with %XLATE function for string conversion.


0001.00 Hoption(*nodebugio:*srcstmt)                                        
0002.00  *                                                                  
0003.00 Dlowercase        s             26a   inz('abcdefghijklmnopqrstuvwxyz')
0004.00 Duppercase        s             26a   inz('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
0005.00 Dstring1          s             50a                                  
0006.00 Dstring2          s                   like(string1)                  
0007.00  *                                                                  
0008.00  /Free                                                              
0009.00                                                                      
0010.00     string1 = 'this string will be converted';                      
0011.00     dsply string1;                                                  
0012.00     string1 = %xlate(lowercase:uppercase:string1);                  
0013.00     dsply string1;                                                  
0014.00                                                                      

RESULT -

DSPLY  this string will be converted    
DSPLY  THIS STRING WILL BE CONVERTED    

We have another way of doing this - by using SQL function and this can be done in single shot. (see below)

0015.00     string1 = 'this is another way of converting';      
0016.00     dsply string1;                                      
0017.00     exec sql set :string2 = upper(:string1);            
0018.00     dsply string2;                                      

RESULT -

DSPLY  this is another way of converting  
DSPLY  THIS IS ANOTHER WAY OF CONVERTING  

Saturday, 22 July 2017

Writing records to flat file in RPGLE

While working on AS400 server, users often have to deal with flat files.
Declaring flat file in RPGLE - when a flat file is declared in RPGLE, make sure to rename record format because RPGLE won't allow you to have same file and record format name. In order to rename the record format we use RENAME keyword in F-spec.


01.00 hoption(*nodebugio:*srcstmt)                              
02.00  *                                                        
03.00 Femp       if   e             disk                        
04.00 Fflat      if a e             disk    rename(flat:rflat)  
05.00 F                                     prefix(fld_)        
06.00 F                                     usropn              
07.00  *                                                        
08.00 D command         pr                  extpgm('QCMDEXC')    
09.00 D                              200A   options(*varsize) const
10.00 D                               15P 5 const                
11.00  *                                                        
12.00 D string          s            200A                        
13.00  *                                                        
14.00  /free                                                    
15.00                                                            
16.00     string = 'clrpfm flat';                        
17.00     command (string:%len(%trim(string)));                  
18.00                                                            
19.00     if not %open(flat);                                    
20.00       open flat;                          
21.00     endif;                                
22.00                                          
23.00     read remp;                            
24.00     dow not %eof(emp);                    
25.00       %subst(fld_flat:1:5) = %char(empid);
26.00       %subst(fld_flat:7:27) = empname;    
27.00       %subst(fld_flat:31:60) = empaddrs;  
28.00       write rflat;                        
29.00       read remp;                          
30.00     enddo;                                
31.00                                          
32.00     *inlr = *on;                          
_________________________________________________________________________________

line 8 to 10 - declaring prototype of QCMDEXC program which is used to run CL commands in RPGLE.
line 16 to 17 - clearing flat file. Note that flat file is declared in user open (usropn) mode so that it can be cleared before performing any read/write action, otherwise we will get file already used error.
line 19 to 21 - opening flat file
line 23 - 30 - read emp file and write to flat file. 

Create Progress Bar in AS400 (IBMi)

Sometimes we have to deal with monitoring batch jobs in AS400 and it will be very difficult to predict the job run time especially when the number of records to process vary on daily basis. This case land us into a situation where we end up pressing F5 button until the job finishes.
To simplify our monitoring task, we can create a screen which shows the progress of job and percentage it completed (as shown below)

Progress Bar
Enough talk, lets jump to code -

Display file source -

             

RPGLE source -

01.00 Hoption(*nodebugio:*srcstmt)                              
02.00  *                                                        
03.00 FDUTIL001  cf   e             workstn                      
04.00  *                                                        
05.00 D cmdstring       s            512                        
06.00 D cmdlen          s             15p 5                      
07.00 D i               s              2p 0                      
08.00  *                                                        
09.00 Dqcmdexc          pr                  extpgm('QCMDEXC')    
10.00 D cmd                         3000a   const options(*varsize)
11.00 D cmdlen                        15p 5 const                
12.00 D                                3a   const options(*nopass)
13.00  /free                                                    
14.00    cmdstring = 'DLYJOB DLY(1)';                            
15.00    cmdlen = %len(%trim(cmdstring));                        
16.00    for i = 1 to 60 by 10;                                  
17.00      %subst(pgrbar:1:i) = *ALL'*';                        
18.00      pct = (i/60)*100;                                    
19.00      write  dsprcd;            
20.00      qcmdexc(cmdstring:cmdlen);
21.00    endfor;                    
22.00    exfmt dummy;
23.00    *inlr = *on;                

Here the trickiest part is - how to update the value of field without user interaction on screen.
While dong all these we have to make sure that the DFRWRT parameter should be *NO while compiling display file. Bingo.. there you go..

Batch FTP from system

Batch FTP is a process where FTP can be done via batch process. This is widely used process where we don't have any other alternative to send file to target system.
During batch FTP, commands are fired from file with INPUT override and FTP log is written to file to OUTPUT override file. Lets have an example

1.00 PGM
2.00 OVRDBF FILE(INPUT) TOFILE(LIBRARY/QCLSRC) MBR(FTPCMDS)
3.00 OVRDBF FILE(OUTPUT) TOFILE(LIBRARY/QCLSRC) MBR(OUT)
4.00 FTP RMTSYS(REMOTESYS)
5.00 ENDPGM  

Line 2 - FTPCMDS is a TXT type of member which contains list of commands. System will automatically read commands from FTPCMDS member and fire them one by one.
Line 3 – Member OUT with TXT type is used to dump FTP log.

LIBRARY/QCLSRC) MBR(FTPCMDS) data (to transfer savefile to target system) -
  • username password
  • cd library
  • bin
  • put filename filename
  • quit

How to fire commands at remote system – Commands that we use in AS400 (IBMi) via commandline or in CL program can also be run at target AS400 sytem via FTP. To do this we just have to add prefix 'quote rcmd' to the command that we running.
For example – If we want to create TEST flat file of length 320 in LIBRARY then use
quote rcmd CRTPF FILE(LIBRARY/TEST) RCDLEN(320)  

Friday, 21 July 2017

Download file from AS400 (IBMi) system to PC

There are many ways through which we can download file from IBMi system to local PC. One of the way is using command prompt.
Type below commands on command prompt -
  • I want to downalod my file to desktop, so first go to local directory – C:\Users\amits\Desktop
  • Connect with system – FTP IPAddress of system (or system name)
  • Provide username and password   
  • get library/filename filename.txt (to download in txt format)
  • quit

Progress bar in AS400 (version 2)

This is another version of progress which most people like (previous version is here ). Source of display file A                     ...