Accessing UNIX file system from VB

CyrilCarter
CyrilCarter used Ask the Experts™
on
I have a requirement to store files to a unix server at runtime.  Can I use  NETRESOURCE object to do this similar to accessing a Windows file system?  If not, what should I use?  Does anyone have sample code for me to plagiarize :)

Thanks, Cyril
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Commented:
Here is an example of a class called cSystem.  It performs FTP to any ftp server:

Option Explicit
Private m_file As cFile
Private m_fileList As cFileList
Private m_noFiles As Boolean
Private m_host As String
Private m_directory As String
Private m_error As String
Private m_path As String
Private m_list As Collection
Private m_transferType As Long
'persistent handle to the internet
Private hInternet As Long

'persistent handle internet connection
Private hConnect As Long

'default FTP login data
Private m_UserId As String
Private m_password As String

'constants are more efficient than literals
'when used in several places
Private Const sRootDots = ".."
Private Const sSlash = "/"

Private Type SYSTEMTIME
    wYear                   As Integer
    wMonth                  As Integer
    wDayOfWeek              As Integer
    wDay                    As Integer
    wHour                   As Integer
    wMinute                 As Integer
    wSecond                 As Integer
    wMillisecond            As Integer
End Type

Private Declare Function FtpGetFile Lib "wininet" _
   Alias "FtpGetFileA" _
  (ByVal hConnect As Long, _
   ByVal lpszRemoteFile As String, _
   ByVal lpszNewFile As String, _
   ByVal fFailIfExists As Long, _
   ByVal dwFlagsAndAttributes As Long, _
   ByVal dwFlags As Long, _
   ByVal dwContext As Long) As Long

Private Declare Function FtpPutFile Lib "wininet" _
   Alias "FtpPutFileA" _
  (ByVal hConnect As Long, _
   ByVal lpszLocalFile As String, _
   ByVal lpszNewRemoteFile As String, _
   ByVal dwFlags As Long, _
   ByVal dwContext As Long) As Long
   
Private Declare Function FileTimeToSystemTime Lib "kernel32" ( _
    lpFileTime As FILETIME, lpSystemTime As SYSTEMTIME) As Long

Private Const MAX_PATH  As Long = 260
Private Const FILE_ATTRIBUTE_ARCHIVE = &H20

'use registry configuration
Private Const INTERNET_OPEN_TYPE_PRECONFIG = 0
'direct to net
Private Const INTERNET_OPEN_TYPE_DIRECT = 1
'via named proxy
Private Const INTERNET_OPEN_TYPE_PROXY = 3
'prevent using java/script/INS
Private Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4
'used for FTP connections
Private Const INTERNET_FLAG_PASSIVE = &H8000000
Private Const INTERNET_FLAG_RELOAD = &H80000000

'additional cache flags
'don't write this item to the cache
Private Const INTERNET_FLAG_NO_CACHE_WRITE = &H4000000
Private Const INTERNET_FLAG_DONT_CACHE = INTERNET_FLAG_NO_CACHE_WRITE
'make this item persistent in cache
Private Const INTERNET_FLAG_MAKE_PERSISTENT = &H2000000
'use offline semantics
Private Const INTERNET_FLAG_FROM_CACHE = &H1000000
Private Const INTERNET_FLAG_OFFLINE = INTERNET_FLAG_FROM_CACHE

'additional flags
'use PCT/SSL if applicable (HTTP)
Private Const INTERNET_FLAG_SECURE = &H800000
'use keep-alive semantics
Private Const INTERNET_FLAG_KEEP_CONNECTION = &H400000
'don't handle redirections automatically
Private Const INTERNET_FLAG_NO_AUTO_REDIRECT = &H200000
'do background read prefetch
Private Const INTERNET_FLAG_READ_PREFETCH = &H100000
'no automatic cookie handling
Private Const INTERNET_FLAG_NO_COOKIES = &H80000
'no automatic authentication handling
Private Const INTERNET_FLAG_NO_AUTH = &H40000
'return cache file if net request fails
Private Const INTERNET_FLAG_CACHE_IF_NET_FAIL = &H10000
'return cache file if net request fails
Private Const INTERNET_DEFAULT_FTP_PORT = 21
'   "     "  gopher "
Private Const INTERNET_DEFAULT_GOPHER_PORT = 70
'   "     "  HTTP   "
Private Const INTERNET_DEFAULT_HTTP_PORT = 80
'   "     "  HTTPS  "
Private Const INTERNET_DEFAULT_HTTPS_PORT = 443
'default for SOCKS firewall servers.
Private Const INTERNET_DEFAULT_SOCKS_PORT = 1080
'FTP: use existing InternetConnect handle for server if possible
Private Const INTERNET_FLAG_EXISTING_CONNECT = &H20000000
Private Const INTERNET_SERVICE_FTP = 1
Private Const INTERNET_SERVICE_GOPHER = 2
Private Const INTERNET_SERVICE_HTTP = 3

