Logging On to an FTP Server

The first step is to log on to the FTP server ftp.microsoft.com. In general, there are two types of FTP servers: public and private ones. For both types, you must provide a user name and password. In public FTP servers, such as ftp.microsoft.com, you typically use "anonymous" as your user name, and your email address as the password. The Internet Transfer control makes it easy, because it supplies this information automatically (getting your email address from the Windows registry).

To log on to a private FTP server, you must provide a password and user name, and you do that using the Internet Transfer control's UserName and Password properties. For example, if we wanted to connect to ftp.private.com, we would start by setting the Internet Transfer control's URL property to ftp.private.com. (You don't have to set the URL property if you use the OpenURL() method, because you pass the URL when calling OpenURL().)

Inet1.URL = "ftp://ftp.private.com"
           .
           .
           .

Next, set the UserName and Password properties:

Inet1.URL = "ftp://ftp.private.com"
  —>   Inet1.UserName = "George Washington"
  —>   Inet1.Password = "no_lies"
           .
           .
           .

Then you can the Execute method to execute FTP commands such as GET; in this case, we'll download a file named source.txt from the FTP server and save it as c:\target.txt:

Inet1.URL = "ftp://ftp.private.com"
        Inet1.UserName = "George Washington"
        Inet1.Password = "no_lies"
        Inet1.Execute , "GET source.txt C:\target.txt"
                .
                .
                .

In this way, you use the Execute method to execute FTP commands (just as you might in a command-line FTP program). Finally, we execute a CLOSE command to close the connection:

Inet1.URL = "ftp://ftp.private.com"
        Inet1.UserName = "George Washington"
        Inet1.Password = "no_lies"
        Inet1.Execute , "GET source.txt C:\target.txt"
  —>   Inet.Execute ,"CLOSE"

That's the way to execute FTP commands: with the Execute method. You may be familiar with these commands. They are the backbone of FTP handling, although, as we'll see in a minute, you don't have to use any of these commands to download if you use the OpenURL() method. Table 3.1 shows the supported FTP commands, such as CD, GET, DELETE, PUT, and MKDIR (MKDIR creates a new directory on the Internet host if you have the privilege to do so), and describes what they do. Using these commands, we can download files from and upload files to FTP sites. For example, to change directories and upload a file named source.txt, renaming it to target.txt, you would execute this code:

Inet1.URL = "ftp://ftp.private.com"
        Inet1.UserName = "George Washington"
        Inet1.Password = "no_lies"
        Inet1.Execute , "CD gifs\thegifs"
        Inet1.Execute , "PUT C:\source.txt target.txt"
        Inet.Execute ,"CLOSE"

Table 3.1 FTP commands of the Internet Transfer Control's Execute Method

Command Does This
CD newdir Changes directory (e.g., Execute , "CD gifs\thegifs").
CDUP Changes to parent directory. Identical to "CD ..".
DELETE file Deletes specified file (e.g., Execute , "DELETE nogood.dat").
DIR [newdir] Searches current directory or directory specified. Use GetChunk() to get the data (e.g., Execute , "DIR /gifs").
GET source target Gets file "source" and creates a local file "target" (e.g., Execute , "GET source.txt C:\target.txt").
LS List. Same as DIR.
MKDIR newdir Creates new directory newdir (e.g., Execute , "MKDIR /newdir").
PUT source target Copies local file "source" to remote computer as "target" (e.g., Execute , "PUT C:\source.txt target.txt").
PWD Print Working Directory. Use GetChunk() method to get the data (e.g., Execute , "PWD").
QUIT Close the connection (e.g., Execute , "QUIT").
RECV source target Same as GET.
RENAME oldname Renames a file (e.g., Execute , "RENAME oldnewname name.txt newname.txt").
RMDIR dirname Removes directory (e.g., Execute , "RMDIR dirname").
SEND source Same as PUT.
SIZE filename Gets the size of specified file (e.g., Execute "SIZE filename.txt").

© 1997 by Steven Holzner. All rights reserved.