File Listings

The File Transfer control can be used to provide file listings of remote directories on an FTP server.

In the following Visual Basic example, an instance of the File Transfer control has been placed on a form. The form also has edit controls for server name, port number, user name, password, and the remote directory. There are command buttons for connecting, changing directory, listing files, and disconnecting. There are checkboxes that specify whether the listing is to be parsed, whether only file names should be returned, and whether SSL Authorization should be used for secure FTP servers. Finally, there is a multi-line, scrolling edit control where the file listing is displayed.

The server type and the three options related to the checkboxes are initialized in the Form_Load event:

Private p_bParseList As Boolean
Private p_bNamesOnly As Boolean

Private Sub chkAuthSSL_Click()
    If chkAuthSSL.Value = 0 Then
        FileTransfer1.Options = FileTransfer1.Options And (Not fileOptionSecureExplicit)
    Else
        FileTransfer1.Options = FileTransfer1.Options Or fileOptionSecureExplicit
    End If
End Sub

Private Sub chkNamesOnly_Click()
    If chkNamesOnly.Value = 0 Then
        p_bNamesOnly = False
    Else
        p_bNamesOnly = True
    End If
End Sub

Private Sub chkUnparsed_Click()
    If chkUnparsed.Value = 0 Then
        p_bParseList = True
    Else
        p_bParseList = False
    End If
End Sub

Private Sub Form_Load()
    chkAuthSSL_Click
    FileTransfer1.ServerType = fileServerFtp
    chkUnparsed_Click
    chkNamesOnly_Click
End Sub

When the connection is made, the current directory is displayed and listed, using the code for the "List" button:

Private Sub cmdConnect_Click()
    Dim lResult as Long
    
    On Error GoTo Err_report
    FileTransfer1.UserName = editUserName.Text
    FileTransfer1.Password = editPassword.Text
    FileTransfer1.ServerPort = editPort.Text
    
    lResult = FileTransfer1.Connect(editServer.Text)
    If lResult <> 0 Then
        MsgBox "Connect Failed" & vbCrLf & FileTransfer1.LastErrorString
    End If
    editRemoteDir.Text = FileTransfer1.ServerDirectory
    cmdList_Click
    Exit Sub
Err_report:
    MsgBox Err.Number & ": " & Err.Description
End Sub

To change directory, the code for the "Change Dir" button reads the Remote Dir control text, and calls the ChangeDirectory method. Then it displays the directory listing for the new directory:

Private Sub cmdChgDir_Click()
    Dim lResult as Long

    If FileTransfer1.ThrowError Then
        On Error GoTo Err_report
    End If
    lResult = FileTransfer1.ChangeDirectory(Trim(editRemoteDir.Text))
    If lResult <> 0 Then
        MsgBox "ChangeDirectory error: " & lResult
    End If
    editRemoteDir.Text = FileTransfer1.ServerDirectory
    cmdList_Click
    Exit Sub
Err_report:
    MsgBox Err.Number & ": " & Err.Description
    editRemoteDir.Text = FileTransfer1.ServerDirectory
End Sub

The code for the "List" button takes note of the "unparsed" and "names only" settings when it invokes the OpenDirectory and ReadDirectory methods. The subroutine loops on ReadDirectory until ReadDirectory returns an error. An error of "end of data" or "end of directory" is a normal termination indicator. Results from ReadDirectory are displayed. The CloseDirectory method is used to terminate the listing.