'transfer flags
Private Const FTP_TRANSFER_TYPE_UNKNOWN = &H0
Private Const FTP_TRANSFER_TYPE_ASCII = &H1
Private Const FTP_TRANSFER_TYPE_BINARY = &H2
Private Const INTERNET_FLAG_TRANSFER_ASCII = FTP_TRANSFER_TYPE_ASCII
Private Const INTERNET_FLAG_TRANSFER_BINARY = FTP_TRANSFER_TYPE_BINARY
Private Const FTP_TRANSFER_TYPE_MASK = (FTP_TRANSFER_TYPE_ASCII Or _
                                       FTP_TRANSFER_TYPE_BINARY)

'internet error flags
Private Const INTERNET_ERROR_BASE = 12000
Private Const ERROR_INTERNET_OUT_OF_HANDLES = (INTERNET_ERROR_BASE + 1)
Private Const ERROR_INTERNET_TIMEOUT = (INTERNET_ERROR_BASE + 2)
Private Const ERROR_INTERNET_EXTENDED_ERROR = (INTERNET_ERROR_BASE + 3)
Private Const ERROR_INTERNET_INTERNAL_ERROR = (INTERNET_ERROR_BASE + 4)
Private Const ERROR_INTERNET_INVALID_URL = (INTERNET_ERROR_BASE + 5)
Private Const ERROR_INTERNET_UNRECOGNIZED_SCHEME = (INTERNET_ERROR_BASE + 6)
Private Const ERROR_INTERNET_NAME_NOT_RESOLVED = (INTERNET_ERROR_BASE + 7)
Private Const ERROR_INTERNET_PROTOCOL_NOT_FOUND = (INTERNET_ERROR_BASE + 8)
Private Const ERROR_INTERNET_INVALID_OPTION = (INTERNET_ERROR_BASE + 9)
Private Const ERROR_INTERNET_BAD_OPTION_LENGTH = (INTERNET_ERROR_BASE + 10)
Private Const ERROR_INTERNET_OPTION_NOT_SETTABLE = (INTERNET_ERROR_BASE + 11)
Private Const ERROR_INTERNET_SHUTDOWN = (INTERNET_ERROR_BASE + 12)
Private Const ERROR_INTERNET_INCORRECT_USER_NAME = (INTERNET_ERROR_BASE + 13)
Private Const ERROR_INTERNET_INCORRECT_PASSWORD = (INTERNET_ERROR_BASE + 14)
Private Const ERROR_INTERNET_LOGIN_FAILURE = (INTERNET_ERROR_BASE + 15)
Private Const ERROR_INTERNET_INVALID_OPERATION = (INTERNET_ERROR_BASE + 16)
Private Const ERROR_INTERNET_OPERATION_CANCELLED = (INTERNET_ERROR_BASE + 17)
Private Const ERROR_INTERNET_INCORRECT_HANDLE_TYPE = (INTERNET_ERROR_BASE + 18)
Private Const ERROR_INTERNET_INCORRECT_HANDLE_STATE = (INTERNET_ERROR_BASE + 19)
Private Const ERROR_INTERNET_NOT_PROXY_REQUEST = (INTERNET_ERROR_BASE + 20)
Private Const ERROR_INTERNET_REGISTRY_VALUE_NOT_FOUND = (INTERNET_ERROR_BASE + 21)
Private Const ERROR_INTERNET_BAD_REGISTRY_PARAMETER = (INTERNET_ERROR_BASE + 22)
Private Const ERROR_INTERNET_NO_DIRECT_ACCESS = (INTERNET_ERROR_BASE + 23)
Private Const ERROR_INTERNET_NO_CONTEXT = (INTERNET_ERROR_BASE + 24)
Private Const ERROR_INTERNET_NO_CALLBACK = (INTERNET_ERROR_BASE + 25)
Private Const ERROR_INTERNET_REQUEST_PENDING = (INTERNET_ERROR_BASE + 26)
Private Const ERROR_INTERNET_INCORRECT_FORMAT = (INTERNET_ERROR_BASE + 27)
Private Const ERROR_INTERNET_ITEM_NOT_FOUND = (INTERNET_ERROR_BASE + 28)
Private Const ERROR_INTERNET_CANNOT_CONNECT = (INTERNET_ERROR_BASE + 29)
Private Const ERROR_INTERNET_CONNECTION_ABORTED = (INTERNET_ERROR_BASE + 30)
Private Const ERROR_INTERNET_CONNECTION_RESET = (INTERNET_ERROR_BASE + 31)
Private Const ERROR_INTERNET_FORCE_RETRY = (INTERNET_ERROR_BASE + 32)
Private Const ERROR_INTERNET_INVALID_PROXY_REQUEST = (INTERNET_ERROR_BASE + 33)
Private Const ERROR_INTERNET_NEED_UI = (INTERNET_ERROR_BASE + 34)
Private Const ERROR_INTERNET_HANDLE_EXISTS = (INTERNET_ERROR_BASE + 36)
Private Const ERROR_INTERNET_SEC_CERT_DATE_INVALID = (INTERNET_ERROR_BASE + 37)
Private Const ERROR_INTERNET_SEC_CERT_CN_INVALID = (INTERNET_ERROR_BASE + 38)
Private Const ERROR_INTERNET_HTTP_TO_HTTPS_ON_REDIR = (INTERNET_ERROR_BASE + 39)
Private Const ERROR_INTERNET_HTTPS_TO_HTTP_ON_REDIR = (INTERNET_ERROR_BASE + 40)
Private Const ERROR_INTERNET_MIXED_SECURITY = (INTERNET_ERROR_BASE + 41)
Private Const ERROR_INTERNET_CHG_POST_IS_NON_SECURE = (INTERNET_ERROR_BASE + 42)
Private Const ERROR_INTERNET_POST_IS_NON_SECURE = (INTERNET_ERROR_BASE + 43)
Private Const ERROR_INTERNET_CLIENT_AUTH_CERT_NEEDED = (INTERNET_ERROR_BASE + 44)
Private Const ERROR_INTERNET_INVALID_CA = (INTERNET_ERROR_BASE + 45)
Private Const ERROR_INTERNET_CLIENT_AUTH_NOT_SETUP = (INTERNET_ERROR_BASE + 46)
Private Const ERROR_INTERNET_ASYNC_THREAD_FAILED = (INTERNET_ERROR_BASE + 47)
Private Const ERROR_INTERNET_REDIRECT_SCHEME_CHANGE = (INTERNET_ERROR_BASE + 48)
Private Const ERROR_INTERNET_DIALOG_PENDING = (INTERNET_ERROR_BASE + 49)
Private Const ERROR_INTERNET_RETRY_DIALOG = (INTERNET_ERROR_BASE + 50)
Private Const ERROR_INTERNET_HTTPS_HTTP_SUBMIT_REDIR = (INTERNET_ERROR_BASE + 52)
Private Const ERROR_INTERNET_INSERT_CDROM = (INTERNET_ERROR_BASE + 53)
Private Const ERROR_INTERNET_FORTEZZA_LOGIN_NEEDED = (INTERNET_ERROR_BASE + 54)
Private Const ERROR_INTERNET_SEC_CERT_ERRORS = (INTERNET_ERROR_BASE + 55)
Private Const ERROR_INTERNET_SEC_CERT_NO_REV = (INTERNET_ERROR_BASE + 56)
Private Const ERROR_INTERNET_SEC_CERT_REV_FAILED = (INTERNET_ERROR_BASE + 57)

