bvstone

Reading Through an IFS Directory With RPG and Uploading the Files to Google Drive

Posted:

Reading Through an IFS Directory With RPG and Uploading the Files to Google Drive

Recently I was asked by a customer how to use GreenTools for G Suite (G4G) to upload all the files in a directory in the IFS to their Google Drive.

G4G doesn't have any wildcard or "entire folder" options (yet) but I thought since I have a few programs that do read through directories it would be fairly simple to put together an example.

Program DIRLOOP was created and looks like this:

     H DFTACTGRP(*NO) BNDDIR('BVSTOOLS') ACTGRP('BVSTOOLS')
      ****************************************************************
      * Prototypes
      ****************************************************************
      /COPY QCOPYSRC,P.G4GDRV
      *
     D stat            PR            10I 0 EXTPROC('stat')
     D  filename                       *   VALUE Options(*String)
     D  bufpointer                     *   VALUE
      *
     D opendir         PR              *   EXTPROC('opendir')
     D  dirname                        *   VALUE options(*string)
      *
     D closedir        PR            10I 0 EXTPROC('closedir')
     D  dirhandle                      *   VALUE
      *
     D readdir         PR              *   EXTPROC('readdir')
     D  dirp                           *   VALUE
      ****************************************************************
     D StatDS          DS           128
     D  st_mode                      10U 0
     D  st_ino                       10U 0
     D  st_nlink                      5U 0
     D  st_reserved1                  2A
     D  st_uid                       10U 0
     D  st_gid                       10U 0
     D  st_size                      10U 0
     D  st_atime                     10U 0
     D  st_mtime                     10U 0
     D  st_ctime                     10U 0
     D  st_dev                       10U 0
     D  st_blksize                   10I 0
     D  st_allocsize                 10I 0
     D  st_objtype                   10A
     D  st_reserved2                  2A
     D  st_codepage                   5U 0
     D  st_ccsid                      5U 0
     D  st_reserved3                 57A
     D  st_ino_gen_id                10U 0
      *
     D dirEnt@         s               *
     D dirEnt          ds                  based(dirEnt@)
     D   d_reserved1                 16A
     D   d_fileno_gen_id...
     D                               10U 0
     D   d_fileno                    10U 0
     D   d_reclen                    10U 0
     D   d_reserved2                 10I 0
     D   d_reserved3                  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 directory       S            256    INZ('/tmp/testfiles')
     D gFolder         S            256    INZ('/temp/testfiles')
     D gFolderID       S            256    INZ('0B2Sd62H63YjoM0lzanJ3Rzh5TUU')
     D fileName        S            256
     D qualFileName    S            256
     D errorMsg        S            256
     D newFileID       S            256
     D id              S            256    INZ('bvstone@gmail.com')
     D dir@            S               *
     D rc              S             10I 0
      *****************************************************************
      /free
         dir@ = opendir(%trimr(directory));

         if (dir@ = *NULL);
           //error - Directory Not Found
           exsr $Return;
         endif;

         dirEnt@ = readdir(dir@);

         dow (dirEnt@ <> *NULL);
           fileName = %subst(d_name:1:d_namelen);
           qualFileName = %trim(directory) + '/' + fileName;

           if (stat(%trim(qualFileName):%addr(StatDS)) < 0);
             //error
             exsr $Return;
           endif;

           if (st_objtype = '*STMF') or
              (st_objtype = '*DSTMF') or
              (st_objtype = '*DOC');
             rc = #g4gdrv_setValue('id':id:errorMsg);

             // We can either use the fully qualified folder path or the ID of the
             //  folder we want to upload to.  The latter is much faster.
             //rc = #g4gdrv_setValue('upload_to_folder':gFolder);
             rc = #g4gdrv_setValue('in_parent_id':gFolderID);
             rc = #g4gdrv_setValue('upload_file':qualFileName);

             rc = #g4gdrv_upload(newFileID:errorMsg);

             if (rc < 0);
               //error uploading file
                 exsr $Return;
             endif;

           endif;

           dirEnt@ = readdir(dir@);
         enddo;

         closedir(dir@);
         exsr $Return;
       //***************************************************************
       //* Return
       //***************************************************************
       begsr $Return;

         *INLR = *ON;

       endsr;
      /end-free

The program is a very simple one and I tried to put all the prototypes and variables in the program that would be needed.  The only thing that isn't is the /copy member for P.G4GDRV which is part of the G4G application

This program reads through the IFS directory /tmp/testfiles.  For each file we then call the #g4gdrv_upload() function to upload the file to the /temp/testfiles directory on my Google Drive account.

Really very simple.  The only thing that really needs mentioning is this part of the code:

             // We can either use the fully qualified folder path or the ID of the
             //  folder we want to upload to.  The latter is much faster.
             //rc = #g4gdrv_setValue('upload_to_folder':gFolder);
             rc = #g4gdrv_setValue('in_parent_id':gFolderID);