Private Sub cmdList_Click()
    Dim strDirName As String
    Dim strFileName As String
    Dim dwFileLength As Long
    Dim strFileDate As String
    Dim strFileOwner As String
    Dim strFileGroup As String
    Dim dwFilePerms As Long
    Dim bIsDirectory As Boolean
    Dim lResult as Long
    Dim strPerms As String
    
    strDirName = Trim(editRemoteDir.Text)
    
    If FileTransfer1.ThrowError Then
        On Error GoTo Err_report
    End If
    
    txtFileList.Text = "" 
    
    lResult = FileTransfer1.OpenDirectory(strDirName, p_bParseList)
    If lResult <> 0 Then
        MsgBox "OpenDirectory error: " & lResult
        Exit Sub
    End If
    
    Do
        If p_bNamesOnly Then
            lResult = FileTransfer1.ReadDirectory(strFileName)
        Else
            lResult = FileTransfer1.ReadDirectory(strFileName, _
                        dwFileLength, strFileDate, _
                        strFileOwner, strFileGroup, _
                        dwFilePerms, bIsDirectory)
        End If
        
        If lResult <> 0 Then
            If lResult <> fileErrorEndOfData And _
               lResult <> fileErrorEndOfDirectory Then
                MsgBox "ReadDirectory error: " & lResult
            End If
            Exit Do
        End If
        
        If p_bNamesOnly Then
            txtFileList.Text = txtFileList.Text & strFileName
        Else
            txtFileList.Text = txtFileList.Text & _
                strFileName & " " & dwFileLength & " " & strFileDate & " " & _
                strFileOwner & " " & strFileGroup
            If p_bParseList Then
                strPerms = FilePerms(dwFilePerms, bIsDirectory)
                txtFileList.Text = txtFileList.Text & " " & strPerms
            End If
        End If
        txtFileList.Text = txtFileList.Text & vbCrLf
    Loop
    FileTransfer1.CloseDirectory
    Exit Sub
    
Err_report:
    MsgBox Err.Number & ": " & Err.Description
    FileTransfer1.CloseDirectory
End Sub

The file-listing subroutine uses the function FilePerms to convert the bitmask representation of the file permissions and the Boolean directory flag to a 10-character UNIX-style string, using constants defined by the control to analyze the permissions:

Private Function FilePerms(dwFilePerms As Long, bIsDirectory As Boolean) _
        As String
    If (dwFilePerms And filePermSymbolicLink) = filePermSymbolicLink Then
        FilePerms = "l"
    ElseIf bIsDirectory Then
        FilePerms = "d"
    Else
        FilePerms = "-"
    End If
    If (dwFilePerms And filePermOwnerRead) = filePermOwnerRead Then
        FilePerms = FilePerms & "r"
    Else
        FilePerms = FilePerms & "-"
    End If
    If (dwFilePerms And filePermOwnerWrite) = filePermOwnerWrite Then
        FilePerms = FilePerms & "w"
    Else
        FilePerms = FilePerms & "-"
    End If
    If (dwFilePerms And filePermOwnerExecute) = filePermOwnerExecute Then
        FilePerms = FilePerms & "x"
    Else
        FilePerms = FilePerms & "-"
    End If
    If (dwFilePerms And filePermGroupRead) = filePermGroupRead Then
        FilePerms = FilePerms & "r"
    Else
        FilePerms = FilePerms & "-"
    End If
    If (dwFilePerms And filePermGroupWrite) = filePermGroupWrite Then
        FilePerms = FilePerms & "w"
    Else
        FilePerms = FilePerms & "-"
    End If
    If (dwFilePerms And filePermGroupExecute) = filePermGroupExecute Then
        FilePerms = FilePerms & "x"
    Else
        FilePerms = FilePerms & "-"
    End If
    If (dwFilePerms And filePermWorldRead) = filePermWorldRead Then
        FilePerms = FilePerms & "r"
    Else
        FilePerms = FilePerms & "-"
    End If
    If (dwFilePerms And filePermWorldWrite) = filePermWorldWrite Then
        FilePerms = FilePerms & "w"
    Else
        FilePerms = FilePerms & "-"
    End If
    If (dwFilePerms And filePermWorldExecute) = filePermWorldExecute Then
        FilePerms = FilePerms & "x"
    Else
        FilePerms = FilePerms & "-"
    End If
End Function

Copyright © 2008 Catalyst Development Corporation. All rights reserved.