The earlier listing of the ftp.microsoft.com directory shows that one of the files is a binary file named LS-LR.zip. Let's download that file now. (Note that when you read this, the contents of the microsoft FTP directory may have changed.) Open the Menu Editor in the Visual Basic Tools menu and add a new item, Get File, to FTPer's File menu following the Close item, as shown in Figure 3.2. Give this item the name Getfile.
Figure 3.2 The FTPer Get File menu item.
Now double-click that item to open its click event:
Private Sub Getfile_Click()
End Sub
Our task is to download the file ftp://ftp.microsoft.com/LS-LR.zip. We can use OpenURL() to get this file, but we can't just assign the output of OpenURL() to a string, because LS-LR.zip is not a text file; instead, we set up a binary array:
Private Sub Getfile_Click()
—> Dim binarydata() As Byte
.
.
.
End Sub
Now we are free to get the file we want using OpenURL(), passing it the icByteArray parameter to indicate that we are downloading a binary file:
Private Sub Getfile_Click()
Dim binarydata() As Byte
—> binarydata() = Inet1.OpenURL("ftp://ftp.microsoft.com/LS-LR.zip",
icByteArray)
.
.
.
End Sub
Now we store this binary data to disk by opening LS-LR.zip and writing the binarydata() array to it:
Private Sub Getfile_Click()
Dim binarydata() As Byte
binarydata() = Inet1.OpenURL("ftp://ftp.microsoft.com/LS-LR.zip",
icByteArray)
—> Open "E:\vb\LS-LR.zip" For Binary Access Write As #1
—> Put #1, , binarydata()
.
.
.
End Sub
Finally, we close the file and pop a message box on the screen to indicate we are finished:
Private Sub Getfile_Click()
Dim binarydata() As Byte
binarydata() = Inet1.OpenURL("ftp://ftp.microsoft.com/LS-LR.zip",
icByteArray)
Open "E:\vb\LS-LR.zip" For Binary Access Write As #1
Put #1, , binarydata()
—> Close #1
—> MsgBox "Finished."
End Sub
And that's it—now we're able to download FTP files, both text and binary, as well as work with the standard FTP commands such as MKDIR, GET, PUT, and CD. The code for our FTPer program appears in Listing 3.1.
Listing 3.1 (FTPer) FrmMain.frm
VERSION 5.00
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.1#0"; "COMDLG32.OCX"
Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#1.1#0"; "COMCTL32.OCX"
Object = "{48E59290-9880-11CF-9754-00AA00C00908}#1.0#0"; "MSINET.OCX"
Object = "{3B7C8863-D78F-101B-B9B5-04021C009402}#1.1#0"; "RICHTX32.OCX"
Begin VB.Form frmMain
Caption = "FTPer"
ClientHeight = 4890
ClientLeft = 165
ClientTop = 735
ClientWidth = 4680
LinkTopic = "Form1"
ScaleHeight = 4890
ScaleWidth = 4680
StartUpPosition = 3 'Windows Default
Begin ComctlLib.Toolbar tbToolBar
Align = 1 'Align Top
Height = 420
Left = 0
TabIndex = 1
Top = 0
Width = 4680
_ExtentX = 8255
_ExtentY = 741
ButtonWidth = 635
ButtonHeight = 582
Appearance = 1
ImageList = "imlIcons"
BeginProperty Buttons {7791BA41-E020-11CF-8E74-00A0C90F26F8}
NumButtons = 17
BeginProperty Button1 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
Key = "New"
Object.ToolTipText = "New"
Object.Tag = ""
ImageIndex = 1
EndProperty
BeginProperty Button2 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
Key = "Open"
Object.ToolTipText = "Open"
Object.Tag = ""
ImageIndex = 2
EndProperty
BeginProperty Button3 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
Key = "Save"
Object.ToolTipText = "Save"
Object.Tag = ""
ImageIndex = 3
EndProperty
BeginProperty Button4 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
Object.Tag = ""
Style = 3
EndProperty
BeginProperty Button5 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
Key = "Print"
Object.ToolTipText = "Print"
Object.Tag = ""
ImageIndex = 4
EndProperty
BeginProperty Button6 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
Object.Tag = ""
Style = 3
EndProperty
BeginProperty Button7 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
Key = "Cut"
Object.ToolTipText = "Cut"
Object.Tag = ""
ImageIndex = 5
EndProperty
BeginProperty Button8 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
Key = "Copy"
Object.ToolTipText = "Copy"
Object.Tag = ""
ImageIndex = 6
EndProperty
BeginProperty Button9 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
Key = "Paste"
Object.ToolTipText = "Paste"
Object.Tag = ""
ImageIndex = 7
EndProperty
BeginProperty Button10 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
Object.Tag = ""
Style = 3
EndProperty
BeginProperty Button11 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
Key = "Bold"
Object.ToolTipText = "Bold"
Object.Tag = ""
ImageIndex = 8
EndProperty
BeginProperty Button12 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
Key = "Italic"
Object.ToolTipText = "Italic"
Object.Tag = ""
ImageIndex = 9
EndProperty
BeginProperty Button13 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
Key = "Underline"
Object.ToolTipText = "Underline"
Object.Tag = ""
ImageIndex = 10
EndProperty
BeginProperty Button14 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
Object.Tag = ""
Style = 3
EndProperty
BeginProperty Button15 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
Key = "Left"
Object.ToolTipText = "Left Justify"
Object.Tag = ""
ImageIndex = 11
EndProperty
BeginProperty Button16 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
Key = "Center"
Object.ToolTipText = "Center"
Object.Tag = ""
ImageIndex = 12
EndProperty
BeginProperty Button17 {7791BA43-E020-11CF-8E74-00A0C90F26F8}
Key = "Right"
Object.ToolTipText = "Right Justify"
Object.Tag = ""
ImageIndex = 13
EndProperty
EndProperty
End
Begin VB.ListBox List1
Height = 675
Left = 360
TabIndex = 3
Top = 1320
Width = 3975
End
Begin MSComDlg.CommonDialog dlgCommonDialog
Left = 2880
Top = 600
_ExtentX = 847
_ExtentY = 847
FontSize = 1.87198e-37
End
Begin ComctlLib.StatusBar sbStatusBar
Align = 2 'Align Bottom
Height = 270
Left = 0
TabIndex = 0
Top = 4620
Width = 4680
_ExtentX = 8255
_ExtentY = 476
SimpleText = ""
BeginProperty Panels {2C787A51-E01C-11CF-8E74-00A0C90F26F8}
NumPanels = 3
BeginProperty Panel1 {2C787A53-E01C-11CF-8E74-00A0C90F26F8}
AutoSize = 1
Object.Width = 2619
MinWidth = 2540
Text = "Status"
TextSave = "Status"
Object.Tag = ""
EndProperty
BeginProperty Panel2 {2C787A53-E01C-11CF-8E74-00A0C90F26F8}
Style = 6
AutoSize = 2
Object.Width = 2540
MinWidth = 2540
TextSave = "12/4/96"
Object.Tag = ""
EndProperty
BeginProperty Panel3 {2C787A53-E01C-11CF-8E74-00A0C90F26F8}
Style = 5
AutoSize = 2
Object.Width = 2540
MinWidth = 2540
TextSave = "3:06 PM"
Object.Tag = ""
EndProperty
EndProperty
BeginProperty Font {0BE35203-8F91-11CE-9DE3-00AA004BB851}
Name = "MS Sans Serif"
Size = 8.25
Charset = 0
Weight = 400
Underline = 0 'False
Italic = 0 'False
Strikethrough = 0 'False
EndProperty
End
Begin InetCtlsObjects.Inet Inet1
Left = 1200
Top = 480
_ExtentX = 1005
_ExtentY = 1005
End
Begin RichTextLib.RichTextBox RichTextBox1
Height = 2175
Left = 360
TabIndex = 2
Top = 2400
Width = 3975
_ExtentX = 7011
_ExtentY = 3836
ScrollBars = 3
TextRTF = $"frmMain.frx":0000
End
Begin VB.Label Label2
Caption = "Text:"
Height = 495
Left = 360
TabIndex = 5
Top = 2160
Width = 1215
End
Begin VB.Label label1
Caption = "Text Files:"
Height = 495
Left = 360
TabIndex = 4
Top = 1080
Width = 1215
End
Begin ComctlLib.ImageList imlIcons
Left = 2040
Top = 480
_ExtentX = 1005
_ExtentY = 1005
BackColor = -2147483643
ImageWidth = 16
ImageHeight = 16
MaskColor = 12632256
BeginProperty Images {8556BCD1-E01E-11CF-8E74-00A0C90F26F8}
NumListImages = 13
BeginProperty ListImage1 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
Picture = "frmMain.frx":00F7
Key = ""
EndProperty
BeginProperty ListImage2 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
Picture = "frmMain.frx":0449
Key = ""
EndProperty
BeginProperty ListImage3 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
Picture = "frmMain.frx":079B
Key = ""
EndProperty
BeginProperty ListImage4 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
Picture = "frmMain.frx":0AED
Key = ""
EndProperty
BeginProperty ListImage5 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
Picture = "frmMain.frx":0E3F
Key = ""
EndProperty
BeginProperty ListImage6 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
Picture = "frmMain.frx":1191
Key = ""
EndProperty
BeginProperty ListImage7 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
Picture = "frmMain.frx":14E3
Key = ""
EndProperty
BeginProperty ListImage8 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
Picture = "frmMain.frx":1835
Key = ""
EndProperty
BeginProperty ListImage9 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
Picture = "frmMain.frx":1B87
Key = ""
EndProperty
BeginProperty ListImage10 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
Picture = "frmMain.frx":1ED9
Key = ""
EndProperty
BeginProperty ListImage11 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
Picture = "frmMain.frx":222B
Key = ""
EndProperty
BeginProperty ListImage12 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
Picture = "frmMain.frx":257D
Key = ""
EndProperty
BeginProperty ListImage13 {8556BCD3-E01E-11CF-8E74-00A0C90F26F8}
Picture = "frmMain.frx":28CF
Key = ""
EndProperty
EndProperty
End
Begin VB.Menu mnuFile
Caption = "&File"
Begin VB.Menu mnuFileNew
Caption = "&New"
Shortcut = ^N
End
Begin VB.Menu mnuFileOpen
Caption = "&Open"
Shortcut = ^O
End
Begin VB.Menu mnuFileClose
Caption = "&Close"
End
Begin VB.Menu Getfile
Caption = "Get File"
End
Begin VB.Menu mnuFileBar1
Caption = "-"
End
Begin VB.Menu mnuFileSave
Caption = "&Save"
Shortcut = ^S
End
Begin VB.Menu mnuFileSaveAs
Caption = "Save &As..."
End
Begin VB.Menu mnuFileSaveAll
Caption = "Save A&ll"
End
Begin VB.Menu mnuFileBar2
Caption = "-"
End
Begin VB.Menu mnuFileProperties
Caption = "Propert&ies"
End
Begin VB.Menu mnuFileBar3
Caption = "-"
End
Begin VB.Menu mnuFilePageSetup
Caption = "Page Set&up..."
End
Begin VB.Menu mnuFilePrintPreview
Caption = "Print Pre&view"
End
Begin VB.Menu mnuFilePrint
Caption = "&Print..."
Shortcut = ^P
End
Begin VB.Menu mnuFileBar4
Caption = "-"
End
Begin VB.Menu mnuFileSend
Caption = "Sen&d..."
End
Begin VB.Menu mnuFileBar5
Caption = "-"
End
Begin VB.Menu mnuFileMRU
Caption = ""
Index = 0
Visible = 0 'False
End
Begin VB.Menu mnuFileMRU
Caption = ""
Index = 1
Visible = 0 'False
End
Begin VB.Menu mnuFileMRU
Caption = ""
Index = 2
Visible = 0 'False
End
Begin VB.Menu mnuFileMRU
Caption = ""
Index = 3
Visible = 0 'False
End
Begin VB.Menu mnuFileBar6
Caption = "-"
Visible = 0 'False
End
Begin VB.Menu mnuFileExit
Caption = "E&xit"
End
End
Begin VB.Menu mnuEdit
Caption = "&Edit"
Begin VB.Menu mnuEditUndo
Caption = "&Undo"
Shortcut = ^Z
End
Begin VB.Menu mnuEditBar1
Caption = "-"
End
Begin VB.Menu mnuEditCut
Caption = "Cu&t"
Shortcut = ^X
End
Begin VB.Menu mnuEditCopy
Caption = "&Copy"
Shortcut = ^C
End
Begin VB.Menu mnuEditPaste
Caption = "&Paste"
Shortcut = ^V
End
Begin VB.Menu mnuEditPasteSpecial
Caption = "Paste &Special..."
End
End
Begin VB.Menu mnuHelp
Caption = "&Help"
Begin VB.Menu mnuHelpContents
Caption = "&Contents"
End
Begin VB.Menu mnuHelpSearch
Caption = "&Search For Help On..."
End
Begin VB.Menu mnuHelpBar1
Caption = "-"
End
Begin VB.Menu mnuHelpAbout
Caption = "&About FTPer..."
End
End
End
Attribute VB_Name = "frmMain"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Declare Function OSWinHelp% Lib "user32" Alias "WinHelpA" (ByVal hwnd&, ByVal HelpFile$, ByVal wCommand%, dwData As Any)
Private Sub Form_Load()
Dim DirectoryText
Me.Left = GetSetting(App.Title, "Settings", "MainLeft", 1000)
Me.Top = GetSetting(App.Title, "Settings", "MainTop", 1000)
Me.Width = GetSetting(App.Title, "Settings", "MainWidth", 6500)
Me.Height = GetSetting(App.Title, "Settings", "MainHeight", 6500)
DirectoryText = Inet1.OpenURL("ftp://ftp.microsoft.com")
DirectoryTextLength = Len(DirectoryText)
StringEnd = InStr(DirectoryText, ".txt""")
While (StringEnd <> 0)
StringLength = 0
While (InStr(StringEnd - StringLength, Left(DirectoryText,
StringEnd), """") = 0)
StringLength = StringLength + 1
Wend
StringLength = StringLength - 1
List1.AddItem Mid(DirectoryText, StringEnd - StringLength + 1,
StringLength - 1) + ".txt"
DirectoryText = Right(DirectoryText, DirectoryTextLength -
StringEnd)
DirectoryTextLength = DirectoryTextLength - StringEnd
StringEnd = InStr(DirectoryText, ".txt""")
Wend
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Me.WindowState <> vbMinimized Then
SaveSetting App.Title, "Settings", "MainLeft", Me.Left
SaveSetting App.Title, "Settings", "MainTop", Me.Top
SaveSetting App.Title, "Settings", "MainWidth", Me.Width
SaveSetting App.Title, "Settings", "MainHeight", Me.Height
End If
End Sub
Private Sub Getfile_Click()
Dim binarydata() As Byte
binarydata() = Inet1.OpenURL("ftp://ftp.microsoft.com/LS-LR.zip",
icByteArray)
Open "E:\vb\LS-LR.zip" For Binary Access Write As #1
Put #1, , binarydata()
Close #1
MsgBox "Finished."
End Sub
Private Sub List1_Click()
RichTextBox1.Text = Inet1.OpenURL("ftp://ftp.microsoft.com/" + L
ist1.Text)
End Sub
Private Sub mnuHelpAbout_Click()
'To Do
MsgBox "About Box Code goes here!"
End Sub
Private Sub tbToolBar_ButtonClick(ByVal Button As ComctlLib.Button)
Select Case Button.Key
Case "New"
mnuFileNew_Click
Case "New"
mnuFileNew_Click
Case "Open"
mnuFileOpen_Click
Case "Save"
mnuFileSave_Click
Case "Print"
mnuFilePrint_Click
Case "Cut"
mnuEditCut_Click
Case "Copy"
mnuEditCopy_Click
Case "Paste"
mnuEditPaste_Click
Case "Bold"
'To Do
MsgBox "Bold Code goes here!"
Case "Italic"
'To Do
MsgBox "Italic Code goes here!"
Case "Underline"
'To Do
MsgBox "Underline Code goes here!"
Case "Left"
'To Do
MsgBox "Left Code goes here!"
Case "Center"
'To Do
MsgBox "Center Code goes here!"
Case "Right"
'To Do
MsgBox "Right Code goes here!"
End Select
End Sub
Private Sub mnuHelpContents_Click()
Dim nRet As Integer
'if there is no helpfile for this project display a message to t
'he user you can set the HelpFile for your application in the
'Project Properties dialog
If Len(App.HelpFile) = 0 Then
MsgBox "Unable to display Help Contents. There is no Help associated with this project.", vbInformation, Me.Caption
Else
On Error Resume Next
nRet = OSWinHelp(Me.hwnd, App.HelpFile, 3, 0)
If Err Then
MsgBox Err.Description
End If
End If
End Sub
Private Sub mnuHelpSearch_Click()
Dim nRet As Integer
'if there is no helpfile for this project display a message to
'the user you can set the HelpFile for your application in the
'Project Properties dialog
If Len(App.HelpFile) = 0 Then
MsgBox "Unable to display Help Contents. There is no Help associated with this project.", vbInformation, Me.Caption
Else
On Error Resume Next
nRet = OSWinHelp(Me.hwnd, App.HelpFile, 261, 0)
If Err Then
MsgBox Err.Description
End If
End If
End Sub
Private Sub mnuEditCopy_Click()
'To Do
MsgBox "Copy Code goes here!"
End Sub
Private Sub mnuEditCut_Click()
'To Do
MsgBox "Cut Code goes here!"
End Sub
Private Sub mnuEditPaste_Click()
'To Do
MsgBox "Paste Code goes here!"
End Sub
Private Sub mnuEditPasteSpecial_Click()
'To Do
MsgBox "Paste Special Code goes here!"
End Sub
Private Sub mnuEditUndo_Click()
'To Do
MsgBox "Undo Code goes here!"
End Sub
Private Sub mnuFileOpen_Click()
Dim sFile As String
With dlgCommonDialog
'To Do
'set the flags and attributes of the
'common dialog control
.Filter = "All Files (*.*)|*.*"
.ShowOpen
If Len(.filename) = 0 Then
Exit Sub
End If
sFile = .filename
End With
'To Do
'process the opened file
End Sub
Private Sub mnuFileClose_Click()
'To Do
MsgBox "Close Code goes here!"
End Sub
Private Sub mnuFileSave_Click()
'To Do
MsgBox "Save Code goes here!"
End Sub
Private Sub mnuFileSaveAs_Click()
'To Do
'Set up the common dialog control
'prior to calling ShowSave
dlgCommonDialog.ShowSave
End Sub
Private Sub mnuFileSaveAll_Click()
'To Do
MsgBox "Save All Code goes here!"
End Sub
Private Sub mnuFileProperties_Click()
'To Do
MsgBox "Properties Code goes here!"
End Sub
Private Sub mnuFilePageSetup_Click()
dlgCommonDialog.ShowPrinter
End Sub
Private Sub mnuFilePrintPreview_Click()
'To Do
MsgBox "Print Preview Code goes here!"
End Sub
Private Sub mnuFilePrint_Click()
'To Do
MsgBox "Print Code goes here!"
End Sub
Private Sub mnuFileSend_Click()
'To Do
MsgBox "Send Code goes here!"
End Sub
Private Sub mnuFileMRU_Click(Index As Integer)
'To Do
MsgBox "MRU Code goes here!"
End Sub
Private Sub mnuFileExit_Click()
'unload the form
Unload Me
End Sub
Private Sub mnuFileNew_Click()
'To Do
MsgBox "New File Code goes here!"
End Sub
We've explored the FTP protocol, and we've seen how powerful it is. The Internet Transfer control also supports the HTTP protocol, and we'll look into that next.