When using G4G to upload a file there are many ways to set the values.  For example here we have two options to set the folder we want to upload with.  The first way is setting the value upload_to_folder with the fully qualified path of the folder.  The second is using the actual folder ID.  Everything in Google Drive has a unique ID.  If we use this ID instead of the actual folder name then G4G will run faster since it doesn't have to look up the ID of the folder itself.

Getting the ID of the folder was as simple as opening Google Drive in my browser and going to that specific folder.  As shown in the following image, the ID is the last part of the URL:

I also put together a video that shows the program running (I hoe you like Jazz!  I forgot to turn off my music and Dream Theater was in the background so I had to remove that for Copyright purposes and added some music offered by YouTube.)

In closing, we have a lot of power at our fingertips with sometimes underused system APIs and functions.  Even today in dealing with customers the IFS is very underused.  So hopefully this will help some with everyday processing of files, and also take advantage of the Cloud by using Google Drive.

 

 


Last edited 06/07/2017 at 09:14:02



Latest Posts:

BVSTools is Now Running V7R4M0 BVSTools is Now Running V7R4M0
Posted by September 28, 2019
BVSTools >> BVSTools Announcements
GreenTools for G Suite (G4G) Now Includes Send Mail Functionality (G4GSMAIL Addon) GreenTools for G Suite (G4G) Now Includes Send Mail Functionality (G4GSMAIL Addon)
Posted by August 27, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
GreenTools For G Suite (G4G) v12.00 Released With Base OAuth 2.0 Functionality GreenTools For G Suite (G4G) v12.00 Released With Base OAuth 2.0 Functionality
Posted by July 28, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
BVSTools Small Price Increase in 2020 BVSTools Small Price Increase in 2020
Posted by July 26, 2019
BVSTools >> BVSTools Announcements
GreenTools for Vertex Cloud (VTXCLOUD) Now Available GreenTools for Vertex Cloud (VTXCLOUD) Now Available
Posted by July 22, 2019
BVSTools >> BVSTools Announcements >> GreenTools for Vertex Cloud (VTXCLOUD) Specific Announcements
GreenTools for Google Apps (G4G) - Drive Addon Successfully Verified by Google GreenTools for Google Apps (G4G) - Drive Addon Successfully Verified by Google
Posted by July 22, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
Why I Cancelled my DynDNS Service and How I Replaced It with an IBM i Application Why I Cancelled my DynDNS Service and How I Replaced It with an IBM i Application
Posted by July 17, 2019
IBM Power Systems >> (QGPL) IBM i
Green Tools for G Suite (G4G) Product Updates (Licensing, Functionality, Base Product) Green Tools for G Suite (G4G) Product Updates (Licensing, Functionality, Base Product)
Posted by July 13, 2019
BVSTools >> BVSTools Announcements >> GreenTools for G Suite (Google Apps) (G4G) Specific Announcements
Reading JSON Data from Standard Input With YAJL and RPG Reading JSON Data from Standard Input With YAJL and RPG
Posted by July 12, 2019
Programming >> Proof of Concept (POC)
MAILTOOL Updated to Allow Use of IBM Global Security Kit (GSKIT) for SSL/TLS Communications MAILTOOL Updated to Allow Use of IBM Global Security Kit (GSKIT) for SSL/TLS Communications
Posted by June 19, 2019
BVSTools >> BVSTools Announcements >> eMail Tool (MAILTOOL) Specific Announcements
GETURI v10.00 Released Supporting IBM Global Security Kit (GSKIT) and Server Name Indication (SNI) GETURI v10.00 Released Supporting IBM Global Security Kit (GSKIT) and Server Name Indication (SNI)
Posted by June 11, 2019
BVSTools >> BVSTools Announcements >> Get URI (GETURI) Specific Announcements
BVSTools Now Offers Vertex Cloud Interface BVSTools Now Offers Vertex Cloud Interface
Posted by April 15, 2019
BVSTools >> BVSTools Announcements
Token Has an Invalid Signature Error for Office 365 Email Token Has an Invalid Signature Error for Office 365 Email
Posted by March 22, 2019
BVSTools >> BVSTools Software Discussion >> GreenTools for Microsoft Apps (G4MS) Specific Discussion
Resending Emails that have Errored Out with Updated Router or Authentication Information Resending Emails that have Errored Out with Updated Router or Authentication Information
Posted by March 1, 2019
BVSTools >> BVSTools Software Discussion >> Email Tools (MAILTOOL) Specific Discussion
BVSTools Offers Toolset to Work With HubSpot OAuth 2.0 APIs On Your IBM i BVSTools Offers Toolset to Work With HubSpot OAuth 2.0 APIs On Your IBM i
Posted by January 27, 2019
BVSTools >> BVSTools Announcements

Reply




Copyright 1983-2019 BVSTools
GreenBoard(v3) Powered by the eRPG SDK, MAILTOOL Plus!, GreenTools for Google Apps, jQuery, jQuery UI, BlockUI, CKEditor and running on the IBM i (AKA AS/400, iSeries, System i).