Question with FTP code

D

Darvin

I use the FTPClient by Stuart McCall to transfer files to an ftp site. I want
to transfer a file with a temporary "tmp" prefix, then when it's totally
uploaded I want to rename it by removing the prefix. Here's my code:

With New FTPClient
.OpenFTP
.OpenServer cstrServer, cstrUser, cstrPW
.PutFile cstrDir & "/" & cstrSubDir, "tmp" &
strFinalZippedName, strLocalDir & strFinalZippedName
.GetFileNames "", strFinalZippedName
If .FileNames.Count > 0 Then
.DelFile "", strFinalZippedName
Else
.SetDir "."
End If
.RenFile "tmp" & strFinalZippedName, strFinalZippedName
.CloseServer
.CloseFTP
End With

This code works fine to upload but I must be doing something wrong when I
try to delete any existing files so that I can rename the new upload.

Can anyone help me with this?

Thanks in advance
-Darvin
 
S

Stuart McCall

Darvin said:
I use the FTPClient by Stuart McCall to transfer files to an ftp site. I
want
to transfer a file with a temporary "tmp" prefix, then when it's totally
uploaded I want to rename it by removing the prefix. Here's my code:

With New FTPClient
.OpenFTP
.OpenServer cstrServer, cstrUser, cstrPW
.PutFile cstrDir & "/" & cstrSubDir, "tmp" &
strFinalZippedName, strLocalDir & strFinalZippedName
.GetFileNames "", strFinalZippedName
If .FileNames.Count > 0 Then
.DelFile "", strFinalZippedName
Else
.SetDir "."
End If
.RenFile "tmp" & strFinalZippedName, strFinalZippedName
.CloseServer
.CloseFTP
End With

This code works fine to upload but I must be doing something wrong when I
try to delete any existing files so that I can rename the new upload.

Can anyone help me with this?

Thanks in advance
-Darvin

Hi Darvin

Are you getting any error messages?


Try placing a breakpoint on the line:

If .FileNames.Count > 0 Then

Run the code till it breaks, then:

What does strFinalZippedName contain?
Also, what does .FileNames.Count contain?
 
D

Darvin

strFinalZippedName = 23959G0V48G.zip This is a compressed file that I'm
trying to upload.
..FileNames.Count = 2 This seems strange since at the most there should be
only one file already on the site (which I want to remove and then rename the
temporary file with the above file name)
..FileNames.Item(1) = "."
..FileNames.Item(2) = ".."

I haven't yet seen any error raised when I use the .PutFile - however I am
noticing that very often the zip file is corrupted after the upload.

An error gets raised when trying to .DelFile because there is actually no
strFinalZippedName to delete. Of course after one error the ftp gets closed
which is understandable.
On other occasions I've seen an error raised when running the .SetDir

Thanks
-Darvin
 
S

Stuart McCall

Darvin said:
strFinalZippedName = 23959G0V48G.zip This is a compressed file that I'm
trying to upload.
.FileNames.Count = 2 This seems strange since at the most there should be
only one file already on the site (which I want to remove and then rename
the
temporary file with the above file name)
.FileNames.Item(1) = "."
.FileNames.Item(2) = ".."

I haven't yet seen any error raised when I use the .PutFile - however I am
noticing that very often the zip file is corrupted after the upload.

An error gets raised when trying to .DelFile because there is actually no
strFinalZippedName to delete. Of course after one error the ftp gets
closed
which is understandable.
On other occasions I've seen an error raised when running the .SetDir

Hi Darvin

Sorry for the delay getting back to you. I've been in hospital for three
days (I tend to spend a lot of time in there these days - ho hum).

Re the corruption problem, do you have the following lines in the
declarations section of the class module? :

Private Const FTP_TRANSFER_TYPE_ASCII = &H0
Private Const FTP_TRANSFER_TYPE_BINARY = &H1

If so, you've ended up with an early, bugged version. If I sent this to you
(can't remember), I apologise. The bug is this: those values ought to be &H1
and &H2. So when you uploaded in binary mode, you were actually transmitting
the file in ascii mode.

Also in that early version I hadn't yet implemented a FileExists function,
which is really what you need, rather than using GetFileNames. So here it
is. Just paste the function onto the end of the class module:

Public Function FileExists(pRemoteDir, pFileName) As Boolean
'Returns True if FileName exists on server
Dim hFind As Long
Dim fData As WIN32_FIND_DATA
'
'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
m_RemoteDir = pRemoteDir
If Len(m_RemoteDir) = 0 Then m_RemoteDir = "."
SetDir pRemoteDir
fData.cFileName = String(MAX_PATH, 0)
hFind = FtpFindFirstFile(m_hCon, CStr(pFileName), fData, 0, 0)
FileExists = (hFind <> 0)
End Function

That, I think should get you going.
 
D

Darvin

Thank you very much! This was very helpful and solved the problem of the
corruption.

I like the "FileExists" function but for some reason it is always returning
True even if the file does not exist. Is there something else I'm doing wrong?

If .FileExists(".", strFinalZippedName) = True Then
.DelFile ".", strFinalZippedName
End If

I have one more question. Is it possible to track the upload and/or download
progress? I'd like to display a (relatively acurate) progress bar as the
upload or download takes place.

-Darvin
 
S

Stuart McCall

Darvin said:
Thank you very much! This was very helpful and solved the problem of the
corruption.

I like the "FileExists" function but for some reason it is always
returning
True even if the file does not exist. Is there something else I'm doing
wrong?

If .FileExists(".", strFinalZippedName) = True Then
.DelFile ".", strFinalZippedName
End If

No it's not you. Your code is perfectly ok. One thing I found when creating
this module (back in '96) is that ftp servers can differ in quirky ways that
are sometimes hard to track down. With that in mind, I've re-written the
FileExists method, to make it a little more 'defensive'. Replace the
function with this:

Public Function FileExists(pRemoteDir, pFileName) As Boolean
'Returns True if FileName exists on server
Dim hFind As Long
Dim fData As WIN32_FIND_DATA
Dim LastErr As Long
'
'Bail out if server connection not established
If m_hCon = 0 Then RaiseError errNotConnected
'
m_RemoteDir = pRemoteDir
If Len(m_RemoteDir) = 0 Then m_RemoteDir = "."
SetDir pRemoteDir
fData.cFileName = String(MAX_PATH, 0)
hFind = FtpFindFirstFile(m_hCon, CStr(pFileName), fData, 0&, 0&)
LastErr = Err.LastDllError
If hFind = 0 Then Exit Function
FileExists = (LastErr <> ERROR_NO_MORE_FILES)
End Function

I'll have to let you run tests on it because I have no access to an FTP
server at the moment. If you have problems could you please put a breakpoint
on the last line in the function and tell me:

a) if the code reaches this line
b} the value of LastErr
I have one more question. Is it possible to track the upload and/or
download
progress? I'd like to display a (relatively acurate) progress bar as the
upload or download takes place.

