Results 1 to 6 of 6

Thread: [resolved] Problem with FTP Rename

  1. #1

    Thread Starter
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Resolved [resolved] Problem with FTP Rename

    My Declarations
    Code:
    Public Declare Function apiFtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" (ByVal hConnect As Long, ByVal lpszExisting As String, ByVal lpszNew As String) As Boolean
    Public Declare Function apiFtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByVal hConnect As Long, ByVal lpszFileName As String) As Boolean
    Public Declare Function apiFtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hConnect As Long, ByVal lpszDirectory As String) As Boolean
    Public Declare Function apiFtpRemoveDirectory Lib "wininet.dll" Alias "FtpRemoveDirectoryA" (ByVal hConnect As Long, ByVal lpszDirectory As String) As Boolean
    and my rename code
    Code:
    Public Function RenameFile(ByVal strOldName As String, ByVal strNewName As String, ByVal blnFtpFile As Boolean) As Boolean
        
    '---- FTP -
    '----   will work on the current directory (make sure you are there before calling)
        
    '---- Local -
    '----   Use fill file path names
        
    '----   Note: To rename a directory, use this function
        
        Dim lngRes As Long
        
        On Error Resume Next
        
        RenameFile = True
        
        If blnFtpFile Then
            If IntCon.zIntConHandle <> 0 Then
                lngRes = apiFtpRenameFile(IntCon.zIntConHandle, ByVal strOldName, ByVal strNewName)
            End If
        Else
    '---- local drive rename
    '        lngRes = apiMoveFile(strOldName, strNewName) 'only api to rename a local file
        End If
        
    '---- repeated error msg/clean up
        If Err.Number <> 0 Then
            RenameFile = False
            MsgBox "Error : " & Err.Number & vbCrLf & Err.Description, vbOKOnly + vbExclamation, "Error"
            Err.Clear
        End If
        
        
        If Err.LastDllError <> 0 Then
            RenameFile = False
            MsgBox "Last DLL Error : " & Err.LastDllError, vbOKOnly + vbExclamation, "Error Last Error"
            Err.Clear
        End If
    End Function
    Now it works fine but still raises an error (lastdll) of 2 - which I think is that it cannot find the file.

    I am tempted to ignore that error as it is working, but I wanted to make sure.
    I looked on google, but MSDN says it`s a function, and ActiveVB says its a sub. Confused? I am!

    The function sounds better (at least you'd know if it worked of not).
    Comments?
    Last edited by Ecniv; May 17th, 2005 at 02:52 AM.

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  2. #2
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Problem with FTP Rename

    Well, if MSDN says it is a function then is should be a function.

    Here is a working example from ALLAPI.net, see is that can help you

    VB Code:
    1. Const FTP_TRANSFER_TYPE_UNKNOWN = &H0
    2. Const FTP_TRANSFER_TYPE_ASCII = &H1
    3. Const FTP_TRANSFER_TYPE_BINARY = &H2
    4. Const INTERNET_DEFAULT_FTP_PORT = 21               ' default for FTP servers
    5. Const INTERNET_SERVICE_FTP = 1
    6. Const INTERNET_FLAG_PASSIVE = &H8000000            ' used for FTP connections
    7. Const INTERNET_OPEN_TYPE_PRECONFIG = 0                    ' use registry configuration
    8. Const INTERNET_OPEN_TYPE_DIRECT = 1                        ' direct to net
    9. Const INTERNET_OPEN_TYPE_PROXY = 3                         ' via named proxy
    10. Const INTERNET_OPEN_TYPE_PRECONFIG_WITH_NO_AUTOPROXY = 4   ' prevent using java/script/INS
    11. Const MAX_PATH = 260
    12. Private Type FILETIME
    13.     dwLowDateTime As Long
    14.     dwHighDateTime As Long
    15. End Type
    16. Private Type WIN32_FIND_DATA
    17.     dwFileAttributes As Long
    18.     ftCreationTime As FILETIME
    19.     ftLastAccessTime As FILETIME
    20.     ftLastWriteTime As FILETIME
    21.     nFileSizeHigh As Long
    22.     nFileSizeLow As Long
    23.     dwReserved0 As Long
    24.     dwReserved1 As Long
    25.     cFileName As String * MAX_PATH
    26.     cAlternate As String * 14
    27. End Type
    28. Private Declare Function InternetCloseHandle Lib "wininet" (ByRef hInet As Long) As Long
    29. Private Declare Function InternetConnect Lib "wininet.dll" Alias "InternetConnectA" (ByVal hInternetSession As Long, ByVal sServerName As String, ByVal nServerPort As Integer, ByVal sUserName As String, ByVal sPassword As String, ByVal lService As Long, ByVal lFlags As Long, ByVal lContext As Long) As Long
    30. Private Declare Function InternetOpen Lib "wininet.dll" Alias "InternetOpenA" (ByVal sAgent As String, ByVal lAccessType As Long, ByVal sProxyName As String, ByVal sProxyBypass As String, ByVal lFlags As Long) As Long
    31. Private Declare Function FtpSetCurrentDirectory Lib "wininet.dll" Alias "FtpSetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
    32. Private Declare Function FtpGetCurrentDirectory Lib "wininet.dll" Alias "FtpGetCurrentDirectoryA" (ByVal hFtpSession As Long, ByVal lpszCurrentDirectory As String, lpdwCurrentDirectory As Long) As Long
    33. Private Declare Function FtpCreateDirectory Lib "wininet.dll" Alias "FtpCreateDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
    34. Private Declare Function FtpRemoveDirectory Lib "wininet.dll" Alias "FtpRemoveDirectoryA" (ByVal hFtpSession As Long, ByVal lpszDirectory As String) As Boolean
    35. Private Declare Function FtpDeleteFile Lib "wininet.dll" Alias "FtpDeleteFileA" (ByVal hFtpSession As Long, ByVal lpszFileName As String) As Boolean
    36. Private Declare Function FtpRenameFile Lib "wininet.dll" Alias "FtpRenameFileA" (ByVal hFtpSession As Long, ByVal lpszExisting As String, ByVal lpszNew As String) As Boolean
    37. Private Declare Function FtpGetFile Lib "wininet.dll" 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, ByRef dwContext As Long) As Boolean
    38. Private Declare Function FtpPutFile Lib "wininet.dll" Alias "FtpPutFileA" (ByVal hConnect As Long, ByVal lpszLocalFile As String, ByVal lpszNewRemoteFile As String, ByVal dwFlags As Long, ByVal dwContext As Long) As Boolean
    39. Private Declare Function InternetGetLastResponseInfo Lib "wininet.dll" Alias "InternetGetLastResponseInfoA" (lpdwError As Long, ByVal lpszBuffer As String, lpdwBufferLength As Long) As Boolean
    40. Private Declare Function FtpFindFirstFile Lib "wininet.dll" Alias "FtpFindFirstFileA" (ByVal hFtpSession As Long, ByVal lpszSearchFile As String, lpFindFileData As WIN32_FIND_DATA, ByVal dwFlags As Long, ByVal dwContent As Long) As Long
    41. Private Declare Function InternetFindNextFile Lib "wininet.dll" Alias "InternetFindNextFileA" (ByVal hFind As Long, lpvFindData As WIN32_FIND_DATA) As Long
    42. Const PassiveConnection As Boolean = True
    43. Private Sub Form_Load()
    44.     'KPD-Team 2000
    45.     'URL: [url]http://www.allapi.net[/url]
    46.     'E-Mail: [email][email protected][/email]
    47.     Dim hConnection As Long, hOpen As Long, sOrgPath  As String
    48.     'open an internet connection
    49.     hOpen = InternetOpen("API-Guide sample program", INTERNET_OPEN_TYPE_PRECONFIG, vbNullString, vbNullString, 0)
    50.     'connect to the FTP server
    51.     hConnection = InternetConnect(hOpen, "your ftp server", INTERNET_DEFAULT_FTP_PORT, "your login", "your password", INTERNET_SERVICE_FTP, IIf(PassiveConnection, INTERNET_FLAG_PASSIVE, 0), 0)
    52.     'create a buffer to store the original directory
    53.     sOrgPath = String(MAX_PATH, 0)
    54.     'get the directory
    55.     FtpGetCurrentDirectory hConnection, sOrgPath, Len(sOrgPath)
    56.     'create a new directory 'testing'
    57.     FtpCreateDirectory hConnection, "testing"
    58.     'set the current directory to 'root/testing'
    59.     FtpSetCurrentDirectory hConnection, "testing"
    60.     'upload the file 'test.htm'
    61.     FtpPutFile hConnection, "C:\test.htm", "test.htm", FTP_TRANSFER_TYPE_UNKNOWN, 0
    62.     'rename 'test.htm' to 'apiguide.htm'
    63.     FtpRenameFile hConnection, "test.htm", "apiguide.htm"
    64.     'enumerate the file list from the current directory ('root/testing')
    65.     EnumFiles hConnection
    66.     'retrieve the file from the FTP server
    67.     FtpGetFile hConnection, "apiguide.htm", "c:\apiguide.htm", False, 0, FTP_TRANSFER_TYPE_UNKNOWN, 0
    68.     'delete the file from the FTP server
    69.     FtpDeleteFile hConnection, "apiguide.htm"
    70.     'set the current directory back to the root
    71.     FtpSetCurrentDirectory hConnection, sOrgPath
    72.     'remove the direcrtory 'testing'
    73.     FtpRemoveDirectory hConnection, "testing"
    74.     'close the FTP connection
    75.     InternetCloseHandle hConnection
    76.     'close the internet connection
    77.     InternetCloseHandle hOpen
    78. End Sub
    79. Public Sub EnumFiles(hConnection As Long)
    80.     Dim pData As WIN32_FIND_DATA, hFind As Long, lRet As Long
    81.     'set the graphics mode to persistent
    82.     Me.AutoRedraw = True
    83.     'create a buffer
    84.     pData.cFileName = String(MAX_PATH, 0)
    85.     'find the first file
    86.     hFind = FtpFindFirstFile(hConnection, "*.*", pData, 0, 0)
    87.     'if there's no file, then exit sub
    88.     If hFind = 0 Then Exit Sub
    89.     'show the filename
    90.     Me.Print Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
    91.     Do
    92.         'create a buffer
    93.         pData.cFileName = String(MAX_PATH, 0)
    94.         'find the next file
    95.         lRet = InternetFindNextFile(hFind, pData)
    96.         'if there's no next file, exit do
    97.         If lRet = 0 Then Exit Do
    98.         'show the filename
    99.         Me.Print Left(pData.cFileName, InStr(1, pData.cFileName, String(1, 0), vbBinaryCompare) - 1)
    100.     Loop
    101.     'close the search handle
    102.     InternetCloseHandle hFind
    103. End Sub
    104. Sub ShowError()
    105.     Dim lErr As Long, sErr As String, lenBuf As Long
    106.     'get the required buffer size
    107.     InternetGetLastResponseInfo lErr, sErr, lenBuf
    108.     'create a buffer
    109.     sErr = String(lenBuf, 0)
    110.     'retrieve the last respons info
    111.     InternetGetLastResponseInfo lErr, sErr, lenBuf
    112.     'show the last response info
    113.     MsgBox "Error " + CStr(lErr) + ": " + sErr, vbOKOnly + vbCritical
    114. End Sub

    Cheers,

    RyanJ
    Last edited by sciguyryan; May 14th, 2005 at 05:40 AM.
    My Blog.

    Ryan Jones.

  3. #3
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Problem with FTP Rename

    No, do not change the ByVal call to ByRef (EDIT: Oh, I see that Ryan has changed his post). A VB string is passed as a pointer to a pointer so passing it ByVal means you're passing a pointer to the string and is the correct way of passing a string to an API function. Furthermore you don't have to check the LastDLLError unless the FtpRenameFile (or apiFtpRenameFile as you have renamed it) function returns 0 (or False) since it's only then an error can have occured. The function returns a BOOL which is not exactly the same as a VB Boolean but in your declaration you have specified that the function will return a Boolean. It will still work since VB will simply cast it as a boolean, however in the code you let a Long variable take care of the return value. You should use either a boolean or a long but you should make up your mind about it .

  4. #4
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    Re: Problem with FTP Rename

    Quote Originally Posted by Joacim Andersson
    No, do not change the ByVal call to ByRef (EDIT: Oh, I see that Ryan has changed his post).

    Yes, I noticed that - and as you said I changed it

    Thanks for pointing that out, I realized just before you posted


    Cheers,

    RyanJ
    My Blog.

    Ryan Jones.

  5. #5

    Thread Starter
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Amsterdam...
    Posts
    5,343

    Re: Problem with FTP Rename

    Thanks for the replies.

    Its making the error 2 (file not found I think in dll errors) for Renaming a file and for deleting a file. This, to me, suggests that windoze is (for some reason) looking for the file which has just gone although there is no handles for opening it held.

    As to the ByVal, byref bit I know what they mean, however it is unclear in MSDN as all their examples and descriptions are for C++ ... and not for vb/vba. Its just that if you put a byref and it should be byval it kills Access. An I was searching for info for vb stuff, which came across a site with declarations etc in vb code, but contradicted MSDN with the sub function and some of the references.

    Anyway, as it is actually deleting/renaming but erroring as a missing file, I've ignored the error. My main concern was that this does not impact further on in the program, but the Access as FTP is working fine now.

    At least as a single connection download. Not sure I should venture into the multiple connection downloads...

    Thanks for the AllApiNet example... Its one I used the first time around when the ftp openinternetfile for downloading etc didn't work, probably due to using the wrong flags.

    BTW - long or bolean won't matter. if it fails it will be 0 in either case. It only matters with the handle being returned

    BOFH Now, BOFH Past, Information on duplicates

    Feeling like a fly on the inside of a closed window (Thunk!)
    If I post a lot, it is because I am bored at work! ;D Or stuck...
    * Anything I post can be only my opinion. Advice etc is up to you to persue...

  6. #6
    I'm about to be a PowerPoster! Joacim Andersson's Avatar
    Join Date
    Jan 1999
    Location
    Sweden
    Posts
    14,649

    Re: Problem with FTP Rename

    Quote Originally Posted by Ecniv
    BTW - long or bolean won't matter. if it fails it will be 0 in either case. It only matters with the handle being returned
    It won't return a handle it only returns TRUE or FALSE. However TRUE is cast as 1 in the APIs not -1 as in VB. However VB will convert that for you. There is another difference and that is that the BOOL datatype is a 32bit integer (a VB long) while a VB boolean is a 16-bit integer.

    However don't check the last error unless the function returns false.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width