home *** CD-ROM | disk | FTP | other *** search
/ business-86-101-185-173.business.broadband.hu / business-86-101-185-173.business.broadband.hu.zip / business-86-101-185-173.business.broadband.hu / ftpclass.zip / ftp_test.PRG next >
Text File  |  1999-05-11  |  5KB  |  148 lines

  1. ********************************************************
  2. *                                                      *            
  3. *  Program: FTP_Test                                   *
  4. *  Written By: Robert Abram                            *
  5. *  Date: 9/98                                          *
  6. *                                                      *
  7. *  Reason: Because Programmers need all the help       *
  8. *          they can get.                               *
  9. *                                                      *
  10. ********************************************************
  11.  
  12.  * Test FTP Services
  13.  * This is just a quick demonstration of the most used functions
  14.  *    What you need is a ftp site to connect to, a file you can send up and 
  15.  *    rights on the ftp server to create, read, write and delete files
  16.  
  17.  * **** Replace all the Function Parameters enclosed in #PARM# with valid information
  18.  
  19.  * Note: In this Demo, all output is to the Foxpro main window
  20.  
  21.   PUBLIC sz_ftp
  22.  
  23.    * Make it so Foxpro can find our FTP Class
  24.     SET PROCEDURE TO ftp.prg ADDITIVE
  25.    
  26.    * Create a FTP Object
  27.     sz_ftp = CREATEOBJECT('ftp_service')
  28.  
  29.    * Try to get a handle into the Internet and Connect Briefly with the FTP Site
  30.    * Note: Insert your USER ID, PASSWORD, FTP ADDRESS, PORT # Here  
  31.    * Note: ("21" is the Default Port)
  32.    
  33.     IF sz_ftp.OpenInternet(#username#, #password#, #URL#, "21") 
  34.     
  35.         MessageBox("Successful Connection", 64, "FTP Message")
  36.     
  37.        * Display the Inital Directory
  38.         ? "--- Inital Directory"
  39.         ListDir()
  40.         
  41.        * Create a Directory called "Dir_Test" on the FTP Server
  42.         ? "--- Creating Directory (Dir_Test)"
  43.         IF !sz_ftp.CreateFtpDirectory('Dir_Test')
  44.             ?sz_ftp.GetErrorCode(.T.)
  45.         ENDIF
  46.         ListDir()
  47.         
  48.        * Change to that directory
  49.         ? "--- Changing Directory to Dir_Test"
  50.         IF !sz_ftp.ChangeFtpDirectory("Dir_Test")
  51.             ?sz_ftp.GetErrorCode(.T.)
  52.         ENDIF
  53.         ListDir()
  54.     
  55.        * Copy up a file to the server
  56.        * NOTE: If you get Error #2 Cant Find File.  It couldn't find this 
  57.        *       TESTFILE.TXT file.
  58.         ? "--- Sending File to FTP Server"        
  59.         IF !sz_ftp.PutFtpFile("testfile.txt", "testfile.txt")
  60.             ?sz_ftp.GetErrorCode(.T.)
  61.         ENDIF
  62.         ListDir()
  63.         
  64.         ? "--- Receiving File to FTP Server"
  65.         IF !sz_ftp.GetFtpFile("testfile.txt", "testfile.new", .T.)
  66.             ?sz_ftp.GetErrorCode(.T.)
  67.         ENDIF
  68.         ListDir()
  69.  
  70.        * Delete the Files 
  71.         ? "--- Removing File Dir.txt to FTP Server"        
  72.         IF !sz_ftp.DeleteFtpFile("testfile.txt")
  73.             ?sz_ftp.GetErrorCode(.T.)
  74.         ENDIF
  75.         ListDir()
  76.  
  77.        * Change back the Original Directory
  78.         ? "--- Changing Directory back on Level"
  79.         IF !sz_ftp.ChangeFtpDirectory("..")
  80.             ?sz_ftp.GetErrorCode(.T.)
  81.         ENDIF
  82.         ListDir()
  83.  
  84.        * Remove the Directory From the Server
  85.         ? "--- Removing Directory Dir_Test"
  86.         IF !sz_ftp.RemoveFtpDirectory("Dir_Test")
  87.             ?sz_ftp.GetErrorCode(.T.)
  88.         ENDIF
  89.         ListDir()
  90.                 
  91.         sz_ftp.CloseInternet()
  92.                 
  93.       ELSE
  94.           MessageBox("No Connection Made", 64, "FTP Message")
  95.           sz_ftp.CloseInternet()
  96.           ?sz_ftp.GetErrorCode(.T.)
  97.           
  98.      ENDIF
  99.  
  100.    RETURN
  101.  
  102.   ***************************************************
  103.     PROCEDURE ListDir
  104.  
  105.         
  106.         LOCAL lcOutput, lcDirName, laDirArray
  107.  
  108.       * Reset the variable to hold the file array.  
  109.       * The Object redefines the variable to an array.
  110.         laDirArray = ""
  111.         
  112.       * Get the Current Directory Name
  113.         IF !sz_ftp.GetFtpDirectory(@lcDirName)
  114.             sz_ftp.GetErrorCode(.T.)
  115.             lcDirName = ''
  116.         ENDIF
  117.         
  118.       * Get the Files in the Current Directory
  119.       * This one always returns an Error.  When the API Function that gets the files
  120.       * can't find any more files, it return the Error 'ERROR_NO_MORE_FILES' 
  121.         IF !sz_ftp.GetFtpDirectoryArray(@laDirArray, "*.*") 
  122.            * Error 18 always returns if the directory is empty
  123.             IF !EMPTY(laDirArray) OR sz_ftp.GetErrorCode(.F.) != 18
  124.                 sz_ftp.GetErrorCode(.T.)
  125.             ENDIF
  126.         ENDIF
  127.         
  128.        * Output the Current Directory 
  129.         ? 
  130.         ? 'FTP Directory: ' + lcDirName 
  131.         
  132.        * Interate through our array of files
  133.         IF !EMPTY(laDirArray)  && If Empty, there were no files found in the current Directory.
  134.             FOR x = 1 TO ALEN(laDirArray, 1)
  135.                
  136.               * Pad up a String with some file information.
  137.                lcOutput = '   ' + PADR(laDirArray[x, 1], 25) + PADL(STR(laDirArray[x, 3]), 12) + ;
  138.                                 ' ' + PADR(DTOC(laDirArray[x, 6]), 14) + laDirArray[x, 7]
  139.               * Output the File Information
  140.                ? lcOutput
  141.                 
  142.             ENDFOR
  143.         ENDIF
  144.     
  145.       * Just move a line down.
  146.         ?
  147.                 
  148.     RETURN