ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

All the talk about FTP...

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

  • All the talk about FTP...

    It had been a while but since all the new chatter about FTP..

    I thought I'd play around a bit.

    here is some code -- it connects to a 2003 server on my network and lists all
    files in the default directory. (the default directory is setup in IIS on server)

    so... I guess a cheap mans version of verifying that a file has completed on the remote
    server could be...

    Ftp the file with a wacked extension myfile.building
    then last step of FTP would be rename the file to myfile.txt

    then use this program (below) to read the remote directory looking for completed .txt files.
    Code:
                         [FONT=courier new]     * PROGRAM - ##FTP                                                                             [/FONT]
    [FONT=courier new]      * PURPOSE -                                                                                   [/FONT]
    [FONT=courier new]      * WRITTEN -                                                                                   [/FONT]
    [FONT=courier new]      * AUTHOR  -                                                                                   [/FONT]
    [FONT=courier new]      *                                                                                             [/FONT]
    [FONT=courier new]      *=======================================================================                      [/FONT]
    [FONT=courier new]     d cmdstring       s           2000                                                             [/FONT]
    [FONT=courier new]     d cmdlength       s             15  5                                                          [/FONT]
    [FONT=courier new]     d ftpdata         s            100                                                             [/FONT]
    [FONT=courier new]     d Password        s             30    inz('Someone@code400.com')                               [/FONT]
    [FONT=courier new]     d Q               s              1    inz('''')                                                [/FONT]
    [FONT=courier new]     d remoteIP        s             15    inz('10.0.1.51')                                         [/FONT]
    [FONT=courier new]     d sqlstmt         s          23000    varying                                                  [/FONT]
    [FONT=courier new]     d Userid          s             10    inz('Anonymous')                                         [/FONT]
    
    [FONT=courier new]     d openList        pr                                                                           [/FONT]
    [FONT=courier new]     d FetchNext       pr              n                                                            [/FONT]
    [FONT=courier new]     d closeList       pr                                                                           [/FONT]
    
    [FONT=courier new]      // external calls                                                                             [/FONT]
    [FONT=courier new]     d $command        pr                  extpgm( 'QCMDEXC' )                                      [/FONT]
    [FONT=courier new]     d   cmdstring                 2000    options( *varsize ) const                                [/FONT]
    [FONT=courier new]     d   cmdlength                   15  5                     const                                [/FONT]
    
    [FONT=courier new]      /free                                                                                         [/FONT]
    
    [FONT=courier new]         exsr $ftplist;                                                                             [/FONT]
    [FONT=courier new]         exsr $readList;                                                                            [/FONT]
    
    [FONT=courier new]         *inlr = *on;                                                                               [/FONT]
    
    [FONT=courier new]        //-------------------------------------------                                               [/FONT]
    [FONT=courier new]        // $ftpList - List the directory contents to                                                [/FONT]
    [FONT=courier new]        //            a table DIROUTPUT.                                                            [/FONT]
    [FONT=courier new]        //                                                                                          [/FONT]
    [FONT=courier new]        // if it doesnt exist system creates if there                                               [/FONT]
    [FONT=courier new]        // then member is *Replaced..                                                               [/FONT]
    [FONT=courier new]        //-------------------------------------------                                               [/FONT]
    [FONT=courier new]             begsr $ftpList;                                                                        [/FONT]
    
    [FONT=courier new]             //prepare the FTP                                                                      [/FONT]
    
    [FONT=courier new]             //in case this is a repeat delete the overrides and                                    [/FONT]
    [FONT=courier new]             //the input output file(s)                                                             [/FONT]
    
    [FONT=courier new]             cmdstring = 'DLTOVR INPUT';                                                            [/FONT]
    [FONT=courier new]             cmdlength = %len(%trim(cmdstring));                                                    [/FONT]
    [FONT=courier new]             monitor;                                                                               [/FONT]
    [FONT=courier new]             $command(cmdstring : cmdlength);                                                       [/FONT]
    [FONT=courier new]             on-error;                                                                              [/FONT]
    [FONT=courier new]             endmon;                                                                                [/FONT]
    
    [FONT=courier new]             cmdstring = 'DLTOVR OUTPUT';                                                           [/FONT]
    [FONT=courier new]             cmdlength = %len(%trim(cmdstring));                                                    [/FONT]
    [FONT=courier new]             monitor;                                                                               [/FONT]
    [FONT=courier new]             $command(cmdstring : cmdlength);                                                       [/FONT]
    [FONT=courier new]             on-error;                                                                              [/FONT]
    [FONT=courier new]             endmon;                                                                                [/FONT]
    
    [FONT=courier new]             cmdstring = 'DLTF FILE(QTEMP/INPUT)';                                                  [/FONT]
    [FONT=courier new]             cmdlength = %len(%trim(cmdstring));                                                    [/FONT]
    [FONT=courier new]             monitor;                                                                               [/FONT]
    [FONT=courier new]             $command(cmdstring : cmdlength);                                                       [/FONT]
    [FONT=courier new]             on-error;                                                                              [/FONT]
    [FONT=courier new]             endmon;                                                                                [/FONT]
    
    [FONT=courier new]             cmdstring = 'DLTF FILE(QTEMP/OUTPUT)';                                                 [/FONT]
    [FONT=courier new]             cmdlength = %len(%trim(cmdstring));                                                    [/FONT]
    [FONT=courier new]             monitor;                                                                               [/FONT]
    [FONT=courier new]             $command(cmdstring : cmdlength);                                                       [/FONT]
    [FONT=courier new]             on-error;                                                                              [/FONT]
    [FONT=courier new]             endmon;                                                                                [/FONT]
    
    [FONT=courier new]             cmdstring = 'CRTPF FILE(QTEMP/INPUT) ' +                                               [/FONT]
    [FONT=courier new]                          ' RCDLEN(256)';                                                           [/FONT]
    [FONT=courier new]             cmdlength = %len(%trim(cmdstring));                                                    [/FONT]
    [FONT=courier new]             monitor;                                                                               [/FONT]
    [FONT=courier new]             $command(cmdstring : cmdlength);                                                       [/FONT]
    [FONT=courier new]             on-error;                                                                              [/FONT]
    [FONT=courier new]             endmon;                                                                                [/FONT]
    
    [FONT=courier new]             cmdstring = 'CRTPF FILE(QTEMP/OUTPUT) ' +                                              [/FONT]
    [FONT=courier new]                          ' RCDLEN(256)';                                                           [/FONT]
    [FONT=courier new]             cmdlength = %len(%trim(cmdstring));                                                    [/FONT]
    [FONT=courier new]             monitor;                                                                               [/FONT]
    [FONT=courier new]             $command(cmdstring : cmdlength);                                                       [/FONT]
    [FONT=courier new]             on-error;                                                                              [/FONT]
    [FONT=courier new]             endmon;                                                                                [/FONT]
    
    [FONT=courier new]              // populate the input file                                                            [/FONT]
    
    
    [FONT=courier new]         Exec SQL   INSERT INTO INPUT                                                               [/FONT]
    [FONT=courier new]                    values('Anonymous  [EMAIL="Someone@code400.dom"]Someone@code400.dom[/EMAIL]');                                       [/FONT]
    
    [FONT=courier new]         Exec SQL   INSERT INTO INPUT                                                               [/FONT]
    [FONT=courier new]                    values('dir (DISK ');                                                           [/FONT]
    
    
    [FONT=courier new]         Exec SQL   INSERT INTO INPUT                                                               [/FONT]
    [FONT=courier new]                    values('quit');                                                                 [/FONT]
    
    [FONT=courier new]             cmdstring = 'OVRDBF FILE(INPUT) TOFILE(QTEMP/INPUT)' +                                 [/FONT]
    [FONT=courier new]                         ' OVRSCOPE(*JOB) ';                                                        [/FONT]
    [FONT=courier new]             cmdlength = %len(%trim(cmdstring));                                                    [/FONT]
    [FONT=courier new]             monitor;                                                                               [/FONT]
    [FONT=courier new]             $command(cmdstring : cmdlength);                                                       [/FONT]
    [FONT=courier new]             on-error;                                                                              [/FONT]
    [FONT=courier new]             endmon;                                                                                [/FONT]
    
    [FONT=courier new]             cmdstring = 'OVRDBF FILE(OUTPUT) TOFILE(QTEMP/OUTPUT)' +                               [/FONT]
    [FONT=courier new]                         ' OVRSCOPE(*JOB) ';                                                        [/FONT]
    [FONT=courier new]             cmdlength = %len(%trim(cmdstring));                                                    [/FONT]
    [FONT=courier new]             monitor;                                                                               [/FONT]
    [FONT=courier new]             $command(cmdstring : cmdlength);                                                       [/FONT]
    [FONT=courier new]             on-error;                                                                              [/FONT]
    [FONT=courier new]             endmon;                                                                                [/FONT]
    
    [FONT=courier new]             cmdstring = 'STRTCPFTP ' + Q + %trim(remoteIp)  + Q  ;                                 [/FONT]
    [FONT=courier new]             cmdlength = %len(%trim(cmdstring));                                                    [/FONT]
    [FONT=courier new]             monitor;                                                                               [/FONT]
    [FONT=courier new]             $command(cmdstring : cmdlength);                                                       [/FONT]
    [FONT=courier new]             on-error;                                                                              [/FONT]
    [FONT=courier new]             endmon;                                                                                [/FONT]
    
    [FONT=courier new]             endsr;                                                                                 [/FONT]
    
    [FONT=courier new]        //-------------------------------------------                                               [/FONT]
    [FONT=courier new]        // $readList - read the list created                                                        [/FONT]
    [FONT=courier new]        //-------------------------------------------                                               [/FONT]
    
    [FONT=courier new]             begsr $readList;                                                                       [/FONT]
    
    [FONT=courier new]              sqlstmt = 'select * from DIROUTPUT ';                                                 [/FONT]
    [FONT=courier new]              openList();                                                                           [/FONT]
    [FONT=courier new]              dow fetchNext();                                                                      [/FONT]
    
    [FONT=courier new]             //   file date              1      8                                                   [/FONT]
    [FONT=courier new]             //   file time             11     17                                                   [/FONT]
    [FONT=courier new]             //   date&time              1     17                                                   [/FONT]
    [FONT=courier new]             //   isdirectory           25     29                                                   [/FONT]
    [FONT=courier new]             //   file size             29     38                                                   [/FONT]
    [FONT=courier new]             //   file name             40     89                                                   [/FONT]
    [FONT=courier new]             //   .....                 49     98                                                   [/FONT]
    
    
    
    [FONT=courier new]               dsply %subst(ftpdata:40:25) ' ';                                                     [/FONT]
    
    [FONT=courier new]              enddo;                                                                                [/FONT]
    [FONT=courier new]              closeList();                                                                          [/FONT]
    
    
    [FONT=courier new]             endsr;                                                                                 [/FONT]
    
    [FONT=courier new]        //-------------------------------------------                                               [/FONT]
    [FONT=courier new]      /end-free                                                                                     [/FONT]
    
    [FONT=courier new]      *--------------------------------------------------------                                     [/FONT]
    [FONT=courier new]      *  openList  - Open a cursor to read file                                                     [/FONT]
    [FONT=courier new]      *--------------------------------------------------------                                     [/FONT]
    [FONT=courier new]     p openList        b                                                                            [/FONT]
    
    [FONT=courier new]     d openList        pi                                                                           [/FONT]
    
    [FONT=courier new]      /Free                                                                                         [/FONT]
    
    [FONT=courier new]       exec sql                                                                                     [/FONT]
    [FONT=courier new]        declare MyCursor cursor for statement;                                                      [/FONT]
    
    [FONT=courier new]       exec sql                                                                                     [/FONT]
    [FONT=courier new]        prepare statement from :sqlstmt;                                                            [/FONT]
    
    [FONT=courier new]       exec sql                                                                                     [/FONT]
    [FONT=courier new]        open mycursor;                                                                              [/FONT]
    
    [FONT=courier new]      /End-Free                                                                                     [/FONT]
    
    [FONT=courier new]     p openList        e                                                                            [/FONT]
    [FONT=courier new]      *--------------------------------------------------------                                     [/FONT]
    [FONT=courier new]      *  fetchNext  - read one record at a time                                                     [/FONT]
    [FONT=courier new]      *--------------------------------------------------------                                     [/FONT]
    [FONT=courier new]     p fetchNext       b                                                                            [/FONT]
    
    [FONT=courier new]     d fetchNext       pi              n                                                            [/FONT]
    
    [FONT=courier new]      /free                                                                                         [/FONT]
    
    [FONT=courier new]       exec sql                                                                                     [/FONT]
    [FONT=courier new]        fetch next from mycursor into : ftpdata;                                                    [/FONT]
    [FONT=courier new]         if sqlstt < '02000';                                                                       [/FONT]
    [FONT=courier new]           return *on;                                                                              [/FONT]
    [FONT=courier new]         else;                                                                                      [/FONT]
    [FONT=courier new]           return *off;                                                                             [/FONT]
    [FONT=courier new]         endif;                                                                                     [/FONT]
    
    [FONT=courier new]      /end-free                                                                                     [/FONT]
    
    [FONT=courier new]     p fetchNext       e                                                                            [/FONT]
    [FONT=courier new]      *--------------------------------------------------------                                     [/FONT]
    [FONT=courier new]      *  closeOrderList  - Close the OrderHdr cursor                                                [/FONT]
    [FONT=courier new]      *--------------------------------------------------------                                     [/FONT]
    [FONT=courier new]     p closeList       b                                                                            [/FONT]
    
    [FONT=courier new]     d closeList       pi                                                                           [/FONT]
    
    [FONT=courier new]      /free                                                                                         [/FONT]
    
    [FONT=courier new]       exec sql                                                                                     [/FONT]
    [FONT=courier new]        close MyCursor;                                                                             [/FONT]
    
    [FONT=courier new]      /end-free                                                                                     [/FONT]
    
    [FONT=courier new]     p closeList       e 
                                                                               
    [/FONT]

    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
Working...
X