PDA

View Full Version : Ftp get



TheOne
09-04-2011, 00:48
Hi,

I didn't see any FTP forum so I hope it is ok to post to the INET forum (closest one in type). ;)

I was modifying your FTP sample.
I was able to connect to my IP address and login.
Able to change to my local directory, C:\
Able to change my server/remote directory, /tmp

Then normal in DOS, I would use GET filename newfilename (ex: GET TBT TBT.txt) and it would get the file and put it in my C: drive.

So I copied the sample and used ftpresult = FTP_GetFile(TBT, "Async")

But I didn't see any TBT file in my C: drive. Do I do something wrong?

Second question: is there a way to rename the "get" file to add the .txt extension?

Last question: I was thinking of having the user input their id and password. Right now it is hardcoded in the script. Is there a way to suppress the display of the password when they type it in via the CONSOLE module?

Thanks. This is probably basic (no pun intended :D) to you guys.

PS: I was displaying the FTP GETFILE and it is 0. And displaying for the FTP FINISHED and it was 910.

Michael Clease
09-04-2011, 02:04
It might be easier if you posted the code, just remove your login details and replace them.

Here is site and file you could use

ftp://ftp.bbc.co.uk/bin/tcf.gif

or for a list visit

http://www.ftp-sites.org/

TheOne
11-04-2011, 14:08
Hi Michael,

This was taken mostly from the sample in the FTP folder. Just slightly modified.


Uses "FTP" 'Script uses FTP module
Uses "Console"

'-------------------------------------------------------------------------
'Variable declaration
'-------------------------------------------------------------------------
DIM ftpResult AS number
DIM ftpConnect AS number
Dim LocalDir As String
DIM LocalDirList AS STRING
DIM ServerDir AS STRING
DIM ServerDirList AS STRING
Dim FileName As String
Dim FtpServer As String Value "x"
Dim FtpUser As String Value "x"
Dim FtpPass As String Value "x"
Dim fBytesIn As Number



'-------------------------------------------------------------------------
'Start FTP connection
'-------------------------------------------------------------------------

'---Set a Log file so we can check later
'---LOG file name has the same name of source file plus log ext
ftpResult = FTP_SetLogFile(APP_SOURCENAME + ".Log")
IF ftpResult < 0 THEN
Console_Write("failed to set up log file")
ELSE
Console_Write("Logfile set up ok" + $CRLF)
END IF

'---Now try to connect to FTP site
ftpConnect = FTP_Connect(FtpServer, FtpUser, FtpPass)
IF ftpConnect < 0 THEN
Stop
END IF

'---Set local directory
LocalDir = "C:\"
ftpResult = FTP_SetLocalDir(LocalDir)
IF ftpResult >= 0 THEN
Console_Write("C: directory is local directory" + $CRLF)
ELSE
Console_Write("Error setting to C: directory" + $CRLF)
END IF

'---Set directory on the server
ServerDir = "/tmp"
ftpResult = FTP_SetServerDir(ServerDir)
IF ftpResult >= 0 THEN
Console_Write("Directory /tmp found" + $CRLF)
ELSE
Console_Write("Directory /tmp is not found" + $CRLF)
END IF


'
' '-------------------------------------------------------------------------
' 'Start DownLoad process
' '-------------------------------------------------------------------------

FileName = "TBT"
ftpResult = FTP_GetFile("TBT","ASYNC")
Console_Write("Get result code is: " + ftpResult + $CRLF)
ftpResult = FTP_Finished
Console_Write("FTP finished is: " + ftpResult + $CRLF)

'-------------------------------------------------------------------------
'Close FTP Connection
'-------------------------------------------------------------------------
IF ftpConnect >= 0 THEN
ftpResult = FTP_Quit
EndIf

WaitKey

Michael Clease
11-04-2011, 14:41
I made some bits more generic and add a loop for the download.

It looks like FTP_FINISHED is a byte counter for the number of bytes left to download.




Uses "FTP" 'Script uses FTP module
Uses "Console"