Yes its possible, but not with the module as it stands. I decided to go for
simplicity/maintainability rather than features/complexity with this module.
If you want to see what difference the necessary changes would make, take a
look at this:

http://www.mvps.org/access/downloads/InetTransferLib.zip
 
D

Darvin

The function still returned True even with a missing file. The value of
LastErr was 0.

P.S. Thanks also for the link. I'll check this out when I get a chance.

-Thanks
Darvin
 
S

Stuart McCall

Darvin said:
The function still returned True even with a missing file. The value of
LastErr was 0.

P.S. Thanks also for the link. I'll check this out when I get a chance.

-Thanks
Darvin


Hi Darvin

I managed to test the FileExists method using a friend's FTP server. It
works fine for me. Perhaps you'd better post some code. If you do, use
entries like 'MyFTPServer' as the server name and 'MyPassword' as the login
password.
 
D

Darvin

This is the code I'm running. Everything seems to work great except that the
FileExists function always returns True so it gives an error when trying to
delete a non-existent file. I'm using the revised FileExists function from
your post on 2/5/08.

With New FTPClient
.OpenFTP
.OpenServer MyFTPServer, MyUserName, MyPassword
.PutFile MyFTPDirectory, MyZippedUpload, "C:\" & MyZippedFile
If .FileExists(".", MyZippedFile) = True Then
.DelFile ".", MyZippedFile
End If
.RenFile MyZippedUpload, MyZippedFile
.CloseServer
.CloseFTP
End With

Thanks for you help. It is greatly appreciated!
-Darvin
 
S

Stuart McCall

Darvin said:
This is the code I'm running. Everything seems to work great except that
the
FileExists function always returns True so it gives an error when trying
to
delete a non-existent file. I'm using the revised FileExists function from
your post on 2/5/08.

With New FTPClient
.OpenFTP
.OpenServer MyFTPServer, MyUserName, MyPassword
.PutFile MyFTPDirectory, MyZippedUpload, "C:\" &
MyZippedFile
If .FileExists(".", MyZippedFile) = True Then
.DelFile ".", MyZippedFile
End If
.RenFile MyZippedUpload, MyZippedFile
.CloseServer
.CloseFTP
End With

Thanks for you help. It is greatly appreciated!
-Darvin

Hi Darvin

Well I pasted in your code, declared all the My stuff as strings, set the
values, tested it and it works perfectly for me <shrug>. This must be one of
those quirky server differences I mentioned. Unfortunately, until it breaks
down for me, there's little I can do to fix it.

I hate to leave things hanging like this :-(
 
D

Darvin

Thanks for looking into this as you did. I understand that it's verry
difficult finding an issue that one can't replicate. You've been a great help
anyway!

Thanks
-Darvin
 
S

Stuart McCall

Darvin said:
Thanks for looking into this as you did. I understand that it's verry
difficult finding an issue that one can't replicate. You've been a great
help
anyway!

Thanks
-Darvin

You're welcome. If I discover anything that will help, I'll let you know. Is
your email address legit once the obvious is removed?

For now, the workaround is to do the delete with error checking switched
off, so change this:

If .FileExists(".", MyZippedFile) = True Then
.DelFile ".", MyZippedFile
End If

to this:

On Error Resume Next
..DelFile ".", MyZippedFile
On Error GoTo 0

That way it doesn't matter whether the file exists or not.
 
C

cmraguilar

Stuart,

Can you provide a link to your FTP code. I tried a link from a post in
2006, but it had an error.

Thanks
 
C

cmraguilar

I have to say, that code is pretty sweet. I had to make a few changes to fit
my needs, but it works like a charm. Awesome job.
 
S

Stuart McCall

cmraguilar said:
I have to say, that code is pretty sweet. I had to make a few changes to
fit
my needs, but it works like a charm. Awesome job.

Thanks for your kind words. Much appreciated.

I hope it serves you well.
 
Top