'FTP API errors
Private Const ERROR_FTP_TRANSFER_IN_PROGRESS = (INTERNET_ERROR_BASE + 110)
Private Const ERROR_FTP_DROPPED = (INTERNET_ERROR_BASE + 111)
Private Const ERROR_FTP_NO_PASSIVE_MODE = (INTERNET_ERROR_BASE + 112)

'gopher API errors
Private Const ERROR_GOPHER_PROTOCOL_ERROR = (INTERNET_ERROR_BASE + 130)
Private Const ERROR_GOPHER_NOT_FILE = (INTERNET_ERROR_BASE + 131)
Private Const ERROR_GOPHER_DATA_ERROR = (INTERNET_ERROR_BASE + 132)
Private Const ERROR_GOPHER_END_OF_DATA = (INTERNET_ERROR_BASE + 133)
Private Const ERROR_GOPHER_INVALID_LOCATOR = (INTERNET_ERROR_BASE + 134)
Private Const ERROR_GOPHER_INCORRECT_LOCATOR_TYPE = (INTERNET_ERROR_BASE + 135)
Private Const ERROR_GOPHER_NOT_GOPHER_PLUS = (INTERNET_ERROR_BASE + 136)
Private Const ERROR_GOPHER_ATTRIBUTE_NOT_FOUND = (INTERNET_ERROR_BASE + 137)
Private Const ERROR_GOPHER_UNKNOWN_LOCATOR = (INTERNET_ERROR_BASE + 138)