'-------------------------------------------------------------------------
'Variable declaration
'-------------------------------------------------------------------------
Dim ftpResult As Number
Dim ftpConnect As Number
Dim LocalDir As String
Dim LocalDirList As String
Dim ServerDir As String
Dim ServerDirList As String
Dim FileName As String
Dim FtpServer As String Value "x"
Dim FtpUser As String Value "x"
Dim FtpPass As String Value "x"
Dim fBytesIn As Number


'-------------------------------------------------------------------------
'Start FTP connection
'-------------------------------------------------------------------------

'---Set a Log file so we can check later
'---LOG file name has the same name of source file plus log ext
ftpResult = FTP_SetLogFile(APP_SourceName + ".Log")
If ftpResult < 0 Then
Console_Write("failed to set up log file")
Else
Console_Write("Logfile set up ok" + $CRLF)
End If

'---Now try to connect to FTP site
ftpConnect = FTP_Connect(FtpServer, FtpUser, FtpPass)
If ftpConnect < 0 Then
MsgBox 0, "Login Error - Check username / Password"
Stop
End If

'---Set local directory
LocalDir = "C:\"
ftpResult = FTP_SetLocalDir(LocalDir)
If ftpResult >= 0 Then
Console_Write("C: directory is local directory" + $CRLF)
Else
Console_Write("Error setting to C: directory" + $CRLF)
End If

'---Set directory on the server
ServerDir = "/tmp"
ftpResult = FTP_SetServerDir(ServerDir)
If ftpResult >= 0 Then
Console_Write("Directory " + Chr$(34) + ServerDir + Chr$(34) +" found" + $CRLF)
Else
Console_Write("Directory " + Chr$(34) + ServerDir + Chr$(34) +" not found" + $CRLF)
End If
'
' '-------------------------------------------------------------------------
' 'Start DownLoad process
' '-------------------------------------------------------------------------

FileName = "TBT"
ftpResult = FTP_GetFile(FileName,"SYNC")
Console_Write("Get result code is: " + ftpResult + $CRLF)
Do
DoEvents
Loop Until FTP_Finished = 0 ' Loop until end of file reached

ftpResult = FTP_Finished
Console_Write("FTP finished is: " + ftpResult + $CRLF)

'-------------------------------------------------------------------------
'Close FTP Connection
'-------------------------------------------------------------------------
If ftpConnect >= 0 Then
ftpResult = FTP_Quit
EndIf

WaitKey



ps. Eros, If you get five minutes could you add a little bit of detail to some of the commands...please..

TheOne
11-04-2011, 16:34
Eureka! Thanks Michael.

I didn't realize you needed a DO LOOP for the file to finish "downloading". Originally I copied the WHILE loop from the example. And it downloaded ok. But tried again later and it didn't. Thanks for the explanation about the FTP_FINISHED.

Is there a way to have the file named as a .txt when it is downloaded?

Also, can thinBasic suppress input/typing?

Thank for all your help and knowledge! :D

Michael Clease
11-04-2011, 16:44
I would rename the file after downloading, a user wouldn't see this operation.

USES "FILE"

n = FILE_Rename(Old_FileName, New_FileName)

as for suppressing input I think Charles looked at this a while back but I cant remember what the result was ??? Simple answer I dont know but if anything comes to mind I will let you know.


Mike C.

TheOne
11-04-2011, 18:03
Thanks Michael.

I didn't even think to look at the File help manual section. I was concentrating on the FTP help manual section.

ErosOlmi
11-04-2011, 18:08
Thanks Michael.

I didn't even think to look at the File help manual section. I was concentrating on the FTP help manual section.

Well, I'm very sorry I didn't finish FTP_... manual reference.
I will do it for the next update (http://www.thinbasic.com/community/project.php?issueid=271).

Eros

TheOne
11-04-2011, 19:57
Hi Eros,

You have done a fantastic job with the help manual.

Anything not there, I guess that's what the forum is for. :)

Believe me, without you guys, I would be stuck and banging my poor head on the walls.

ErosOlmi
08-05-2011, 22:00
I've just published thinBasic beta 1.8.7

It has a new complete help for FTP keywords. I've also rewritten almost entirely FTP module and now it has no dependency external dlls.