ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Read names of all files in an ifs directory

Collapse
This topic is closed.
X
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Read names of all files in an ifs directory

    Hello,

    Does anyone have an example of an rpgle program reading all files in a an ifs directory.

    I want to use this to build a java classpath.

    Thank You

  • #2
    Re: Read names of all files in an ifs directory

    Look here http://www.scottklement.com/rpg/ifs.html it has example code you can download that does just what you want.

    Comment


    • #3
      Re: Read names of all files in an ifs directory

      Here is a very basic program. It reads the /home directory and displays the names that it finds:
      Code:
           H dftactgrp( *NO )
           H   actgrp( *NEW )
           H bnddir( 'QC2LE' )
      
           D PATHTOLIST      c                   const( '/home' )
      
            * 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                    8
           D   d_nlsinfo                   12
           D     nls_ccsid                 10i 0 overlay( d_nlsinfo:1 )
           D     nls_cntry                  2    overlay( d_nlsinfo:5 )
           D     nls_lang                   3    overlay( d_nlsinfo:7 )
           D     nls_reserv                 3    overlay( d_nlsinfo:10 )
           D   d_namelen                   10u 0
           D   d_name                     640
      
            * Open a Directory
      
           D opendir         pr              *   extproc( 'opendir' )
           D  dirname                        *   value options( *string )
      
            * Read Directory Entry
      
           D readdir         pr              *   extproc( 'readdir' )
           D  dirp                           *   value options( *string )
      
            * Close a Directory
      
           D closedir        pr            10i 0 extproc( 'closedir' )
           D  dirp                           *   value options( *string )
      
            * Check for Sub-directory
      
           D stat            pr            10i 0 extproc( 'stat' )
           D  #sPath                         *   value options( *string )
           D  #sBuf                          *   value
      
           D @__errno        pr              *   extproc( '__errno' )
           D strerror        pr              *   extproc( 'strerror' )
           D    errnum                     10i 0 value
           D errno           pr            10i 0
      
      
           D* a few local variables...
           D dh              s               *
           D rc              s             10i 0
           D Rpy             s              1
           D Name            s            640
           D Msg             s             52
           D rtnVal          s             10u 0
      
           D stat_struct     ds
           D  st_other1                    48
           D  st_objtype                   10
           D  st_other2                    68
      
            /free
             //  Open up the directory.
             dh = opendir( PATHTOLIST ) ;
             if  dh = *NULL ;
                Msg = 'opendir: ' + %str( strerror( errno ) ) ;
                dsply  Msg ;
                *inlr = *ON ;
                return ;
             endif ;
      
             //  Read each entry from the directory (in a loop)
             p_dirent = readdir( dh ) ;
             dow  p_dirent <> *NULL ;
      
                Name = %str( %addr( d_name ) ) ;
                rtnVal = stat( %trim(PATHTOLIST + '/' + d_name) :
                               %addr(stat_struct) ) ;
      
                if rtnVal = 0 ;
                   if st_objType = '*DIR      ' ;
      
                   // it's a directory, but there are
                   // two cases we will ignore...
                      if %str( %addr(d_name) ) <> '.'
                         AND
                         %str( %addr(d_name) ) <> '..' ;
      
                      // process the directory entry...
                      // (could process recursively here...)
                         Msg = 'Dir: ' + Name ;
                         dsply Msg ;
      
                      endif ;
      
                   elseif st_objType = '*STMF     ' ;
                   // it's a streamfile...
                      Msg = '     ' + Name ;
                      dsply Msg ;
                   endif ;
                endif ;
      
                p_dirent = readdir( dh ) ;
             enddo ;
      
             //  Close the directory
             rc = closedir( dh ) ;
      
             //  End Program
             dsply  ' ' '' Rpy ;
             *inlr = *ON ;
             return ;
            /end-free
      
            * Retrieve the error number for UNIX-type APIs
      
           P errno           b
           D errno           pi            10i 0
           D p_errno         s               *
           D wwreturn        s             10i 0 based( p_errno )
           C                   eval      p_errno = @__errno
           c                   return    wwreturn
           P                 e
      The directory name is taken from the PATHTOLIST constant. In this case, the constant is '/home'; but it could be a variable that can have any path as a starting point. It could be in incoming parm value.

      This code is written as a program. It could be modified for use as a procedure. And as a procedure, each sub-directory name that is read from the current directory could be passed back in for processing with a recursive call.
      Tom

      There are only two hard things in Computer Science: cache invalidation, naming things and off-by-one errors.

      Why is it that all of the instruments seeking intelligent life in the universe are pointed away from Earth?

      Comment


      • #4
        Re: Read names of all files in an ifs directory

        Here's another method:
        Read through for news and resources on software development topics, including low-code-no-code, serverless computing and programming languages.

        I notice the pictures are no longer displaying but the source is still there.
        Regards

        Kit
        http://www.ecofitonline.com
        DeskfIT - ChangefIT - XrefIT
        ___________________________________
        There are only 3 kinds of people -
        Those that can count and those that can't.

        Comment


        • #5
          Re: Read names of all files in an ifs directory

          WOW!!!

          All of these suggestions are great. You guys(and gals) are the coolest.

          Comment

          Working...
          X