Listing Files  
 

In addition to uploading and downloading files, the FTP control can also be used to list the files that are in a specific directory on the server. This is done by using three methods: OpenDirectory, ReadDirectory and CloseDirectory. For example, consider a form with two TextBox controls, a ListBox control and a CommandButton control. The first TextBox control is used to specify the host name of the server and the second control specifies the directory. The ListBox control is populated with the name of the files in that directory.

Private Sub Command1_Click()
    Dim strHostName As String
    Dim strDirectory As String
    Dim strFileName As String
    Dim nError As Long
    
    strHostName = Trim(Text1.Text)
    strDirectory = Trim(Text2.Text)

    ' Establish a connection to the server and display any
    ' errors to the user
    nError = FtpClient1.Connect(strHostName)
    If nError Then
        MsgBox FtpClient1.LastErrorString, vbExclamation
        Exit Sub
    End If
    
    ' Open the directory on the server to begin the
    ' process of reading the contents
    nError = FtpClient1.OpenDirectory(strDirectory)
    If nError Then
        MsgBox FtpClient1.LastErrorString, vbExclamation
        FtpClient1.Disconnect
        Exit Sub
    End If
    
    ' Read each file name until the end of the directory
    ' listing is reached
    Do
        nError = FtpClient1.ReadDirectory(strFileName)
        If nError Then Exit Do
        List1.AddItem strFileName
    Loop
    
    If nError <> stErrorEndOfDirectory Then
        MsgBox FtpClient1.LastErrorString, vbExclamation
    End If
    
    ' Close the directory and disconnect from the server
    FtpClient1.CloseDirectory
    FtpClient1.Disconnect
End Sub

In this example, the ReadDirectory method is used to return only the name of the file, however it can return additional information such as the file size, date it was last modified, its access permissions and whether or not its a regular file or a subdirectory. For more information, refer to the Technical Reference documentation for this method.