'HTTP API errors
Private Const ERROR_HTTP_HEADER_NOT_FOUND = (INTERNET_ERROR_BASE + 150)
Private Const ERROR_HTTP_DOWNLEVEL_SERVER = (INTERNET_ERROR_BASE + 151)
Private Const ERROR_HTTP_INVALID_SERVER_RESPONSE = (INTERNET_ERROR_BASE + 152)
Private Const ERROR_HTTP_INVALID_HEADER = (INTERNET_ERROR_BASE + 153)
Private Const ERROR_HTTP_INVALID_QUERY_REQUEST = (INTERNET_ERROR_BASE + 154)
Private Const ERROR_HTTP_HEADER_ALREADY_EXISTS = (INTERNET_ERROR_BASE + 155)
Private Const ERROR_HTTP_REDIRECT_FAILED = (INTERNET_ERROR_BASE + 156)
Private Const ERROR_HTTP_NOT_REDIRECTED = (INTERNET_ERROR_BASE + 160)
Private Const ERROR_HTTP_COOKIE_NEEDS_CONFIRMATION = (INTERNET_ERROR_BASE + 161)
Private Const ERROR_HTTP_COOKIE_DECLINED = (INTERNET_ERROR_BASE + 162)
Private Const ERROR_HTTP_REDIRECT_NEEDS_CONFIRMATION = (INTERNET_ERROR_BASE + 168)

'additional Internet API error codes
Private Const ERROR_INTERNET_SECURITY_CHANNEL_ERROR = (INTERNET_ERROR_BASE + 157)
Private Const ERROR_INTERNET_UNABLE_TO_CACHE_FILE = (INTERNET_ERROR_BASE + 158)
Private Const ERROR_INTERNET_TCPIP_NOT_INSTALLED = (INTERNET_ERROR_BASE + 159)
Private Const ERROR_INTERNET_DISCONNECTED = (INTERNET_ERROR_BASE + 163)
Private Const ERROR_INTERNET_SERVER_UNREACHABLE = (INTERNET_ERROR_BASE + 164)
Private Const ERROR_INTERNET_PROXY_SERVER_UNREACHABLE = (INTERNET_ERROR_BASE + 165)
Private Const ERROR_INTERNET_BAD_AUTO_PROXY_SCRIPT = (INTERNET_ERROR_BASE + 166)
Private Const ERROR_INTERNET_UNABLE_TO_DOWNLOAD_SCRIPT = (INTERNET_ERROR_BASE + 167)
Private Const ERROR_INTERNET_SEC_INVALID_CERT = (INTERNET_ERROR_BASE + 169)
Private Const ERROR_INTERNET_SEC_CERT_REVOKED = (INTERNET_ERROR_BASE + 170)

'InternetAutodial specific errors
Private Const ERROR_INTERNET_FAILED_DUETOSECURITYCHECK = (INTERNET_ERROR_BASE + 171)
Private Const ERROR_INTERNET_NOT_INITIALIZED = (INTERNET_ERROR_BASE + 172)
Private Const ERROR_INTERNET_NEED_MSN_SSPI_PKG = (INTERNET_ERROR_BASE + 173)
Private Const ERROR_INTERNET_LOGIN_FAILURE_DISPLAY_ENTITY_BODY = (INTERNET_ERROR_BASE + 174)
Private Const INTERNET_ERROR_LAST = (INTERNET_ERROR_BASE + 174)

Private Type FILETIME
   dwLowDateTime As Long
   dwHighDateTime As Long
End Type


Private Type WIN32_FIND_DATA
   dwFileAttributes As Long
   ftCreationTime As FILETIME
   ftLastAccessTime As FILETIME
   ftLastWriteTime As FILETIME
   nFileSizeHigh As Long
   nFileSizeLow As Long
   dwReserved0 As Long
   dwReserved1 As Long
   cFileName As String * MAX_PATH
   cAlternate As String * 14
