home *** CD-ROM | disk | FTP | other *** search
/ Columbia Kermit / kermit.zip / ckscripts / usend < prev    next >
Text File  |  2020-01-01  |  3KB  |  101 lines

  1. ; Macros to use instead of PUT /UNIQUE, which is not reliable
  2. ; due to differing or missing server implementations of STOU,
  3. ; plus the the fact that there is no consistent or reliable
  4. ; way of finding out the unique name chosen by STOU even when
  5. ; it works.
  6. ;
  7. ; These macros assume an FTP connection is already open
  8. ; and the server is CD'd to the desired directory.  They can
  9. ; not be guaranteed to work if two or more people are using
  10. ; them at the same time for the same filename in the same
  11. ; directory of the same server.
  12. ;
  13. ; Requires: C-Kermit 8.0 or later or K95 2.0 or later
  14. ;
  15. ; Author: F. da Cruz, Columbia University, Oct 2002
  16.  
  17. local nameused
  18.  
  19. ; Macro USEND -- sends the given file to an FTP server,
  20. ; guaranteeing that it will not overwrite an existing file
  21. ; sending the file under a name that is unique to the server.
  22. ; Call with: name of single file to send.
  23. ; Returns: 0 on success, 1 on error or any kind of failure.
  24. ; On success, sets \m(nameused) to the name that was used.
  25. ;
  26. define USEND {
  27.     undef nameused
  28.     assign filename \fcontents(\%1)
  29.     if not def filename end 1
  30.     if not \v(ftp_connected) end 1
  31.     ftp check \m(filename)
  32.     if success {
  33.     for \%i 1 100 1 {
  34.         ftp check \m(filename).\%i
  35.         if fail {
  36.                 assign namused \m(filename).\%i
  37.                 ftp put /as-name:\m(nameused) \m(filename)
  38.                 end \v(status)                
  39.             }
  40.     }
  41.     end 1 ERROR: No unique name found
  42.     } else {
  43.     ftp put \m(filename)
  44.         end \v(status)
  45.     }
  46. }
  47.  
  48. ; Macro USENDR - like USEND except instead of sending the local file
  49. ; under a unique name, it renames the existing file on the server to a
  50. ; a unique name and then sends the local file under its own name.
  51. ; On success, sets \m(nameused) to the name that was used, which is
  52. ; the same as the original name.
  53. ;
  54. define USENDR {
  55.     undef nameused
  56.     assign filename \fcontents(\%1)
  57.     if not def filename end 1
  58.     if not \v(ftp_connected) end 1
  59.     ftp check \m(filename)
  60.     if success {
  61.     for \%i 1 100 1 {
  62.         ftp check \m(filename).\%i
  63.         if fail {
  64.         ftp rename \m(filename) \m(filename).\%i
  65.         if fail end 1
  66.                 assign namused \m(filename)
  67.         ftp put \m(filename)                
  68.         end \v(status)
  69.         }
  70.     }
  71.     end 1 ERROR: No unique name found
  72.     } else {
  73.     ftp put \m(filename)
  74.     end \v(status)
  75.     }
  76. }
  77. end 0 ; Remove this statement to proceed.
  78.  
  79. ; Sample of use...
  80.  
  81. set ftp debug on                           ; Watch what happens
  82. ftp open somehost.xyzcorp.com /user:olga   ; Open FTP connection and log in
  83. if fail stop 1                             ; Check if open
  84. if not \v(ftp_loggedin) stop 1             ; Check if logged in
  85. set xfer display brief                     ; Brief (one-line) file xfer display
  86. ftp cd tmp                                 ; CD to desired server directory
  87. if fail stop 1 CD failed                   ; Check
  88.  
  89. ; Send the same file several times (substitute USENDR if preferred)...
  90.  
  91. define filetosend foo                      ; Name of an existing file
  92.  
  93. for \%i 1 10 1 {                           ; Send it 10 times
  94.     usend \m(filetosend)
  95.     if fail stop 1 USEND failed.
  96.     echo SENT \m(filetosend) AS \m(nameused) OK.
  97. }
  98. ftp directory \m(filetosend)*              ; See results on server
  99.  
  100. ftp bye
  101.