× The internal search function is temporarily non-functional. The current search engine is no longer viable and we are researching alternatives.
As a stop gap measure, we are using Google's custom search engine service.
If you know of an easy to use, open source, search engine ... please contact support@midrange.com.



Hi Ron,

On Fri, 4 Jan 2002, Klein, Ron wrote:

> I would like to retrieve the contents or list of files and directories on
> the IFS.  I want to pull these into an RPGLE program so that I can display
> the list in a subfile.
>
> Is there a way to do this directly from an RPGLE program or is there an API
> that will return the list so that I can display it.  I looked at WRKLNK, but
> that will only get me a display on the screen or a printout and that is not
> a workable solution.

Yes...  there are a few different ways to approach this...

   a) You could use QSHELL to dump the files & directories to a stream
      file, and then open & reading that.  I don't like this option
      because it can become very complicated, especially if two users
      are reading the directory at once, and you need to differentiate
      between multiple copies of a file created by QSHELL...  It's also
      a lot of extra overhead if you're just reading one directory at
      a time.

   b) You could use the Qp0lProcessSubtree API.  With this one you
      give it some selection criteria, and a callback procedure, and
      it calls your proc with every filename it finds.  I don't like
      the "Qp0lxxxxx" APIs, I find them absolutely horrible to work
      with.  However, if you need to do pathnames that work in any
      international environment, this might be your best option.

   c) You could use the opendir()/readdir() APIs.  This is the way
      you'd do this on UNIX systems as well, and it's much easier
      to code, IMHO, than the awful Qp0lProcessSubtree type of thing.
      This is nice for reading one directory at a time.   If you need
      to recurse (i.e. list the contents of all the subdirectories
      within each directory) things can get a little more complicated.

Here's an example (in RPG IV) of using option c.  You might also find it
helpful to go to  http://archive.midrange.com/rpg400-l/index.htm
and search for "opendir" or "Qp0lProcessSubtree" for other examples.
In fact, searching the archives for this list might also be helpful...

but... at any rate, here's the example code:

     ** This is a simple test program, to demonstrate reading a directory
     **  using the IFS API with RPG IV.

     H DFTACTGRP(*NO) ACTGRP(*NEW) BNDDIR('QC2LE')

      ** <<CHANGE THIS!!>>
     D PATHTOLIST      C                   CONST('/QOpenSys/test/')

     D**********************************************************************
     D*
     D* Directory Entry Structure (dirent)
     D*
     D* struct dirent {
     D*   char           d_reserved1[16];  /* Reserved                       */
     D*   unsigned int   d_reserved2;      /* Reserved                       */
     D*   ino_t          d_fileno;         /* The file number of the file    */
     D*   unsigned int   d_reclen;         /* Length of this directory entry
     D*                                     * in bytes                       */
     D*   int            d_reserved3;      /* Reserved                       */
     D*   char           d_reserved4[8];   /* Reserved                       */
     D*   qlg_nls_t      d_nlsinfo;        /* National Language Information
     D*                                     * about d_name                   */
     D*   unsigned int   d_namelen;        /* Length of the name, in bytes
     D*                                     * excluding NULL terminator      */
     D*   char           d_name[_QP0L_DIR_NAME]; /* Name...null terminated   */
     D*
     D* };
     D*
     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

     D*--------------------------------------------------------------------
     D* Open a Directory
     D*
     D* DIR *opendir(const char *dirname)
     D*
     D*--------------------------------------------------------------------
     D opendir         PR              *   EXTPROC('opendir')
     D  dirname                        *   value options(*string)

     D*--------------------------------------------------------------------
     D* Read Directory Entry
     D*
     D* struct dirent *readdir(DIR *dirp)
     D*
     D*--------------------------------------------------------------------
     D readdir         PR              *   EXTPROC('readdir')
     D  dirp                           *   value options(*string)

      *-------------------------------------------------------------------
      * prototype defs stolen from ERRNO_H (requires BNDDIR('QC2LE'))
      *  Used for working with errors for the UNIX-type APIs
      *-------------------------------------------------------------------
     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 Name            S            640A
     D Msg             S             52A


     C* Step1: Open up the directory.
     C                   eval      dh = opendir(PATHTOLIST)
     C                   if        dh = *NULL
     c                   eval      Msg = 'opendir: '+ %str(strerror(errno))
     c                   dsply                   Msg
     c                   eval      *INLR = *ON
     c                   Return
     c                   endif

     C* Step2: Read each entry from the directory (in a loop)
     c                   eval      p_dirent = readdir(dh)
     c                   dow       p_dirent <> *NULL

     c                   eval      Name = %str(%addr(d_name))
     c                   if        Name<>'.' and Name<>'..'
     c                   movel     Name          Msg
     c     Msg           dsply
     c                   endif

     c                   eval      p_dirent = readdir(dh)
     c                   enddo

     C* Step3: End Program
     c                   dsply                   Pause             1
     c                   eval      *inlr = *On


      *-------------------------------------------------------------------
      * 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




As an Amazon Associate we earn from qualifying purchases.

This thread ...

Follow-Ups:
Replies:

Follow On AppleNews
Return to Archive home page | Return to MIDRANGE.COM home page

This mailing list archive is Copyright 1997-2024 by midrange.com and David Gibbs as a compilation work. Use of the archive is restricted to research of a business or technical nature. Any other uses are prohibited. Full details are available on our policy page. If you have questions about this, please contact [javascript protected email address].

Operating expenses for this site are earned using the Amazon Associate program and Google Adsense.