End Type

Private Declare Function InternetOpen Lib "wininet" _
   Alias "InternetOpenA" _
  (ByVal lpszAgent As String, _
   ByVal dwAccessType As Long, _
   ByVal lpszProxyName As String, _
   ByVal lpszProxyBypass As String, _
   ByVal dwFlags As Long) As Long

Private Declare Function InternetCloseHandle Lib "wininet" _
   (ByVal hEnumHandle As Long) As Long

Private Declare Function InternetConnect Lib "wininet" _
   Alias "InternetConnectA" _
  (ByVal hInternet As Long, _
   ByVal lpszServerName As String, _
   ByVal nServerPort As Long, _
   ByVal lpszUserName As String, _
   ByVal lpszPassword As String, _
   ByVal dwService As Long, _
   ByVal dwFlags As Long, _
   ByVal dwContext As Long) As Long

Private Declare Function FtpFindFirstFile Lib "wininet" _
   Alias "FtpFindFirstFileA" _
  (ByVal hConnect As Long, _
   ByVal lpszSearchFile As String, _
   lpFindFileData As Any, _
   ByVal dwFlags As Long, _
   ByVal dwContext As Long) As Long

Private Declare Function InternetFindNextFile Lib "wininet" _
   Alias "InternetFindNextFileA" _
  (ByVal hFind As Long, _
   lpFindFileData As WIN32_FIND_DATA) As Long

Private Declare Function InternetGetLastResponseInfo Lib "wininet" _
   Alias "InternetGetLastResponseInfoA" _
  (lpdwError As Long, _
   ByVal lpszBuffer As String, _
    lpdwBufferLength As Long) As Long
   
Private Declare Function FtpGetCurrentDirectory Lib "wininet" _
   Alias "FtpGetCurrentDirectoryA" _
  (ByVal hConnect As Long, _
   ByVal lpszCurrentDirectory As String, _
    lpdwCurrentDirectory As Long) As Long

Private Declare Function FtpSetCurrentDirectory Lib "wininet" _
   Alias "FtpSetCurrentDirectoryA" _
  (ByVal hConnect As Long, _
   ByVal lpszDirectory As String) As Long
   
   
Public Property Get Files() As cFileList

GetFTPDirectoryContents
Set Files = m_fileList

End Property
Private Function GetErr(ByVal lErrorCode As Long) As String

  Dim sBuffer As String
  Dim nBuffer As Long

  Select Case lErrorCode
     Case 12001: GetErr = _
        "No more handles could be generated at this time"
     Case 12002: GetErr = _
        "The request has timed out."
     Case 12003:
        'extended error. Retrieve the details using
        'the InternetGetLastResponseInfo API.
         
        sBuffer = Space$(256)
        nBuffer = Len(sBuffer)
         
        If InternetGetLastResponseInfo(lErrorCode, sBuffer, nBuffer) Then
           GetErr = StripNull(sBuffer)
        Else
           GetErr = "Extended error returned from server."
        End If
         
     Case 12004: GetErr = _
        "An internal error has occurred."
     Case 12005: GetErr = _
        "URL is invalid."
     Case 12006: GetErr = _
        "URL scheme could not be recognized, or is not supported."
     Case 12007: GetErr = _
        "Server name could not be resolved."
     Case 12008: GetErr = _
        "Requested protocol could not be located."
     Case 12009: GetErr = _
        "Request to InternetQueryOption or InternetSetOption" & _
        " specified an invalid option value."
     Case 12010: GetErr = _
        "Length of an option supplied to InternetQueryOption or" & _
        " InternetSetOption is incorrect for the type of" & _
        " option specified."
     Case 12011: GetErr = _
        "Request option can not be set, only queried. "
     Case 12012: GetErr = _
        "Win32 Internet support is being shutdown or unloaded."
     Case 12013: GetErr = _
        "Request to connect and login to an FTP server could not" & _
        " be completed because the supplied user name is incorrect."
     Case 12014: GetErr = _
        "Request to connect and login to an FTP server could not" & _
        " be completed because the supplied password is incorrect. "
     Case 12015: GetErr = _
        "Request to connect to and login to an FTP server failed."
     Case 12016: GetErr = _
        "Requested operation is invalid. "
     Case 12017: GetErr = _
        "Operation was canceled, usually because the handle on" & _
        " which the request was operating was closed before the" & _
        " operation completed."
     Case 12018: GetErr = _
        "Type of handle supplied is incorrect for this operation."
     Case 12019: GetErr = _
        "Requested operation can not be carried out because the" & _
        " handle supplied is not in the correct state."
     Case 12020: GetErr = _
        "Request can not be made via a proxy."
     Case 12021: GetErr = _
        "Required registry value could not be located. "
     Case 12022: GetErr = _
        "Required registry value was located but is an incorrect" & _
        " type or has an invalid value."
     Case 12023: GetErr = _
        "Direct network access cannot be made at this time. "
     Case 12024: GetErr = _
        "Asynchronous request could not be made because a zero" & _
        " context value was supplied."
     Case 12025: GetErr = _
        "Asynchronous request could not be made because a" & _
        " callback function has not been set."
     Case 12026: GetErr = _
        "Required operation could not be completed because" & _
        " one or more requests are pending."
     Case 12027: GetErr = _
        "Format of the request is invalid."
     Case 12028: GetErr = _
        "Requested item could not be located."
     Case 12029: GetErr = _
        "Attempt to connect to the server failed."
     Case 12030: GetErr = _
        "Connection with the server has been terminated."
     Case 12031: GetErr = _
        "Connection with the server has been reset."
     Case 12036: GetErr = _
        "Request failed because the handle already exists."
     Case Else: GetErr = _
        "Error details not available."
  End Select

