Windows only FTP routines...
Posted: Wed Jan 31, 2007 3:47 am
This is some routines I've put together to do ftp transfer on a windows system. Loads of people on the forums deserve some credit, there are too many to mention and they know who they are!
If anyone wants to help expand it into an full ftp library - with gadget callbacks, etc then please contribute.
Most standard ftp parameters are optional, defaulting to the most common. Eg, port=21
As usual with my code, there is no comments - but its fairly obvious what does what.
If anyone wants to help expand it into an full ftp library - with gadget callbacks, etc then please contribute.
Most standard ftp parameters are optional, defaulting to the most common. Eg, port=21
As usual with my code, there is no comments - but its fairly obvious what does what.
Code: Select all
Global ftp_hopen,ftp_hconnect
Procedure FtpCallback(handle,context,status,info,len)
If context
Debug("# "+Str(context)+" - "+Str(status)+" - "+Str(info)+" - "+Str(len))
EndIf
EndProcedure
Procedure FtpOpen(server$,user$,password$,port=21,passive=0,ftpname$="FTP",gad=0)
result=#False
ftp_hopen=InternetOpen_(ftpname$,1,"","",0)
If ftp_hopen
ftp_hcallback=InternetSetStatusCallback_(ftp_hopen,@FtpCallback())
ftp_hconnect=InternetConnect_(ftp_hopen,server$,port,user$,password$,1,passive,gad)
If ftp_hconnect
result=#True
Else
InternetCloseHandle_(ftp_hopen)
ftp_hopen=0
EndIf
EndIf
ProcedureReturn result
EndProcedure
Procedure.s FtpDir(dir$="*.*")
result$=""
dir$=Trim(dir$)
If ftp_hconnect
found=FtpFindFirstFile_(ftp_hconnect,dir$,@file.WIN32_FIND_DATA,0,0)
If found
Repeat
attrib=PeekL(@file)
writel=PeekL(@file+20)
writeh=PeekL(@file+24)
sizeh=PeekL(@file+28)
sizel=PeekL(@file+32)
filename$=PeekS(@file+44)
result$+Trim(filename$)+"|"
result$+RSet(Bin(attrib),16,"0")+"|"
result$+RSet(Hex(writeh),8,"0")+RSet(Hex(writel),8,"0")+"|"
result$+Str(sizel+1024*sizeh)+"#"
Until Not InternetFindNextFile_(found,@file)
EndIf
EndIf
ProcedureReturn result$
EndProcedure
Procedure FtpSetPath(dir$="")
result=#False
If ftp_hconnect
result=FtpSetCurrentDirectory_(ftp_hconnect,dir$)
EndIf
ProcedureReturn result
EndProcedure
Procedure FtpCreateDirectory(dir$)
result=#False
If ftp_hconnect
dir$=Trim(dir$)
If dir$<>""
result=FtpCreateDirectory_(ftp_hconnect,dir$)
EndIf
EndIf
ProcedureReturn result
EndProcedure
Procedure FtpDeleteDirectory(dir$)
result=#False
If ftp_hconnect
dir$=Trim(dir$)
If dir$<>""
result=FtpRemoveDirectory_(ftp_hconnect,dir$)
EndIf
EndIf
ProcedureReturn result
EndProcedure
Procedure FtpDeleteFile(file$)
result=#False
If ftp_hconnect
file$=Trim(file$)
If file$<>""
result=FtpDeleteFile_(ftp_hconnect,file$)
EndIf
EndIf
ProcedureReturn result
EndProcedure
Procedure FtpRenameFile(source$,dest$)
result=#False
If ftp_hconnect
source$=Trim(source$)
dest$=Trim(dest$)
If source$<>"" And dest$<>""
result=FtpRenameFile_(ftp_hconnect,source$,dest$)
EndIf
EndIf
ProcedureReturn result
EndProcedure
#ftp_unknown=0
#ftp_ascii=1
#ftp_binary=2
Procedure FtpDownload(source$,dest$="",mode=#ftp_binary,gad=0)
result=#False
If ftp_hconnect
source$=Trim(source$)
If source$<>""
If dest$=""
dest$=source$
EndIf
result=FtpGetFile_(ftp_hconnect,source$,dest$,0,0,mode,gad)
EndIf
EndIf
ProcedureReturn result
EndProcedure
Procedure FtpUpload(source$,dest$="",mode=#ftp_binary,gad=0)
result=#False
If ftp_hconnect
source$=Trim(source$)
If source$<>""
If dest$=""
dest$=source$
EndIf
result=FtpPutFile_(ftp_hconnect,source$,dest$,mode,gad)
EndIf
EndIf
ProcedureReturn result
EndProcedure
Procedure FtpClose()
If ftp_hconnect
InternetCloseHandle_(ftp_hconnect)
InternetCloseHandle_(ftp_hopen)
EndIf
EndProcedure