Page 2 of 2

Posted: Tue Jan 29, 2008 6:42 pm
by Xombie
afriend - sorry, I don't know how that made it in there. It's not in my saved version so I must have been testing something else and left it in.

It doesn't work for me. I've tried various FTP packages including the basic Windows FTP program and they all work. It's just that for some reason, this won't work for me. I have not tested with 4.20 beta 2 yet.

Posted: Tue Jan 29, 2008 9:37 pm
by TerryHough
Here is afriend's code expanded and tweaked a bit. Still not much success, but I have posted a couple of screenshots following the code.

Read the remark lines in the code for the results I get on various commands. None of this is very repeatable.

Code: Select all

;*************************************************************** 
;Example by P Dixon ala Afriend 
;25 Jan 2008 
; Extended and modified TerryHough
;*************************************************************** 
; 
;FTP Commands available - note original text file had "String" or "StringString" or StringStringString" 
;appended to some of the commands. I believe these were in error but stand to be corrected: 
; 
;CloseFTP(#Ftp) - Close the specified client connection And send a notification To the server. 
;CreateFTPDirectory(#Ftp ,Directory$) - Create a new directory on the FTP server. 
;DeleteFTPDirectory(#Ftp, Directory$) - Delete a directory on the FTP server. 
;DeleteFTPFile(#Ftp ,Filename$) - Delete a file on the FTP server. 
;ExamineFTPDirectory(#Ftp ,Directory$) - Examine the current directory on the FTP server. 
;GetFTPDirectory(#Ftp) - Return the current directory. 
;FinishFTPDirectory (#Ftp) - Free the Data allocated With ExamineFTPDirectory. 
;FTPDirectoryEntryAttributes (#Ftp) - Return the attributes of the current entry in the last FTP listing. 
;FTPDirectoryEntryDate (#Ftp) - Return the modified date of the current entry in the last FTP listing. 
;FTPDirectoryEntryName (#Ftp) - Return the name of the current entry in the last FTP listing. 
;FTPDirectoryEntrySize (#Ftp) - Return the size of the current entry in the last FTP listing. 
;FTPDirectoryEntryType (#Ftp) - Return the type of the current entry in the last FTP listing. 
;FTPProgress(#Ftp) - Return the progress on the current transfer. 
;IsFTP(#Ftp) - Tests If the given '#Ftp' is an initialized FTP object. 
;NextFTPDirectoryEntry(#Ftp) - Go To the Next FTP directory entry 
;OpenFTP(#Ftp ,ServerName$, User$, Password$ [Passive [Port]]) - Try To open a connection on the specified server. 
;ReceiveFTPFile(#Ftp, RemoteFilename$, Filename$ [Asynchronous]) - Receive a file from a FTP Server. 
;RenameFTPFile(#Ftp, Filename, $NewFilename$) - Rename Or move a file on the FTP server. 
;SendFTPFile(#Ftp, Filename$, RemoteFilename$ [Asynchronous]) - Send a file To a FTP Server. 
;SetFTPDirectory(#Ftp, Directory$) - Change the current directory. 
; 
;Using the above modified commands works quite well. 
; 
; 

Global Msg$ = ""


Procedure FTP_Results()
  ; Follow file transfer progress until end or failure  
  ; As near as I can make out FTPProgress(0) 
  ; -1 = in progress transmit file 
  ; -2 = file not found or some error 
  ; -3 = file transmited/received - function complete successful? 
  ; so you could put better traps/filters in but this is just a demo 
  Repeat 
    Delay(300) 
    Result = FTPProgress(0) 
  Until Result = -3 Or Result = -2

  Select Result
    Case -1
      ; Indicates FTP action still in progress
      ;Msg$ + "Send: Got a -1 result - " + #LF$
    Case -2
      ;Msg$ + "Failed to send/receive file: " + #LF$
      ProcedureReturn #False
    Case -3  
      ;Msg$ + "Successfully sent/received file: " + #LF$
      ProcedureReturn #True
  EndSelect
EndProcedure

;Host parameters fictional for exercise - substitute your own legitimate parameters 
IPAddress.s = "ftp.server.somewhere"    
Username.s  = "USERNAME" 
Password.s  = "PASSWORD" 


;First initialize the network 
InitNetwork() 

;now open ftp 
If OpenFTP(0, IPAddress, Username, Password) 
  ; The FTP session has been accepted by the FTP Server
  SetFTPDirectory(0,"..")

  ; Now, create a directory on the server
  ; CreateFTPDirectory(0,"TESTDIR")
  ; That worked in initial test

  ; Now, delete a directory on the server
  ; DeleteFTPDirectory(0,"TESTDIR")
  ; That worked in initial test

  ; Choose and set the remote directory
  Directoryname$ = "\RECEIVE"  ; a valid directory with permissions on the server
  Result = SetFTPDirectory(0, Directoryname$)
  ; The SetFTPDirectory is received at the server and acted upon.
  Delay(300)  ; tried various delays up to 5 seconds
  Msg$ + "Current remote directory: " + GetFTPDirectory(0) + #LF$
  ; The GetFTPDirectory is received at the server and replied to, but the current directory name
  ; is not returned from the command.  (See same command later on.)
  
  ; Ready to send a file      
  Filename$ = "NCSec529.pdf"   ; valid file stored in the root directory of Drive C:
  Msg$ + #LF$ + "Send a file to the server: " + Filename$
  SendFTPFile(0, "C:\" + Filename$, Filename$, 1)
  If FTP_Results() 
    Msg$ + " - Successful" + #LF$
  Else
    Msg$ + " - Failed" + #LF$
  EndIf
  ; I have not been successful sending any file yet.  The server never receives any request to store a file.  
  
  SetFTPDirectory(0,"\")  ; Set back to root directory of server session
  Msg$ + #LF$
  
  ; Now see if we can examine a directory
  Directoryname$ = "\SEND\SENT"  ; a valid directory with permissions on the server
  SetFTPDirectory(0, Directoryname$) ; change to the desired directory
  Delay(300)
  Msg$ + "Current remote directory: " + GetFTPDirectory(0) + #LF$
  ; This GetFTPDirectory works as expected and returns the proper information
  
  ExamineFTPDirectory(0)
  Delay(300)
  Msg$ + #LF$ + "Examine directory: " + Directoryname$ + #LF$
  Msg$ + "--------------------------------------------" + #LF$
  While NextFTPDirectoryEntry(0)
    Msg$ + FTPDirectoryEntryName(0) + #TAB$
    Msg$  + #TAB$ + FormatDate("%mm/%dd;%yyyy   %hh:%ii:%ss", FTPDirectoryEntryDate(0)) + #TAB$
    Msg$ + RSet(Str(FTPDirectoryEntrySize(0)),10) + "  " + #TAB$
    Msg$ + Str(FTPDirectoryEntryType(0)) + #LF$
  Wend
  Msg$ + "--------------------------------------------" + #LF$
  FinishFTPDirectory(0) 
  Msg$ + "Done examining the directory" + #LF$
  Delay(300)

  Directoryname$ = "\SEND\SENT"  ; a valid directory with permissions on the server
  ;Now get file from host and store on local computer to ensure file transfer OK
  SetFTPDirectory(0, Directoryname$) ; change to the desired directory
  Delay(300)
  Filename$ = "Purebasic.chm" ; Valid file name stored in the requested directory on the server
  Msg$ + #LF$ + "Receive file from server: " + Filename$
  DeleteFile("C:\" + Filename$) ; delete local copy of file
  Delay(300)
  ReceiveFTPFile(0, Filename$, "C:\" + Filename$, 1)
  If FTP_Results() 
    Msg$ + " - Successful" + #LF$
  Else
    Msg$ + " - Failed" + #LF$
  EndIf
  ; The above file is received successfully.  But only if the ExamineDirectory routine above
  ; is not used.
    
  ; Reset the directory to root
  SetFTPDirectory(0,"\")
  Delay(300)
  Msg$ + "Current remote directory: " + GetFTPDirectory(0) + #LF$

  ; Delete a file from the root directory
  Filename$ = "testtext"
  Msg$ + #LF$ + "Delete a file on the server: " + Filename$
  If DeleteFTPFile(0 , Filename$) 
    Msg$ + " - Successful" + #LF$
  Else
    Msg$ + " - Failed" + #LF$
  EndIf 
  ; An existing file was deleted successfully.  If non-existing file, it still returns successful.
  
  ; Close the FTP session
  CloseFTP(0) 

Else 
  ; Network could not be initialized
  Msg$ + "Network initialization failed." + #LF$
  
EndIf 
MessageRequester("FTP Library", Msg$, #MB_ICONINFORMATION)
End
Here is the results when the ExamineDirectory works:
Image

And here is an example when the ExamineDirectory code was removed and the ReceiveFTPFile actually worked:
Image

Re: ftp Library

Posted: Sun Sep 29, 2024 9:58 pm
by Randy Walker
afriend wrote: Fri Jan 25, 2008 6:09 am

Code: Select all

  Repeat
    Debug FTPProgress(0)
    Delay(300)
  Until FTPProgress(0) = -3 Or FTPProgress(0) = -2
I found this little modification much more effective:

Code: Select all

    Repeat
      j = FTPProgress(0)
      Debug j
      Delay(300)
    Until j = #PB_FTP_Finished Or j =  #PB_FTP_Error
I've encountered a problem with my plain ReceiveFTPFile not receiving a rather large file. (15101KB) Maybe too big for PB to handle??? Anyway I switched to Asynchronous ReceiveFTPFile so I could use FTPProgress to monitor the FTP activity, But I get immediate -1 and -2 so it's failing to download and file size is zero on the file it did create. Big mystery to me, as most things are these days. Is there a file size limit for ReceiveFTPFile?

Re: ftp Library

Posted: Sun Sep 29, 2024 11:59 pm
by Randy Walker
Randy Walker wrote: Sun Sep 29, 2024 9:58 pm
afriend wrote: Fri Jan 25, 2008 6:09 am

Code: Select all

  Repeat
    Debug FTPProgress(0)
    Delay(300)
  Until FTPProgress(0) = -3 Or FTPProgress(0) = -2
I found this little modification much more effective:

Code: Select all

    Repeat
      j = FTPProgress(0)
      Debug j
      Delay(300)
    Until j = #PB_FTP_Finished Or j =  #PB_FTP_Error
I've encountered a problem with my plain ReceiveFTPFile not receiving a rather large file. (15101KB) Maybe too big for PB to handle??? Anyway I switched to Asynchronous ReceiveFTPFile so I could use FTPProgress to monitor the FTP activity, But I get immediate -1 and -2 so it's failing to download and file size is zero on the file it did create. Big mystery to me, as most things are these days. Is there a file size limit for ReceiveFTPFile?
Could not make my way back to my original post to add an edit so I am posting a reply to explain I found a solution to my file too big problem, maybe not the best solution but it worked. I prefaced my ReceiveFTPfile line with the ExamineFTPDirectory(0) and looped through the server files until I hit the file giving me grief and then I did the ReceiveFTPfile command. Does not seem like this extreme measure should be necessary, but it worked and I hate it because I knew the file was there without having to search for it. ExamineFTPDirectory should not be required to do ReceiveFTPfile.