End Function

Function StripNull(Item As String)

   'Return a string without the chr$(0) terminator.
    Dim pos As Integer
    pos = InStr(Item, Chr$(0))
   
    If pos Then
        StripNull = Left$(Item, pos - 1)
    Else: StripNull = Item
    End If

End Function
Function StripSpace(Item As String)

   'Return a string without the " " terminator.
    Dim pos As Integer
    pos = InStr(Item, " ")
   
    If pos Then
        StripSpace = Left$(Item, pos - 1)
    Else: StripSpace = Item
    End If

End Function


Public Function Connect()

'Begin the FTP process by obtaining a handle to an internet session. This
'handle will be used in subsequent calls, so its declared as a form-wide variable.

hInternet = InternetOpen("VBnet FTP Transfer", _
                         INTERNET_OPEN_TYPE_DIRECT, _
                         vbNullString, _
                         vbNullString, _
                         INTERNET_FLAG_NO_CACHE_WRITE)
   
'If a handle was obtained, the next step is to obtain a connection handle that will be
'used for all operations except the FTP directory navigation. The MSDN states that
'the handle used by FtpFindFirstFile and subsequent file calls can not be reused for additional
'navigation or other operations. This handle then will be used for all functions except
'the directory listings.
If hInternet Then
    hConnect = InternetConnect(hInternet, _
                               m_host, _
                               INTERNET_DEFAULT_FTP_PORT, _
                               m_UserId, _
                               m_password, _
                               INTERNET_SERVICE_FTP, _
                               INTERNET_FLAG_EXISTING_CONNECT Or _
                               INTERNET_FLAG_PASSIVE, _
                               &H0)
   
    'if the connection handle is valid, get the current FTP directory
    If hConnect <> 0 Then
        'reflect the current path in the text box (it should be '\' )
        If m_directory > "" Then
            SetCurrentFTPDirectory (m_directory)
        End If
         m_directory = GetFTPDirectory
    Else
         On Error Resume Next
         m_error = GetErr(Err.LastDllError)
         Err.Raise -900, "Connect", m_error
    End If
End If
   
End Function

Public Function GetFile(sLocalFile As String, sRemoteFile As String) As Boolean

If FtpGetFile(hConnect, _
              sRemoteFile, _
              sLocalFile, _
              False, _
              FILE_ATTRIBUTE_ARCHIVE, _
              FTP_TRANSFER_TYPE_ASCII, _
              0&) Then
    GetFile = True
Else
    On Error Resume Next
    m_error = GetErr(Err.LastDllError)
    Err.Raise -900, "GetFile", m_error
    GetFile = False
End If

End Function
Public Function PutFile(sLocalFile As String, sRemoteFile As String) As Boolean

If m_transferType = 0 Then
    m_transferType = FTP_TRANSFER_TYPE_ASCII
End If

If FtpPutFile(hConnect, _
              sLocalFile, _
              sRemoteFile, _
              m_transferType, _
              0&) Then
    PutFile = True
