FTP file listing

Just starting out? Need help? Post your questions and find answers here.
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

FTP file listing

Post by J@ckWhiteIII »

Hey,
I'm trying to code an app to access a FTP server, upload/download files. Somehow, with PB's FTP commands it seems to be a bit limited, and what I tried (upload/download) did not work properly.
1st: Listing the files.
In PB, you can examine the FTP directory and list the entries. As I want to avoid PB's built in FTP commands, I tried to do it with just network commands. A try to connect on the server and get a list of the entries in the FTP directory:

Code: Select all

Procedure OpenFTPConnection(Servername.s,User.s,Password.s)
  Protected.s Reply,Reply2
  Protected.i Connection
  Connection = OpenNetworkConnection(Servername,21)
  *Mem = AllocateMemory (5000)
  If ReceiveNetworkData(Connection,*Mem,5000)
    Reply = PeekS(*Mem)
    If FindString(Reply,"ready")                                                           ; CHECK
      SendNetworkString(Connection,"USER " + User + #LFCR$)
      FreeMemory (*Mem)
      Debug Reply
      *Mem = AllocateMemory(5000)
      If ReceiveNetworkData(Connection,*Mem,5000)
        Reply = PeekS(*Mem)
        If FindString(Reply,"okay")                                                      ;CHECK
          SendNetworkString(Connection,"PASS " + Password + #LFCR$)
          FreeMemory (*Mem)
          Debug Reply
          *Mem = AllocateMemory(5000)
          If ReceiveNetworkData(Connection,*Mem,5000)
            Reply = PeekS(*Mem)
            Debug Reply
            If FindString(Reply,"proceed")                                             ;Should be logged in now (works)
              SendNetworkString(Connection,"NLST " + #LFCR$)               ;is this actually how you use NLST?
              FreeMemory (*Mem)
              Debug Reply
              *Mem = AllocateMemory(5000)
              If ReceiveNetworkData(Connection,*Mem,5000)
                Reply = PeekS(*Mem)
                Debug Reply                                                        ;150 Opening ASCII mode data connection for /bin/ls  <- what's that?
   
              EndIf
            EndIf
          EndIf
        EndIf
      EndIf
    EndIf
  EndIf
  
EndProcedure
This --> "150 Opening ASCII mode data connection for /bin/ls". I find it a bit weird as (i think) i have demanded a list of the directory entries. The question is.......what do I do with that thing now and how do I get the list?


2nd:
I tried to upload files WITH PB'S FTP COMMANDS, but it wouldn't work. This is the code:

Code: Select all

hi.s = OpenFileRequester ("Select a file..","","*.*",-1)
SendFTPFile(0,hi,GetFilePart(hi),#True)
I can't see the file on the server afterwards.

3rd:
I tried to download files with PB'S FTP COMMANDS, but I didn't get them. my try (

Code: Select all

ExamineFTPDirectory(0)
While NextFTPDirectoryEntry(0)
  AddGadgetItem (20,-1,FTPDirectoryEntryName(0))
Wend

For i = 1 To CountGadgetItems(20)
  If GetGadgetItemState(20,i)
    If ReceiveFTPFile(0,GetGadgetItemText(20,i),SaveFileRequester("Save as..","","*.*",0),#True)
      MessageRequester("Success!","Received a file :-)")
    Else
      MessageRequester("Error!","Could not receive File!")
    EndIf
  EndIf
Next
Gadget 20 is a ListViewGadget so I can select entries and click the download button. This doesn't work either.....But I don't understand why.



I hope someone here as more experience with FTP than I and I hope you understand my questions. Thank you in advance :)
Azur
User
User
Posts: 63
Joined: Sat Jan 28, 2012 11:13 am

Re: FTP file listing

Post by Azur »

Hi,
for the download, did you try to use " setftpdirectory() " to something lihe www/yourfolder/
I already had the same issue.
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: FTP file listing

Post by J@ckWhiteIII »

As the server says something like /bin/ls.....maybe that's the directory?
pwd
User
User
Posts: 60
Joined: Thu Sep 06, 2012 9:39 am

Re: FTP file listing

Post by pwd »

As for 2nd & 3rd:
Try to turn off the Asynchronous flag in SendFTPFile() & ReceiveFTPFile() and compare results.
Azur
User
User
Posts: 63
Joined: Sat Jan 28, 2012 11:13 am

Re: FTP file listing

Post by Azur »

this is the procedure when the user dblclicked a name in the list

Code: Select all

Procedure telecharger()
  Define line=GetGadgetState(ListIcon_0)
  If line <> -1
    Define nom.s=GetGadgetItemText(ListIcon_0,line,0)
    Define dest.s=PathRequester("Destination","")
    If OpenFTP(0,appli\server,appli\login,appli\passwd)
      SetFTPDirectory(0,"www/documents/")
      ExamineFTPDirectory(0)
      While NextFTPDirectoryEntry(0)
        If FTPDirectoryEntryName(0)=nom
          Define taille=FTPDirectoryEntrySize(0)
        EndIf
      Wend 
      FinishFTPDirectory(0)  
      SetGadgetAttribute(ProgressBar_0,#PB_ProgressBar_Minimum,0)
      SetGadgetAttribute(ProgressBar_0,#PB_ProgressBar_Maximum,taille)
      ReceiveFTPFile(0,nom,dest+nom,1)
      Define p=0
      Repeat
        p=FTPProgress(0)
        SetGadgetState(ProgressBar_0,p)
        Define ev=WaitWindowEvent()
        If ev=#PB_Event_CloseWindow
          If(MessageRequester("Attention","Voulez-vous interrompre",#PB_MessageRequester_YesNo)=#PB_MessageRequester_Yes)
            AbortFTPFile(0)
            CloseFTP(0)
            clear_barre()
            ProcedureReturn
          EndIf
        EndIf
      Until p=#PB_FTP_Finished
      MessageRequester("Téléchargement","Téléchargement terminé",#PB_MessageRequester_Ok)
      CloseFTP(0)
    Else
      MessageRequester("Attention","Impossible d'acceder au réseau distant",#PB_MessageRequester_Ok)
    EndIf
  EndIf
EndProcedure

User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: FTP file listing

Post by J@ckWhiteIII »

Whoa, the upload works! Thanks alot, I'd never have tried that o.O
Azur
User
User
Posts: 63
Joined: Sat Jan 28, 2012 11:13 am

Re: FTP file listing

Post by Azur »

Yes pwd was right
By using async mode the program dont't wait the end of the transfert and directly exit
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: FTP file listing

Post by J@ckWhiteIII »

@Azur,
your procedure does not work for me. It's always stuck in the Repeat-Until loop
Azur
User
User
Posts: 63
Joined: Sat Jan 28, 2012 11:13 am

Re: FTP file listing

Post by Azur »

Oh yes, this procedure, outside of his context is probably more for example of asynchronous transfert with progress bar, but not sure it can reused directy.
TerryHough
Enthusiast
Enthusiast
Posts: 781
Joined: Fri Apr 25, 2003 6:51 pm
Location: NC, USA
Contact:

Re: FTP file listing

Post by TerryHough »

J@ckWhiteIII wrote:Hey,
I'm trying to code an app to access a FTP server, upload/download files. Somehow, with PB's FTP commands it seems to be a bit limited, and what I tried (upload/download) did not work properly.

In PB, you can examine the FTP directory and list the entries. As I want to avoid PB's built in FTP commands, I tried to do it with just network commands. A try to connect on the server and get a list of the entries in the FTP directory:
You can't successfully mix the FTP and Network commands.

But, take a look in this post for some of my code that exhibits the PB FTP library in use for list in remote directory and downloading a file.
http://www.purebasic.fr/english/viewtop ... veFTPFile+

Some of my similar code for sending a file can be found in this post.
http://www.purebasic.fr/english/viewtop ... endFTPFile

I think between the two of them you can see how to accomplish your desired results.
Post Reply