Midrange News for the IBM i Community


Posted by: Jeff Masak
IT Developer
The Boldt Co
Appleton, WI
Passing a file name into program and using it
has no ratings.
Published: 09 Jan 2013
Revised: 23 Jan 2013 - 4082 days ago
Last viewed on: 28 Mar 2024 (6898 views) 

Using IBM i? Need to create Excel, CSV, HTML, JSON, PDF, SPOOL reports? Learn more about the fastest and least expensive tool for the job: SQL iQuery.

Passing a file name into program and using it Published by: Jeff Masak on 09 Jan 2013 view comments(8)

Is it possible to pass a file name into a program and then use it for close, open. read ?

Need to ovrdbf inside the RPG and then open, read (do something) close it.  on multiple files using the same do something.

 

Thanks 

Jeff

Return to midrangenews.com home page.
Sort Ascend | Descend

COMMENTS

(Sign in to Post a Comment)
Posted by: Ringer
Premium member *
Comment on: Passing a file name into program and using it
Posted: 11 years 2 months 18 days 16 hours 59 minutes ago

Are the files all the same format?

If yes, pass the file name in as a parm and use the F-Spec ExtFile / ExtMbr keyword. Google it. Use UsrOpn on the F-spec. Open the file yourself with Open(e) MYFILE ;

If ( %Error is on ), something went wrong.

http://www.mcpressonline.com/rpg/tips-and-techniques-extfile-and-extmbr.html

However, if you do both an OVRDBF and an ExtFile/ExtMBR, the OVRDBF takes precedence.

Chris Ringer

 

Posted by: neilrh
Premium member *
Jackson, MI
Comment on: Passing a file name into program and using it
Posted: 11 years 2 months 18 days 2 hours ago

If the extfile/extmbr variables are passed as parameters you don't need to usropn on the file. When the file is open the variables used must contain something valid, this is true during program initialization for parameter fields.

Posted by: Ringer
Premium member *
Comment on: Passing a file name into program and using it
Posted: 11 years 2 months 18 days 35 minutes ago

True. I like to trap for errors I guess. If the program is to be called in a loop, make sure you close the file when this program ends so the next open (implied or explicit) uses the new parm values.

Chris Ringer

Posted by: neilrh
Premium member *
Jackson, MI
Comment on: Passing a file name into program and using it
Posted: 11 years 2 months 17 days 23 hours 41 minutes ago

True enough. When I've implemented this sort of process cycle, the driver has been an import control file - which lists file & member name. So the read of this control file, "override" and open of the process file is all within one program. Thus you need the user control and you can monitor for attempts to open invalid file/member.

Posted by: DaleB
Premium member *
Reading, PA
Comment on: Passing a file name into program and using it
Posted: 11 years 2 months 17 days 22 hours 29 minutes ago

Note that using the EXTFILE and EXTMBR assumes all of the files are the same format, which was Chris's original question. Doesn't mean you can't do what your asking if they're not all the same format, but it's going to be more complicated. How much more complicated depends on how different the files are, and what exactly you're trying to do with them.

Posted by: bobcozzi
Site Admin ****
Chicagoland
Comment on: Passing a file name into program and using it
Posted: 11 years 2 months 17 days 21 hours 17 minutes ago

Jeff,


If this is on one file format, then see the earlier replies.

If this is a utility, like those I've been writing for 30+ years, then you can do an OVRDBF inline in your code, and use a program-described Input/Output spec to make it work. Include the LVLCHK(*NO) on the file in this context.

I've also done this with externally described files but tend to use the EXTFILE/EXTMBR keywords and then check the INFDS for the Opened file's record length and adjust accordingly. For example, for RPG IV source code with different source record lengths.

Posted by: jmasak
Premium member *
Appleton, WI
Comment on: Passing a file name into program and using it
Posted: 11 years 2 months 17 days 16 hours 49 minutes ago
Edited: Thu, 10 Jan, 2013 at 16:27:01 (4095 days ago)

Each file has a different file layout.  But all have a the same field in them (i.e. employee#).  The field is defined the same (using a reffld from a reference file) but is in diffent locations in the record layout with different naming conventions. 

 

Employee # in a payroll master file is the first field in the record format and maybe name prmemp#.  In a user master file record it maybe the 4 field and be named umemp#.

I have used the OVRDBF in rpg code and it works great.  Just trying not to duplicate code.

I.E find employee number in the payroll master and do something, and then check the employee number in the user master and complete the same task.

Thanks Jeff

 

 

Posted by: bobcozzi
Site Admin ****
Chicagoland
Comment on: Passing a file name into program and using it
Posted: 11 years 2 months 17 days 14 hours 39 minutes ago

You could use Dynamic Embedded SQL to do what you want.

 

 /free
     myQry = 'SELECT EMPLOY# FROM ' + %trimR(theFile) +
               WHERE EMPLOY# = ' + %char(empNo);
     EXEC SQL PREPARE dynoSQL FROM :myQry;
     EXEC SQL DECLARE E1 CURSOR for dynoSQL;
     EXEC SQL OPEN E1;
     EXEC SQL FETCH E1 INTO :empNo;
     DOW (SQLState < '02000');
         // whatever
     EXEC SQL FETCH E1 INTO :empNo;
     enddo;
     EXEC SQL CLOSE E1;

 Of course you'd have to modify the SQL but since the field name is the same, you can extract it from any of the files using as complex a WHERE clause as you need to.