Else
    On Error Resume Next
    m_error = GetErr(Err.LastDllError)
    Err.Raise -900, "PutFile", m_error
    PutFile = False
End If

End Function

Public Function GetFTPDirectory() As String

Dim nCurrDir As Long
Dim sCurrDir As String

sCurrDir = Space(256)
nCurrDir = Len(sCurrDir)
       
'returns 1 if successful
If FtpGetCurrentDirectory(hConnect, sCurrDir, nCurrDir) = 1 Then
       
    'return a properly qualified path
    sCurrDir = StripNull(sCurrDir)
       
    If Right(sCurrDir, 1) <> sSlash Then
        m_directory = sCurrDir & sSlash
    Else
        m_directory = sCurrDir
    End If
End If
   
GetFTPDirectory = m_directory

End Function

Public Function GetFTPDirectoryContents() As Collection

Dim WFD As WIN32_FIND_DATA
Dim hFindConnect As Long
Dim hFind As Long
Dim sFileSize As String
Dim tmp As String
Set m_list = New Collection
Set m_fileList = New cFileList
'Obtain the current FTP path
m_path = GetFTPDirectory()
   
'If the path is not the FTP base directory, add ".." to provide a means of navigating
'back up the directory structure. (Note: I added this for use on a localhost test - in testing on some
'sites I found that they returned '.' and '..' as folders, so ideally a check would want to be made
'in the GetFolders method to avoid loading duplicates.)

If m_path <> sSlash Then
     m_list.Add sRootDots
End If
 
'The search parameters .. here we'll list all file types. Since GetFTPDirectory takes
'care of qualifying the path, no terminating slash is required.
m_path = m_path & "*.*"
   
'Conation handles used by the FtpFindFirstFile API go out of scope once the all files are
'listed, therefore it can not be reused. This restriction is handled by obtaining
'a fresh connection handle each time a call to FtpFindFirstFile is required, and releasing
'it once finished.
hFindConnect = GetInternetConnectHandle()
 
'If a good connection handle ...
If hFindConnect Then
   '..obtain the handle to the files with the FtpFindFirstFile API. Obtaining the
   'directory contents is essentially similar to using the Win32 file system APIs
   'FindFirstFile and FindNextFile, with the sole exception that there is no FtpFindNextFile
   'API. Instead, successive calls are made to InternetFindNextFile. The data returned
   'is in the familiar WIN32_FIND_DATA structure.
   
   hFind = FtpFindFirstFile(hFindConnect, _
                            m_directory, WFD, _
                            INTERNET_FLAG_RELOAD Or _
                            INTERNET_FLAG_NO_CACHE_WRITE, 0&)

   'If a valid find handle returned, loop through the directory listing
   'contents. If the attributes of the returned string indicate a folder,
   'append a slash to make it both visually stand out in the list, and identifiable
   'in the list double-click routine for navigation.
   '
   'hFile will be 0 if the navigated-to folder is empty.
   If hFind Then
        Do
            tmp = StripNull(WFD.cFileName)
            tmp = StripSpace(tmp)
            If Len(tmp) Then
                Set m_file = New cFile
                If WFD.dwFileAttributes And vbDirectory Then
                    m_list.Add tmp & sSlash
                    m_file.Name = tmp & sSlash
                    m_file.FileSize = WFD.nFileSizeLow
                    m_file.UpdateTime = GetSysTime(WFD.ftLastWriteTime)
                    m_fileList.Add m_file
                Else
                    m_list.Add tmp
                    m_file.Name = tmp
                    m_file.FileSize = WFD.nFileSizeLow
                    m_file.UpdateTime = GetSysTime(WFD.ftLastWriteTime)
                    m_fileList.Add m_file
                End If
            End If
        'continue while valid
        Loop While InternetFindNextFile(hFind, WFD)
   End If 'If hFind
End If  'If hFindConnect
   
'clean up by closing the handles used in this routine
Call InternetCloseHandle(hFind)
Call InternetCloseHandle(hFindConnect)
   
Set GetFTPDirectoryContents = m_list
   
End Function
Private Function GetInternetConnectHandle() As Long

 Dim tmp As Long

