ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Writing from/to identical files in different libraries using variable libraries?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Writing from/to identical files in different libraries using variable libraries?

    Hello,

    I want to change the below program to use variable library names, but I can't figure out how to do this because of the external data structures (ExtName)? I've found that I can use a 21A variable to build a library/file name and then couple this with USROPN for the file, which is fine but ExtName requires a literal value - is my only option here to individually list each field, i.e. newFile.Field1 = oldFile.Field1, newFile.Field2 = oldFile.Field2 etc.? I really want to avoid doing this because there are 317 fields in the file...

    Note: I want to do this in RPG, rather than SQL as I'm currently using SQL INSERT and want to compare the two.


    Code:
    h Option(*NoDebugIO)      
    
     * Files                                                       
    foldFile   IF   E           K DISK    ExtFile('LIBRARY1/FILE1')
    f                                     ExtDesc('LIBRARY1/FILE1')
    f                                     Rename(FILE1R:FILE1RO)   
    fnewFile   IF A E           K DISK    ExtFile('LIBRARY2/FILE1')
    f                                     ExtDesc('LIBRARY2/FILE1')
    f                                     Rename(FILE1R:FILE1RN)   
    
     * Data structures                                             
    d oldFileDS     E DS                  ExtName('LIBRARY1/FILE1')
    d                                     Prefix(oDS_)             
    d newFileDS     E DS                  ExtName('LIBRARY2/FILE1')
    d                                     Prefix(nDS_)             
    
    * MAIN LINE                                     
    /Free                                           
    
        // Position after week                      
        SetGt (02:20:19:33) oldFile;                
        ReadE (02) oldFile;                         
    
        // Copy all records after week into new file
        DoW not %EoF(oldFile);                      
    
          newFileDS = oldFileDS;                    
          Write FILE1RN;                            
          ReadE (02) oldFile;                       
    
        EndDo;                                      
    
      // End of processing                          
      *InLr = *On;                                  
      Return;                                                                            
    
     /End-Free

  • #2
    You should be able to make your File specs user-controlled and use a variable for ExtFile. Just make sure the variable is set before you issue the open command.

    Comment


    • #3
      But what about a variable for the data structures "ExtName" also? It throws a wobbly saying "old/newFile definitions doesn't exist", then "data structure has no fields", the manual says you have to supply a literal value there.

      Comment


      • #4
        I don't approve of hard-coded libraries like this (except, perhaps, in a one-off program.) -- the only exception that I can think of is when accessing something like QTEMP which would never change.

        Please be aware that EXTDESC and the data structure EXTFILE keywords are only used to find the field informtion at COMPILE TIME, so using a variable for these doesn't make sense (the variables won't have any value at compile time). I would suggest using the library list, and setting your library list prior to compiling.

        For EXTFILE (which works at runtime) you could use a variable if you want to bypass the library list. That can be done quite easily by using a variable name for the ExtFile keyword, and being sure that this variable is set before the files are opened, or else place the USROPN keyword on the F-spec, then you can set the variables and use the OPEN opcode to open them prior to using them.

        Comment

        Working...
        X