ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Merging .txt IFS files in Folder on AS/400

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

  • Merging .txt IFS files in Folder on AS/400

    I am getting difficulty in merging IFS files on AS/400.
    I have a below requirement, please advise how to proceed.

    I am getting 5 text files in AS/400 folder from window FTP server. i.e. PUT from Window into AS/400 folder. Then I have to merge these files into a single PF. Then my programs will use this PF for further process.

    I am getting difficulty to merge these 5 .txt files in on 1 .txt file, once it done then I will convert it into PF by CPYFRMIMPF.

    AS/400 IFS folder name: ?R3_222/NS? which is having 5 .txt files as (NSYYYYMMDDHHMMSS.txt format ? i.e.
    NS20100901101000.txt
    NS20100901102000.txt
    NS20100901103000.txt
    NS20100901104000.txt
    NS20100901105000.txt

    PF name R3_222/BKAC01I

    PF definition is: ( field BKAC01I is 2 byte CCSID type O)

    .....A..........T.Name++++++RLen++TDpB......Functi ons++++++++++++++++++++
    *************** Beginning of data ***************************************
    A R BKAC01IR
    A BKAC01I 1024O
    ****************** End of data ******************************************

    Many thanks in advance.

    Regards,
    Abhay

  • #2
    Re: Merging .txt IFS files in Folder on AS/400

    Why not have an pre-process that is CL based. Copy the IFS to a temporary Physical file, do a CPYF *ADD to the PF you want to use. Do this 5 times.

    Comment


    • #3
      Re: Merging .txt IFS files in Folder on AS/400

      In the current requirement we are getting multiple files with dynamic name (e.g. CLYYYYMMDDHHMMSS.txt) every 30 minutes from FTP server to AS/400 IFS folder (/R3_222/CL/).
      we have to merge these multiple dynamic named IFS files into a single fixed name IFS file and then convert (copy) that into Physical file (e.g. BKAC01I), then after that all existing programs will use this physical file for further process.

      The problem is how to merge multiple files with dynamic name (e.g. CLYYYYMMDDHHMMSS.txt) in IFS folder on AS/400

      Comment


      • #4
        Re: Merging .txt IFS files in Folder on AS/400
        1. Search this website for the IFS option to LIST Folder contents (DIR Statement) -- Jamie has posted some as well as others -- you can get the DIRLIST of the IFS Folder you need via RPG easily.
        2. Loop through this list, parse out each file name based on Desired Masking
        3. Perform a "CPYF *ADD" Routine into your Destination table
        4. Remove (Rename/Move) processed file so data isn't duplicated
        5. Wash/Rinse/Repeat

        Comment


        • #5
          Re: Merging .txt IFS files in Folder on AS/400

          this reads an IFS directory and gets a list of all documents in it.. then reads the one at a time
          you could use this a start
          PHP Code:
                *================================================================
                *  
          This program reads a directory in my case its
                
          *  pathname ==> '/home/xml/'
                
          *  it finds the first table in the folder that has a name
                
          *  this is not "." or ".." and then it reads that table.
                *  
          the table is read in chunks of 256 *characters --or--
                *  
          till it hits a carage return line feed (whatever)
                *  
          your code to processes this table would go within
                
          *  the do loop below
                
          *
                *================================================================
               
          H DFTACTGRP(*NOBNDDIR('QC2LE')
                *

                **************************************************************************
              * * 
          Prototypes and definitions for working with the IFS
                
          **************************************************************************
                *
                *  
          open -- open an IFS file
                
          *
               
          D open            pr            10i 0   ExtProc('open')
               
          D   filename                      *     value
               D   openflags                   10i 0   value
               D   mode                        10u 0   value options
          (*nopass)
               
          D   codepage                    10u 0   value options(*nopass)
                * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
                *  
          read -- read an IFS file
                
          *
               
          D read            pr            10i 0   ExtProc('read')
               
          D   filehandle                  10i 0   value
               D   datareceived                  
          *     value
               D   nbytes                      10u 0   value
                
          * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
                *  
          closeclose an IFS file
                
          *
               
          D close           pr            10i 0   ExtProc('close')
               
          D   filehandle                  10i 0   value
                
          * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
                * 
          values for oflag parameterused by open()
               
          D O_RDONLY        s             10i 0   inz(1)
               
          D O_TEXTDATA      s             10i 0   inz(16777216)

                * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
               
          D RdIfsFil        PR           256A
               D  FileName                    256A   
          CONST

               
          d opendir         PR              *   EXTPROC('opendir')
               
          d  dirname                        *   VALUE

               D readdir         PR              
          *   EXTPROC('readdir')
               
          D  dirp                           *   VALUE

               d xmlfield        s             50A   dim
          (9999)
               
          d xmlstart        s              4  0 dim(9999)
               
          d xmlend          s              4  0 dim(9999)
               
          d bighunkdata     s          65535
               d CodePage        S             10u 0 inz
          (819)
               
          d cr              C                   Const(x'0D')
               
          d data            s            256A
               d Data_Rec        S            256A
               d datasize        s              5  0
               d dh              S               
          *
               
          d Eol             C                   Const(x'0D25')
               
          d Error_Flag      S              1A   INZ('0')
               
          d File            S             50
               d FileName        S             50
               d Fp              S             10i 0
               d fnd             s              5  0
               d gt              s              5  0
               d len             s              5  0
               d lf              C                   
          Const(x'25')
               
          d lt              s              5  0
               d N               S              5  0
               d Name            S           2000A
               d Oflag           S             10i 0
               d Omode           S             10u 0
               d PathName        S           2000A
               d R               S              5  0
               d Rc              S             10i 0
               d slashremoved    s             50A
               d str             s              5  0
               d x               s              5  0
               Ã€
          *
               
          ÀDirectory Entry Structure (dirent)
               
          À*
               
          d p_dirent        s               *
               
          d dirent          ds                  based(p_dirent)
               
          d   d_reserv1                   16A
               d   d_reserv2                   10U 0
               d   d_fileno                    10U 0
               d   d_reclen                    10U 0
               d   d_reserv3                   10I 0
               d   d_reserv4                    8A
               d   d_nlsinfo                   12A
               d     nls_ccsid                 10I 0 OVERLAY
          (d_nlsinfo:1)
               
          d     nls_cntry                  2A   OVERLAY(d_nlsinfo:5)
               
          d     nls_lang                   3A   OVERLAY(d_nlsinfo:7)
               
          d     nls_reserv                 3A   OVERLAY(d_nlsinfo:10)
               
          d   d_namelen                   10U 0
               d   d_name                     640A
                
          *---------------------------------------------------------
                * 
          MAIN
                
          *---------------------------------------------------------
                *
                *
               
          c                   exsr      $GetFileName
                
          * If user entered an IFS path into the screen fieldread that file
               c                   
          If        FileName > *Blanks
               c                   
          Eval      Error_Flag RdIfsFil(Filename)
               
          c                   If        Error_Flag =  '1'
                
          Then Error
               c                   
          else
               
          c                   exsr      $fields
               C                   
          Endif
               
          C                   Endif
                *
               
          c                   eval      *inlr = *on
                
          *---------------------------------------------------------
                *    
          $Fields pull out the field names
                
          *---------------------------------------------------------
                /
          free
                     begsr $Fields
          ;

                  
          //
                  // put the spaces back
                  //
                      
          bighunkdata =  %xlate('|':' ':bighunkdata);

                      
          datasize = %len(%trim(bighunkdata));

                  
          //
                  // start the scanning portion..
                  //

                     
          str 1;
                     
          dou str >= datasize;
                     
          gt = %scan('<' bighunkdata str);
                      
          // we found a start lets find the end
                     
          if gt > *zeros;
                     
          lt = %scan('>' bighunkdata str);

                   
          // now we have the entire field lets strip off the "<" & ">"
                     
          if lt > *zeros;
                      
          len = (lt gt)+1;

                       if  %
          subst(%subst(bighunkdata:gt:len):2:1) = '/';
                        
          slashremoved = %replace('': %subst(bighunkdata:gt:len):2:1);
                        
          fnd = %lookup(slashremoved xmlfield );
                        if 
          fnd > *zeros;
                         
          xmlend(fnd) = lt-1;
                        endif;
                       else;
                         
          += 1;
                         
          xmlfield(x) = %subst(bighunkdata:gt:len);
                         
          fnd = %lookup(%subst(bighunkdata:gt:len) : xmlfield );
                         
          xmlstart(fnd) = gt+1;
                       endif;

                      endif;

                     endif;
                     
          str lt 1;
                     
          enddo;

                     
          endsr;

                /
          end-free
                
          *---------------------------------------------------------
                *    
          $GetFileName read thru directory and get filename
                
          *---------------------------------------------------------
               
          c     $GetFileName  begsr
                
          *
               
          c                   eval      pathname =  '/home/xml/'
               
          c                             + %trim(x'00')
               
          À*
               
          ÀStep1Open up the directory root and list all directories
               Ã€
          *
               
          c                   eval      dh opendir(%addr(PathName))
               
          À*
               
          c                   if        dh <>  *NULL
               Ã€
          *
               
          ÀStep2Read each entry from the directory (in a loop)
               
          À*
               
          c                   eval      p_dirent readdir(dh)
               
          c                   dow       p_dirent <> *NULL
               Ã€
          *
               
          ÀFIXMEThis code can only handle file/dir names 256 bytes long
               Ã€
          *         because thats the size of "Name"
               
          À*
               
          c                   if        d_namelen 256  and
               
          c                             %subst(d_name:1:1) <> 'Q'
               
          À*
               
          c                   eval      Name = %subst(d_name:1:d_namelen)
               
          À*
               
          À*  skip directory . and ..
               
          À*
               
          c                   if        Name <> '.'
               
          c                             and name <> '..'
               
          c                             and name <> *Blanks
               Ã€
          *
               
          c                   eval      filename =
               
          c                             '/home/xml/'  +
               
          c                             %trim(name)
               
          c                   leave
               Ã€
          *
               
          c                   endif
               
          c                   endif


               
          c                   eval      p_dirent readdir(dh)
               
          c                   enddo
               c                   
          endif
               
          À*
                *
               
          c                   endsr
                
          *-------------------------------------------------------------
                *    
          RdIfsFil Subprocedure To Read The IFS File
                
          *-------------------------------------------------------------
               
          p RdIfsFil        B                   Export
               d RdIfsFil        PI           256A
               d  FileName                    256A   
          Const
               
          d CharsRead       S             10i 0
               d CurChar         S              1
               d Eof             C                   
          const(x'00')


               
          c                   Eval      Oflag O_Rdonly O_Textdata
               c                   
          Eval      File = %trim(FileName) + x'00'
               
          c                   Eval      Fp open(%addr(File): Oflag)

               
          c                   If        Fp 0
               c                   
          Eval      Error_Flag = *On
               c                   
          Return    Error_Flag
               c                   
          Endif

               
          c                   Eval      0
               c                   
          Eval      0

               c                   Dou       CurChar 
          Eof
                
          *
               
          c                   Exsr      GetChar
               c                   
          Eval      +=1
               c                   
          Eval      %Subst(DataR1) = CurChar
                
          *
               
          c                   if        CurChar X'25'
               
          c                   eval      %Subst(DataR1)  = *blanks
               c                   
          endif

               
          c                   Select
               c                   When      R 
          256 or CurChar X'25'
                
          *
                * if 
          you find the  code then we still have more data in
                
          memory  and we need to process that remaining data.
                *
               
          c                   if        CurChar X'25'
               
          c                   eval      %Subst(DataR1)  = *blanks
               c                   
          endif
                *
                * 
          one record is here then (jamie)
               
          c                   clear                   R
               c                   clear                   Data


               c                   Endsl
               c                   Enddo

                
          Write last line and close the IFS File  (seton subfile end)
               
          c                   CallP     Close(Fp)

               
          c                   Return    Error_Flag
                
          *---------------------------------------------------------
                *    
          GetChar Process IFS RecordOne Character At A Time
                
          *---------------------------------------------------------
               
          c     GetChar       begsr
                
          *        If input buffer is empty, or all characters have been
                
          *          processedrefill the input buffer.
               
          c                   If        CharsRead
               c                   
          Eval      CharsRead Read(Fp:
               
          c                             %Addr(Data_Rec): 256)
               
          c                   Eval      = *Zero
               c                   
          Endif
                *        
          Get the next character in the input buffer.
               
          c                   If        CharsRead <= 0
               c                   
          Eval      CurChar Eof
               c                   
          Else
               
          c                   Eval      +=1
               c                   
          Eval      CurChar = %Subst(Data_RecN1)
               
          c                   select
               c                   when      CurChar 
          = *blanks or CurChar cr  or
               
          c                             CurChar lf
               c                   
          Eval      Bighunkdata = %trim(bighunkdata) +
               
          c                             '|'
               
          c                   other
               c                   
          Eval      Bighunkdata = %trim(bighunkdata) +
               
          c                             %trim(Curchar)
               
          c                   endsl
               c
               c                   
          Endif
               
          c                   Endsr
                
          *---------------------------------------------------------
               
          p RdIfsFil        E 
          Attached Files
          All my answers were extracted from the "Big Dummy's Guide to the As400"
          and I take no responsibility for any of them.

          www.code400.com

          Comment


          • #6
            Re: Merging .txt IFS files in Folder on AS/400

            hi Abhay:

            Merge all files in the directory:
            Code:
            copy NS*.* + AA*.* + BB*.* mynewfile.txt
            del    ns*.*
            del    aa*.*
            del    bb*.*
            Best of Luck
            GLS
            The problem with quotes on the internet is that it is hard to verify their authenticity.....Abraham Lincoln

            Comment


            • #7
              Re: Merging .txt IFS files in Folder on AS/400

              Why the "merge" requirement? Why not just import each file separately into your PF? Seems totally unnecessary to me.
              "Time passes, but sometimes it beats the <crap> out of you as it goes."

              Comment

              Working...
              X