'GetInternetConnectHandle obtains a new handle expressly for use with the
'FtpFindFirstFile and APIs.
'
'Care must be taken to close only the handle returned by this function once the listing
'of the directory has been obtained.
'
'A temporary variable is used here to reduce line length
 If hInternet Then
 
   'Pass the same server as with other calls, along with the requisite username
   'and password. The Microsoft FTP site allows anonymous login, so the username
   'is 'anonymous' and the password is the user's email address.
     
     tmp = InternetConnect(hInternet, _
                           m_host, _
                           INTERNET_DEFAULT_FTP_PORT, _
                           m_UserId, _
                           m_password, _
                           INTERNET_SERVICE_FTP, _
                           INTERNET_FLAG_EXISTING_CONNECT Or _
                           INTERNET_FLAG_PASSIVE, _
                           &H0)
                                               
 End If
 
 'return the connection handle
 GetInternetConnectHandle = tmp
     
End Function
Private Function GetSysTime(ByRef udtTime As FILETIME) As Date

Dim udtSysTime  As SYSTEMTIME
FileTimeToSystemTime udtTime, udtSysTime
With udtSysTime
    GetSysTime = CDate(.wYear & "/" & .wMonth & "/" & .wDay & " " & .wHour & ":" & .wMinute & ":" & .wSecond)
End With
   
End Function

Public Property Get Host() As String
Host = m_host
End Property

Public Property Let Host(ByVal vNewValue As String)
m_host = vNewValue
End Property


Public Property Get Directory() As String
Directory = m_directory
End Property

Public Property Let Directory(ByVal vNewValue As String)

If hInternet > 0 Then
    SetCurrentFTPDirectory (vNewValue)
End If

m_directory = vNewValue

End Property

Public Property Get ErrorDesc() As String
ErrorDesc = m_error
End Property


Public Function SetCurrentFTPDirectory(newPath As String)

m_path = newPath
m_directory = m_path

Call FtpSetCurrentDirectory(hConnect, m_path)

End Function

Public Property Get Userid() As String
Userid = m_UserId
End Property

Public Property Let Userid(ByVal vNewValue As String)
m_UserId = vNewValue
End Property

Public Property Get Password() As String
Password = m_password
End Property

Public Property Let Password(ByVal vNewValue As String)
m_password = vNewValue
End Property

Public Function Disconnect()

Call InternetCloseHandle(hConnect)
Call InternetCloseHandle(hInternet)

hConnect = 0
hInternet = 0
m_directory = ""
m_path = ""

End Function


Public Function IsConnected() As Boolean

If hConnect > 0 Then
    IsConnected = True
Else
    IsConnected = False
End If

End Function

Public Function AppendFile(sLocalFile As String, sRemoteFile As String) As Boolean

AppendFile = PutFile(sLocalFile, sRemoteFile)

End Function

Public Property Get NoFiles() As Boolean
NoFiles = m_noFiles
End Property

Public Property Let NoFiles(ByVal vNewValue As Boolean)
m_noFiles = vNewValue
End Property

Private Sub Class_Initialize()
m_transferType = 0
End Sub

Private Sub Class_Terminate()

Disconnect

End Sub

Public Property Get TransferType() As Long
TransferType = m_transferType
End Property

Public Property Let TransferType(ByVal vNewValue As Long)
m_transferType = vNewValue
End Property
Use Misrosoft servises for unix to map the Unix drive to windows it means the VB code is simple.

Author

Commented:
Richard,
Is this something that I have to buy or is it like a component library in VB?

willisp,  thanks very much for the code!
Ensure you’re charging the right price for your IT

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden using our free interactive tool and use it to determine the right price for your IT services. Start calculating Now!

Author

Commented:
willisp,
My app is not web based so,  what kind of lib should i be using in place of 'wininet'?

Commented:
CyrilCarter,

wininit is used for FTP, so the app doesn't have to be web based.
Misrosoft services for unix is something you have to buy it costs about £75 or $119.01 but it saves a lot of time
why not make UNix directories available for windows..  Here we used "samba", it actually makes you unix directories, browsable by windows same as shared network folders, and you can add, delete, access unix files using it..  It's a free ware.
Author of the Year 2009

Commented:
Hi CyrilCarter,
It appears that you have forgotten this question. I will ask Community Support to close it unless you finalize it within 7 days. I will ask a Community Support Moderator to:

    Accept willisp's comment(s) as an answer.

CyrilCarter, if you think your question was not answered at all or if you need help, just post a new comment here; Community Support will help you.  DO NOT accept THIS comment as an answer.

EXPERTS: If you disagree with that recommendation, please post an explanatory comment.
==========
DanRollins -- EE database cleanup volunteer

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial