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

  1. #/usr/local/bin/kermit +
  2. ;
  3. ; f t p d i r e c t o r y
  4. ;
  5. ; Gets a directory listing from an FTP server
  6. ; that shows the size and full modification date and time of each file.
  7. ; The date and time are in UTC (GMT).
  8. ;
  9. ; Illustrates: how to get remote file date/time and size from server.
  10. ;   How to get a filename list from the server.
  11. ;   How to get file date/time and size info from the server.
  12. ;   How to read a local file.
  13. ;
  14. ; This example merely prints the file information but you could also
  15. ; use it to make decisions about whether to fetch each file, etc.
  16. ;
  17. ; Arguments:
  18. ;  \%1 hostname (empty if you already have an FTP connection)
  19. ;  \%2 username on host (empty for anonymous)
  20. ;  \%3 directory on host (empty if you are already CD'd as desired)
  21. ;  \%4 file specification (default "*")
  22. ;
  23. ; F. da Cruz, Columbia University, February 2003
  24. ;
  25. ; Modified Aug 2004 to show how to identify server directories.
  26.  
  27. set quiet on                               ; No messages
  28. set exit warning off                       ; No warnings
  29. .listname := \v(tmpdir)ftplist.\v(pid)     ; Temp file name
  30. if defined \%1 {                           ; Open FTP connection
  31.     if defined \%2 {                       ; Log in as real user
  32.         ftp open \%1 /user:\%2
  33.     } else {                               ; or anonymously
  34.         ftp open \%1 /anonymous
  35.     }
  36.     if fail exit 1                         ; Check FTP OPEN
  37. } else if not \v(ftp_connected) {          ; Check connection
  38.     exit 1 "No FTP connection"
  39. }
  40. if not \v(ftp_loggedin) {                  ; Check if logged in
  41.     exit 1 "Not logged in to FTP server"
  42. }
  43. if defined \%3 {                           ; CD to disired directory
  44.     ftp cd \%3
  45.     if fail exit 1                         ; Check
  46. }
  47. if not def \%4 def \%4 *                   ; Default filespec if not given
  48.  
  49. ftp mget /namelist:\m(listname) \%4        ; Get list of names into file
  50. if fail exit 1                             ; Check
  51.  
  52. fopen /read \%c \m(listname)               ; Open name-list file
  53. if fail exit 1                             ; Check
  54. while true {                               ; Loop to read each line
  55.     fread /line \%c name                   ; Read a name
  56.     if fail break                          ; Check for EOF
  57.     ftp quote MDTM \m(name)                        ; Get file's modtime
  58.     if fail exit 1 "Server does not support MDTM"  ; Check
  59.     .time := \v(ftp_message)                       ; Save
  60.     ftp size \m(name)                              ; Get file's size
  61.     if fail exit 1 "Server does not support SIZE"  ; Check
  62.     .size := \v(ftp_message)                       ; Save
  63.  
  64. ; The next bit checks to see if the file is a directory.
  65. ; This works only if the server returns directory names in its NLST response.
  66. ; (Most servers do not.)
  67.  
  68.     undef dir                              ; Assume it's not a directory
  69.     ftp cd \m(name)                        ; Try to CD to it
  70.     if success {                           ; If it works...
  71.         .dir = "(DIRECTORY)"               ; it's a directory
  72.         ftp cdup                           ; Go back up.
  73.     }
  74.     echo "\flpad(\m(size),12) \fcvtdate(\m(time)) \m(name) \m(dir)" ; Show info
  75. }
  76. fclose \%c                                 ; Close name-list file
  77. delete \m(listname)                        ; Delete name-list file
  78. if def \%1 exit 0                          ; BYE to server and exit Kermit
  79. end 0
  80.