FTP data
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Paul.
Anyone know how to send files via FTP using PureBasic.
I've gone through the PB Network commands but it doesn't look like they can do what I need...
- connect to FTP server
- change directories on FTP server
- retreive directory list
- delete a file
- send a file
- log off FTP server
Anyone have any ideas??
Anyone know how to send files via FTP using PureBasic.
I've gone through the PB Network commands but it doesn't look like they can do what I need...
- connect to FTP server
- change directories on FTP server
- retreive directory list
- delete a file
- send a file
- log off FTP server
Anyone have any ideas??
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Mr.Skunk.
It might very be interesting to have a library for FTP, like FTPSend()... commands.
But for know if you want to use FTP , you have to handle in your prog the FTP commands using the PB network commands (SendNetworkData and ReceiveNetworkData).
You can find a complete (i think) description of the FTP Protocol (rfc959) at this address :
ftp://ftp.nikhef.nl/pub/rfc/FTP/rfc959
And you can find help using the PB commands with FTP, with the "Atomic FTP Server" in the PB distrib.
Hope it can help you...
Mr Skunk
Mr Skunk's PureBasic Web Page
http://www.skunknet.fr.st
It might very be interesting to have a library for FTP, like FTPSend()... commands.
But for know if you want to use FTP , you have to handle in your prog the FTP commands using the PB network commands (SendNetworkData and ReceiveNetworkData).
You can find a complete (i think) description of the FTP Protocol (rfc959) at this address :
ftp://ftp.nikhef.nl/pub/rfc/FTP/rfc959
And you can find help using the PB commands with FTP, with the "Atomic FTP Server" in the PB distrib.
Hope it can help you...
Mr Skunk
Mr Skunk's PureBasic Web Page
http://www.skunknet.fr.st
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Paul.
I have looked at the Atomic FTP server and also the SendNetworkData/SendNetworkFile etc. and it looks like a PB app has to be running on the other end to receive the data. It doesn't seem to talk to a standard FTP program.
It would of course be great to have an FTP library of commands.
I have looked at the Atomic FTP server and also the SendNetworkData/SendNetworkFile etc. and it looks like a PB app has to be running on the other end to receive the data. It doesn't seem to talk to a standard FTP program.
It would of course be great to have an FTP library of commands.
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by fred.
Fred - AlphaSND
Are you sure of that ? I've tested Atomic FTP server with AceFTP 1.0 and it worked perfectly (at least the supported function). But I'm running Win2K and someone already told me there was some problem under Win9x/ME. Could you precise this ?I have looked at the Atomic FTP server and also the SendNetworkData/SendNetworkFile etc. and it looks like a PB app has to be running on the other end to receive the data. It doesn't seem to talk to a standard FTP program.
It would of course be great to have an FTP library of commands.
Fred - AlphaSND
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by TronDoc.
has anyone come up with a snippet that
will just login to an FTP server and
send a list of files to it?
I've done a long search and find lots
of questions, but no answer...
thanks,
Joe
elecTRONics DOCtor
{registeredPB}P150 32Mb w98/DOS/Linux NO DirX NO IE
has anyone come up with a snippet that
will just login to an FTP server and
send a list of files to it?
I've done a long search and find lots
of questions, but no answer...
thanks,
Joe
elecTRONics DOCtor
{registeredPB}P150 32Mb w98/DOS/Linux NO DirX NO IE
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Paul.
Here is how to send a file via FTP using Windows API calls.
Of course you must add your own personal data (username/password)
#INTERNET_SERVICE_FTP=1
#INTERNET_OPEN_TYPE_DIRECT=1
#FTP_PORT=21
#FTP_TRANSFER_ASCII=1
#FTP_TRANSFER_BINARY=2
Proxy.s=""
ProxyBypass.s=""
ServerName.s="http://www.ftpservername.com"
UserName.s="username"
Password.s="password"
localfile.s="filename.txt"
remotefile.s="filename.txt"
hInternet=InternetOpen_("FTP",#INTERNET_OPEN_TYPE_DIRECT,Proxy,ProxyBypass,0)
hConnect=InternetConnect_(hInternet,ServerName,#FTP_PORT,UserName,Password,#INTERNET_SERVICE_FTP,0,0)
If FtpPutFile_(hConnect,localfile,remotefile,#FTP_TRANSFER_ASCII,0)
MessageRequester("","File has been sent",0)
EndIf
InternetCloseHandle_(hInternet)
Here is how to send a file via FTP using Windows API calls.
Of course you must add your own personal data (username/password)
#INTERNET_SERVICE_FTP=1
#INTERNET_OPEN_TYPE_DIRECT=1
#FTP_PORT=21
#FTP_TRANSFER_ASCII=1
#FTP_TRANSFER_BINARY=2
Proxy.s=""
ProxyBypass.s=""
ServerName.s="http://www.ftpservername.com"
UserName.s="username"
Password.s="password"
localfile.s="filename.txt"
remotefile.s="filename.txt"
hInternet=InternetOpen_("FTP",#INTERNET_OPEN_TYPE_DIRECT,Proxy,ProxyBypass,0)
hConnect=InternetConnect_(hInternet,ServerName,#FTP_PORT,UserName,Password,#INTERNET_SERVICE_FTP,0,0)
If FtpPutFile_(hConnect,localfile,remotefile,#FTP_TRANSFER_ASCII,0)
MessageRequester("","File has been sent",0)
EndIf
InternetCloseHandle_(hInternet)
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by TronDoc.
elecTRONics DOCtor
{registeredPB}P150 32Mb w98/DOS/Linux NO DirX NO IE
I almost forgot. Once I use the API calls to log on,
can I just use the SNS() functions to send normal
FTP commands like ChangeDirectory?
I will try it on my own in the meantime,
I just don't want to irritate my ISP by
screwing up his server with bad commands...(?)
SendNetworkString(0, "CD somedirectory")
SendNetworkString(0, "CD nextdirectorydown")
Edited by - TronDoc on 18 May 2002 08:09:37
Many thanks Paul. I thought there was probably a way to do it with API, but I struggle with BASIC alone. Your help is greatly appreciated. -JoeI've learned a lot about Windows API calls since I first asked this question
(many thanks to Fred and others for answering many of my silly questions)
elecTRONics DOCtor
{registeredPB}P150 32Mb w98/DOS/Linux NO DirX NO IE
I almost forgot. Once I use the API calls to log on,
can I just use the SNS() functions to send normal
FTP commands like ChangeDirectory?
I will try it on my own in the meantime,
I just don't want to irritate my ISP by
screwing up his server with bad commands...(?)
SendNetworkString(0, "CD somedirectory")
SendNetworkString(0, "CD nextdirectorydown")
Edited by - TronDoc on 18 May 2002 08:09:37
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by tranquil.
SendNetworkString is a PB only command and can only be used if the server is written in PB too and uses the ReceiveNetworkString command to receive your clients data. Anyway, this command is not realy confortable and seems to be buggy sometimes. You should use SendNetworkData instead.
Mike
Tranquilizer/ Secretly!
Registred PureBasic User
SendNetworkString is a PB only command and can only be used if the server is written in PB too and uses the ReceiveNetworkString command to receive your clients data. Anyway, this command is not realy confortable and seems to be buggy sometimes. You should use SendNetworkData instead.
Mike
Tranquilizer/ Secretly!
Registred PureBasic User
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by TronDoc.
so if I use SendNetworkData(0, "CD somedirectory") the
normal FTP Server will change to 'somedirectory'?
I don't understand why the network commands were made non-standard...
using the above from Paul and adding the following after
making a good FTP connection I added:
MSGBuffer.s="cd trondoc"
MSGBufferLength=Len(MSGBuffer.s)
SendNetworkData(ClientID, *MSGBuffer.s, MSGBufferLength)
Then I get error: "InitNetwork() must be called and checked before use the network commands."
Edited by - TronDoc on 18 May 2002 10:37:16
so if I use SendNetworkData(0, "CD somedirectory") the
normal FTP Server will change to 'somedirectory'?
I don't understand why the network commands were made non-standard...
using the above from Paul and adding the following after
making a good FTP connection I added:
MSGBuffer.s="cd trondoc"
MSGBufferLength=Len(MSGBuffer.s)
SendNetworkData(ClientID, *MSGBuffer.s, MSGBufferLength)
Then I get error: "InitNetwork() must be called and checked before use the network commands."
Edited by - TronDoc on 18 May 2002 10:37:16
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by fweil.
...,
Just for some more adds to this topic, I am using much networking functions and components in my design and found out that PB has not exactly direct calls to protocol level. It's so but it works calling MS functions as you can find at http://msdn.microsoft.com/library/defau ... _entry.asp
Just it is uneasy to manage client / server exchange because of the requirement to open a tcp network connection with port attribute and the proposed PB functions do not exactly match with this.
One trick is maybe more simple to do for network design 'beginners' which I prefer to use for design clearance is to use an external ftp.exe program like the one you can find on any Windows installation (eg . in Winnt/system32). A regular ftp.exe accepts an batch file as an argument to be executed.
When you use such ftp.exe program you usually key in (after loading ftp.exe) :
open servername
username
password
[cd path]
get file ; or put file which reverse your data stream !
quit
If you enter all ftp commands in a temporary file then you can call :
ftp -s:tempfile
Note that you have to end your tempfile with quit to end it properly.
In a PB program you can build in easy to read and maintain code such :
;*******************************************************
; A regular ftp session should be like :
;
; File download from server to client File Upload from server to client
;
; ftp ServerName ftp ServerName
; username username
; password password
; [cd path] [cd path]
; [bin] [bin]
; get file put file
; quit quit
;
; It works everywhere. But just care about this : file may contain an access path in the get version, not in the put (think and you
; will understand the subtlety.
;
; This procedure and FTPRename should be replaced soon by an internal ftp procedure as soon as possible
;
Procedure FtpSession(Name.s, UserName.s, Password.s, Path.s, File.s, FunctionTag.s)
If UseConsole=1
Print(GetProcessTime$(StartTime)+" : ")
EndIf
If UseConsole=1
PrintN("Procedure FtpSession called")
EndIf
InitNetwork()
;
; If the network access to the server works
;
If OpenNetworkConnection(Name, 21)
;
; Create a temp file named ftps.tmp
; containing :
;
; open Name
; UserName
; Password
; [cd Path] if not empty
; bin
; get/put file
; quit
;
; (notice the EOL concatenation ...)
;
If CreateFile(0, "ftps.tmp")
WriteStringN("open "+Name+EOL+UserName+EOL+Password)
If Path ""
WriteStringN("cd "+Path)
EndIf
WriteStringN("bin"+EOL+FunctionTag+" "+File+EOL+"quit")
EndIf
CloseFile(0)
If UseConsole=1
PrintN(LoadTextFile$("ftps.tmp"))
EndIf
StatusMessage("Calling Ftp")
;
; And finally call the ftp client program file with the appropriate parameters
;
RunProgram(".\Ftp.exe", " -s:ftps.tmp", 1 | 2)
StatusMessage("Ftp closed")
status=1
Else
MessageRequester("Network error", "Server is not reachable", 0)
CloseNetworkConnection()
status=0
EndIf
If UseConsole=1
PrintN("Procedure FtpSession return")
EndIf
If UseConsole=1
Print(GetProcessTime$(StartTime)+" : ")
EndIf
ProcedureReturn status
EndProcedure
;*******************************************************
Maybe you will test this PB code, just cut and paste it but remind to declare EOL as chr(13)+chr(10) somewhere in your source code. I also put a timer for my needs which is not necessary. Here the temporary file is named ftps.tmp, which you will probably delete after use. And finally this example uses an ftp.exe copy in local directory that you can change for any pathaccess/ftp.exe.
So such a code is quite easier to maintain and to understand, and is rather short (if you cancel comments !).
For FTP clients process designers you have to know that if you want to really design ftp functions, you do have to know more about the commands to use.
As shown in Atomic FTP server, internal FTP commands are not like ie :
cd Path
get file
but :
CWD Path
STOR file
!!!
The difference is that when using a ftp client software component, you have a small command parser that translates user level commands in ftp regular commands.
To know more about you have to read the FTP protocol basic documents. It is also interesting just to use ftp.exe MS ftp client in debug mode (just key in debug) to get service messages with internal commands generated by user commands you give. Then you also have server responses. You will for example see that each session command has a 'literal' (internal level) equivalent, and each transfer command also, but for that last set of commands, each transfer starts with a PORT ftp command to tell the server what tcp port to use for next transfer. Which means that in a ftp session datagram you have to manage tcp ports dynamically to work fine.
Enjoy networking and tell me if more info needed.
Francois Weil
14, rue Douer
F64100 Bayonne
...,
Just for some more adds to this topic, I am using much networking functions and components in my design and found out that PB has not exactly direct calls to protocol level. It's so but it works calling MS functions as you can find at http://msdn.microsoft.com/library/defau ... _entry.asp
Just it is uneasy to manage client / server exchange because of the requirement to open a tcp network connection with port attribute and the proposed PB functions do not exactly match with this.
One trick is maybe more simple to do for network design 'beginners' which I prefer to use for design clearance is to use an external ftp.exe program like the one you can find on any Windows installation (eg . in Winnt/system32). A regular ftp.exe accepts an batch file as an argument to be executed.
When you use such ftp.exe program you usually key in (after loading ftp.exe) :
open servername
username
password
[cd path]
get file ; or put file which reverse your data stream !
quit
If you enter all ftp commands in a temporary file then you can call :
ftp -s:tempfile
Note that you have to end your tempfile with quit to end it properly.
In a PB program you can build in easy to read and maintain code such :
;*******************************************************
; A regular ftp session should be like :
;
; File download from server to client File Upload from server to client
;
; ftp ServerName ftp ServerName
; username username
; password password
; [cd path] [cd path]
; [bin] [bin]
; get file put file
; quit quit
;
; It works everywhere. But just care about this : file may contain an access path in the get version, not in the put (think and you
; will understand the subtlety.
;
; This procedure and FTPRename should be replaced soon by an internal ftp procedure as soon as possible
;
Procedure FtpSession(Name.s, UserName.s, Password.s, Path.s, File.s, FunctionTag.s)
If UseConsole=1
Print(GetProcessTime$(StartTime)+" : ")
EndIf
If UseConsole=1
PrintN("Procedure FtpSession called")
EndIf
InitNetwork()
;
; If the network access to the server works
;
If OpenNetworkConnection(Name, 21)
;
; Create a temp file named ftps.tmp
; containing :
;
; open Name
; UserName
; Password
; [cd Path] if not empty
; bin
; get/put file
; quit
;
; (notice the EOL concatenation ...)
;
If CreateFile(0, "ftps.tmp")
WriteStringN("open "+Name+EOL+UserName+EOL+Password)
If Path ""
WriteStringN("cd "+Path)
EndIf
WriteStringN("bin"+EOL+FunctionTag+" "+File+EOL+"quit")
EndIf
CloseFile(0)
If UseConsole=1
PrintN(LoadTextFile$("ftps.tmp"))
EndIf
StatusMessage("Calling Ftp")
;
; And finally call the ftp client program file with the appropriate parameters
;
RunProgram(".\Ftp.exe", " -s:ftps.tmp", 1 | 2)
StatusMessage("Ftp closed")
status=1
Else
MessageRequester("Network error", "Server is not reachable", 0)
CloseNetworkConnection()
status=0
EndIf
If UseConsole=1
PrintN("Procedure FtpSession return")
EndIf
If UseConsole=1
Print(GetProcessTime$(StartTime)+" : ")
EndIf
ProcedureReturn status
EndProcedure
;*******************************************************
Maybe you will test this PB code, just cut and paste it but remind to declare EOL as chr(13)+chr(10) somewhere in your source code. I also put a timer for my needs which is not necessary. Here the temporary file is named ftps.tmp, which you will probably delete after use. And finally this example uses an ftp.exe copy in local directory that you can change for any pathaccess/ftp.exe.
So such a code is quite easier to maintain and to understand, and is rather short (if you cancel comments !).
For FTP clients process designers you have to know that if you want to really design ftp functions, you do have to know more about the commands to use.
As shown in Atomic FTP server, internal FTP commands are not like ie :
cd Path
get file
but :
CWD Path
STOR file
!!!
The difference is that when using a ftp client software component, you have a small command parser that translates user level commands in ftp regular commands.
To know more about you have to read the FTP protocol basic documents. It is also interesting just to use ftp.exe MS ftp client in debug mode (just key in debug) to get service messages with internal commands generated by user commands you give. Then you also have server responses. You will for example see that each session command has a 'literal' (internal level) equivalent, and each transfer command also, but for that last set of commands, each transfer starts with a PORT ftp command to tell the server what tcp port to use for next transfer. Which means that in a ftp session datagram you have to manage tcp ports dynamically to work fine.
Enjoy networking and tell me if more info needed.
Francois Weil
14, rue Douer
F64100 Bayonne
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by TronDoc.
Francois:
ah. now I see. I was originally using ftp.exe and a batch file
with a script to do what I am now trying to do with PureBASIC.
at least with your example of having a PB program write the script (and then
deleting it), the password is not in a simple text file very long for
everyone to see.
although not totally secure, it is better than what I had.
I will study the references you gave, but I am pretty poor at the Windows
part of the BASIC programming so dynamically handling ports and such
sounds beyond me. I'm only currently writing Console stuff because that is
what I understand.
I thought CD was the same as CWD and PUT was the same as STOR,
but I will have to study the literals if I ever want to write
more than a script generator for FTP.
I guess I do not understand what the built in networking commands
are good for if you have to write both the sender and the receiver
for any application you have in mind.
And I still don't know why the error: "InitNetwork() must be called and checked before use the network commands." if I already have a network connection using
Paul's API calls.
Can someone give me an example of including PureBASIC's built in commands within
the framework of that API FTP send?
For example, where would the InitNetwork() go in Paul's API example and where
could I put the SendNetworkData(0, "CwD somedirectory", length) and
SendNetworkData(0, "STOR somefilename", length)literals.
In the meantime I will use Francois's script-writing example.
Thank you Francois.
Struggling through the WinDoze,
Joe
Francois:
ah. now I see. I was originally using ftp.exe and a batch file
with a script to do what I am now trying to do with PureBASIC.
at least with your example of having a PB program write the script (and then
deleting it), the password is not in a simple text file very long for
everyone to see.
although not totally secure, it is better than what I had.
I will study the references you gave, but I am pretty poor at the Windows
part of the BASIC programming so dynamically handling ports and such
sounds beyond me. I'm only currently writing Console stuff because that is
what I understand.
I thought CD was the same as CWD and PUT was the same as STOR,
but I will have to study the literals if I ever want to write
more than a script generator for FTP.
I guess I do not understand what the built in networking commands
are good for if you have to write both the sender and the receiver
for any application you have in mind.
And I still don't know why the error: "InitNetwork() must be called and checked before use the network commands." if I already have a network connection using
Paul's API calls.
Can someone give me an example of including PureBASIC's built in commands within
the framework of that API FTP send?
For example, where would the InitNetwork() go in Paul's API example and where
could I put the SendNetworkData(0, "CwD somedirectory", length) and
SendNetworkData(0, "STOR somefilename", length)literals.
In the meantime I will use Francois's script-writing example.
Thank you Francois.
Struggling through the WinDoze,
Joe
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by Paul.
Windows actually supplies all the commands you would need to do any FTP activity.
For example... to change to another folder use:
FTPSetCurrentDirectory_(hConnect,"foldername")
There are commands to list files, delete files, upload/download files, etc.
Visit msdn.microsoft.com to look up everything you need.
Windows actually supplies all the commands you would need to do any FTP activity.
For example... to change to another folder use:
FTPSetCurrentDirectory_(hConnect,"foldername")
There are commands to list files, delete files, upload/download files, etc.
Visit msdn.microsoft.com to look up everything you need.
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by fweil.
Yes it works fine with MS calls and an FTP process can be quite easy to build.
The only hard thing to do is to read / understand the MS documentation.
Here is a kind of template extending what Paul sent ...
;******************************************************
#INTERNET_SERVICE_FTP = 1
#INTERNET_SERVICE_HTTP = 3
#INTERNET_OPEN_TYPE_DIRECT = 1
#FTP_PORT = 21
#HTTP_PORT = 80
#FTP_TRANSFER_TYPE_ASCII = 1
#FTP_TRANSFER_TYPE_BINARY = 2
#FILE_ATTRIBUTE_NORMAL = 4
Procedure.s GetLastError_message(ErrorCode)
message.s = ""
Quit = 0
ipt = 0
SErrorCode.s = Str(ErrorCode)+" "
If ReadFile(0, "Windows_GetLastError_codes.mes")
While Eof(0) = 0 And Quit = 0
a$=ReadString()
If Left(a$, Len(SErrorCode)) = SErrorCode
message = a$
Quit = 1
EndIf
Wend
CloseFile(0)
If message = ""
message = "Undefined error code : "+Str(ErrorCode))
EndIf
Else
message = "Error codes library not found"
EndIf
ProcedureReturn message
EndProcedure
;
; Main starts here
;
OpenConsole()
Proxy.s=""
ProxyBypass.s=""
ServerName.s="DNSServerName"
UserName.s="useraccount"
Password.s="password"
localfile.s="yourlocalfile"
remotefile.s="yourremotefile"
;
; Testing FTP
;
hInternet=InternetOpen_("FTP",#INTERNET_OPEN_TYPE_DIRECT,Proxy,ProxyBypass,0))
hConnect=InternetConnect_(hInternet,ServerName,#FTP_PORT,UserName,Password,#INTERNET_SERVICE_FTP,0,0))
;
; Puts localfile to remotefile (is an upload) : Thanks to Paul for the
; template about this and the following.
;
If FtpPutFile_(hConnect,localfile,remotefile,#FTP_TRANSFER_TYPE_BINARY,0)
PrintN("File has been sent")
Else
PrintN("Failed : " + GetLastError_message(GetLastError_()))
EndIf
;
; Gets remotefile to localfile (is a download)
;
If FtpGetFile_(hConnect, remotefile, localfile, 0, #FILE_ATTRIBUTE_NORMAL, #FTP_TRANSFER_TYPE_BINARY, 0)
PrintN("File has been received")
Else
PrintN("Failed : " + GetLastError_message(GetLastError_()))
EndIf
;
; Changes the remote working directory
;
If FtpSetCurrentDirectory_(hConnect, "Path")
PrintN("Remote directory changed")
Else
PrintN("Failed : " + GetLastError_message(GetLastError_()))
EndIf
InternetCloseHandle_(hInternet)
While Inkey()=""
Wend
CloseConsole()
End
;******************************************************
This one as the other I proposed checks error conditions, but here at Windows level. It is not so much verbose but interesting just to be able to manage exceptions like when your network does not work (this is the reason why I often test the network connection ...
What interesting added in this post is the GetLastError_message procedure which checks the error code returned by Windows.
I use a file Windows_GetLastError_codes.mes I built formerly to have the human translation of error codes from Windows environment.
Just ask me if you want this file by email or on any ftp you want (makes 173KB actually).
So just to close this topic on my side, You should not need really more than the three functions shown here (download file, upload file and change directory) to manage anything you need. Just build a higher level if you have to manage sets of files. And remind that this Windows calls can assume networking conditions like a proxy or RAS access to open a session.
Francois Weil
14, rue Douer
F64100 Bayonne
Yes it works fine with MS calls and an FTP process can be quite easy to build.
The only hard thing to do is to read / understand the MS documentation.
Here is a kind of template extending what Paul sent ...
;******************************************************
#INTERNET_SERVICE_FTP = 1
#INTERNET_SERVICE_HTTP = 3
#INTERNET_OPEN_TYPE_DIRECT = 1
#FTP_PORT = 21
#HTTP_PORT = 80
#FTP_TRANSFER_TYPE_ASCII = 1
#FTP_TRANSFER_TYPE_BINARY = 2
#FILE_ATTRIBUTE_NORMAL = 4
Procedure.s GetLastError_message(ErrorCode)
message.s = ""
Quit = 0
ipt = 0
SErrorCode.s = Str(ErrorCode)+" "
If ReadFile(0, "Windows_GetLastError_codes.mes")
While Eof(0) = 0 And Quit = 0
a$=ReadString()
If Left(a$, Len(SErrorCode)) = SErrorCode
message = a$
Quit = 1
EndIf
Wend
CloseFile(0)
If message = ""
message = "Undefined error code : "+Str(ErrorCode))
EndIf
Else
message = "Error codes library not found"
EndIf
ProcedureReturn message
EndProcedure
;
; Main starts here
;
OpenConsole()
Proxy.s=""
ProxyBypass.s=""
ServerName.s="DNSServerName"
UserName.s="useraccount"
Password.s="password"
localfile.s="yourlocalfile"
remotefile.s="yourremotefile"
;
; Testing FTP
;
hInternet=InternetOpen_("FTP",#INTERNET_OPEN_TYPE_DIRECT,Proxy,ProxyBypass,0))
hConnect=InternetConnect_(hInternet,ServerName,#FTP_PORT,UserName,Password,#INTERNET_SERVICE_FTP,0,0))
;
; Puts localfile to remotefile (is an upload) : Thanks to Paul for the
; template about this and the following.
;
If FtpPutFile_(hConnect,localfile,remotefile,#FTP_TRANSFER_TYPE_BINARY,0)
PrintN("File has been sent")
Else
PrintN("Failed : " + GetLastError_message(GetLastError_()))
EndIf
;
; Gets remotefile to localfile (is a download)
;
If FtpGetFile_(hConnect, remotefile, localfile, 0, #FILE_ATTRIBUTE_NORMAL, #FTP_TRANSFER_TYPE_BINARY, 0)
PrintN("File has been received")
Else
PrintN("Failed : " + GetLastError_message(GetLastError_()))
EndIf
;
; Changes the remote working directory
;
If FtpSetCurrentDirectory_(hConnect, "Path")
PrintN("Remote directory changed")
Else
PrintN("Failed : " + GetLastError_message(GetLastError_()))
EndIf
InternetCloseHandle_(hInternet)
While Inkey()=""
Wend
CloseConsole()
End
;******************************************************
This one as the other I proposed checks error conditions, but here at Windows level. It is not so much verbose but interesting just to be able to manage exceptions like when your network does not work (this is the reason why I often test the network connection ...
What interesting added in this post is the GetLastError_message procedure which checks the error code returned by Windows.
I use a file Windows_GetLastError_codes.mes I built formerly to have the human translation of error codes from Windows environment.
Just ask me if you want this file by email or on any ftp you want (makes 173KB actually).
So just to close this topic on my side, You should not need really more than the three functions shown here (download file, upload file and change directory) to manage anything you need. Just build a higher level if you have to manage sets of files. And remind that this Windows calls can assume networking conditions like a proxy or RAS access to open a session.
Francois Weil
14, rue Douer
F64100 Bayonne
-
BackupUser
- PureBasic Guru

- Posts: 16777133
- Joined: Tue Apr 22, 2003 7:42 pm
Restored from previous forum. Originally posted by TronDoc.
Yes. Thank you very much. I think there is more than enough here
to do what I need to now. You are correct about Micro$oft documentation.
My eyes started to glaze over when I was reading it hoping to find
what I needed. I'm also thankful for the getlasterror procedure.
I will let you know if I ever need the human-readable file.
Sincerely,
Joe Block
Yes. Thank you very much. I think there is more than enough here
to do what I need to now. You are correct about Micro$oft documentation.
My eyes started to glaze over when I was reading it hoping to find
what I needed. I'm also thankful for the getlasterror procedure.
I will let you know if I ever need the human-readable file.
Sincerely,
Joe Block