FTP listing and downloading files

sheriffporter

Member
Licensed User
Longtime User
Still struggling with the FTP library... I am just not getting part of it..

I got it to connect, and I can upload or download any specific files (if I specify exact filenames). I need a very simple sub to list files, then download all available files in a given directory (they are small text files, and I do not know the names at any given time - i just need to grab them all).

I have this code from the examples:
B4X:
FTP.List("/")

Sub FTP_ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
    Log(ServerPath)
    If Success = False Then
        Log(LastException)
    Else
        For i = 0 To Folders.Length - 1
            Log(Folders(i).Name)
        Next
        For i = 0 To Files.Length - 1
            Log(Files(i).Name & ", " & Files(i).Size & ", " & DateTime.Date(Files(i).Timestamp))
        Next
    End If
End Sub

I can't figure out how to use the data now... I just want to download all of the files found then delete them off the server directory.
I know I need to execute a download like this:


ftp.download(MyServer, DownloadDIR, true, ?? filenames??)

But how do I identify the file names? files(i).name or something like that?
 

Roeschti

Member
Licensed User
Longtime User
I gave you a fully functional App yesterday. What do you see in the Listview? Foldernames and Filenames. What Happens if you click on a filename? It downloads the file to the wanted traget directory.

How do you identify the filenames? Have a look in the listview and the way the listview gets filled, there are all necessery comments. It's really very easy.
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
Enter whatever you like for the files you are looking for. The sample has "*.txt". Once the process completes, the list1 will have all the file names in it. Just process the list1 array for your downloads, etc.

B4X:
'Dim these in Globals
Dim list1 As List
list1.Initialize

Sub FTP_Start
      FTP.List("*.txt")
End Sub

Sub FTP_ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
     For i = 0 To Files.Length -1
        list1.Add(Files(i).Name)
     Next 
End Sub
 
Last edited:
Upvote 0

sheriffporter

Member
Licensed User
Longtime User
Thank you that is helpful. I was not able to get the (*.txt) to work, the FTP server message kept saying "transferring an empty buffer", but once I changed it to files.list("/") it at least reads the files and will return a number of files for me from this:

For i = 0 To Files.Length -1
list1.Add(Files(i).Name)
Next

ToastMessageShow = ("Files Waiting: " & list1.size, True)

So now I know the files are loading into list1 since it is correctly showing the number of files. Now I need to grab the first file and download it, then loop until the count is zero (I think). I am just not sure what identifies the first file in the list, in other words what handle to use to tell it what filename to download first. It wont accept "*.*" and just grab them all which would be nice...
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
The *.txt would not work because you must not have any files on the server in the root directory that end with .txt. Now start a download with list1.Get(0), you can use an int variable in place of 0. In the FTP_DownloadCompleted sub you can then delete the file list1.Get(0). Now start over again and add 1 to the Int.

B4X:
'Example Code
'Dim In Globals
Dim myfile As Int : myfile = 0

Sub GetFiles
     FTP.DownloadFile(list1.Get(myfile), True, File.DirInternal, list1.Get(myfile))
End Sub

Sub FTP_DownloadCompleted (ServerPath As String, Success As Boolean)
     Log("Downloaded: /" & ServerPath & ", Success=" & Success)
     If Success = False Then 
           Log(LastException.Message)
           GetFiles
     End If
     'Delete File Here
     If myfile < list1.Size -1 Then
           myfile = myfile + 1 
           GetFiles
     End If
End Sub
 
Last edited:
Upvote 0

cirollo

Active Member
Licensed User
Longtime User
Strange beahaviour with 3g and FTP List

Hi Folks!

I cannot figure out how to make working this code under 3g/Hsdpa Connection

B4X:
Sub FTP_ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
    ordiniok="NO"
   incassiok="NO"
   Log(ServerPath)
    If Success = False Then
        Log(LastException)
'      Msgbox2("Impossibile Contattare FTP!",Main.nomeprog,"","Ok","",LoadBitmap (File.DirAssets, "warning_256.png"))
'      FTP.CloseNow
'      Activity.Finish
'      StartActivity(Main)
    Else
        For i = 0 To Folders.Length - 1
            Log(Folders(i).Name)
        Next
        For i = 0 To Files.Length - 1
            Log(Files(i).Name & ", " & Files(i).Size & ", " & DateTime.Date(Files(i).Timestamp))
           If fileord <> "" Then
            If Files(i).Name = fileord Then
               ordiniok="SI"
            End If
         Else
            ordiniok="NF"
         End If
         If fileinc <> "" Then
            If Files(i).Name = fileinc Then
               incassiok="SI"
            End If
         Else
            incassiok="NF"
         End If
      Next
    End If
   If ordiniok="NO" Then
      TxtLog.Text = "File non Ricevuto su FTP "& nomefile &Chr(10)&TxtLog.Text
      ' svuotiamo la DataExp sulle righe dell'ordine
      Dim Reader As TextReader
         Reader.Initialize(File.OpenInput(sdRoot, fileord))
         Dim line As String
         Dim WhereFields As Map
      Do While line <> Null 
          line = Reader.ReadLine.SubString2(0,16)
         WhereFields.Initialize
         WhereFields.Put("Seriale", line)
         DBUtils.UpdateRecord(SQL1, "Ordini", "DataExp", "NO", WhereFields)
      Loop
   Else
      If ordiniok="SI" Then
         TxtLog.Text = "File Ricevuto su FTP "& nomefile &Chr(10)&TxtLog.Text
      End If
   End If
   If incassiok="NO" Then
      TxtLog.Text = "File non Ricevuto su FTP "& nomefile &Chr(10)&TxtLog.Text
      ' svuotiamo la DataExp sulle righe dell'ordine
      Dim Reader As TextReader
         Reader.Initialize(File.OpenInput(sdRoot, fileord))
         Dim line As String
         Dim WhereFields As Map
      Do While line <> Null 
          line = Reader.ReadLine.SubString2(0,16)
         WhereFields.Initialize
         WhereFields.Put("Seriale", line)
         DBUtils.UpdateRecord(SQL1, "Ordini", "DataExp", "NO", WhereFields)
      Loop
   Else
      If incassiok="SI" Then
         TxtLog.Text = "File Ricevuto su FTP "& nomefile &Chr(10)&TxtLog.Text
      End If
   End If
End Sub

It seems that this sub is never processed!
I'm under a Passive Mode FTP connection, the strange thing is that if I do that under a wi-fi connection, it works!!!

any suggestion????
thanks!
 
Upvote 0

cirollo

Active Member
Licensed User
Longtime User
Nobody can Help????

is possible FTP.List under 3g networks???

Hi Folks!

I cannot figure out how to make working this code under 3g/Hsdpa Connection

B4X:
Sub FTP_ListCompleted (ServerPath As String, Success As Boolean, Folders() As FTPEntry, Files() As FTPEntry)
    ordiniok="NO"
   incassiok="NO"
   Log(ServerPath)
    If Success = False Then
        Log(LastException)
'      Msgbox2("Impossibile Contattare FTP!",Main.nomeprog,"","Ok","",LoadBitmap (File.DirAssets, "warning_256.png"))
'      FTP.CloseNow
'      Activity.Finish
'      StartActivity(Main)
    Else
        For i = 0 To Folders.Length - 1
            Log(Folders(i).Name)
        Next
        For i = 0 To Files.Length - 1
            Log(Files(i).Name & ", " & Files(i).Size & ", " & DateTime.Date(Files(i).Timestamp))
           If fileord <> "" Then
            If Files(i).Name = fileord Then
               ordiniok="SI"
            End If
         Else
            ordiniok="NF"
         End If
         If fileinc <> "" Then
            If Files(i).Name = fileinc Then
               incassiok="SI"
            End If
         Else
            incassiok="NF"
         End If
      Next
    End If
   If ordiniok="NO" Then
      TxtLog.Text = "File non Ricevuto su FTP "& nomefile &Chr(10)&TxtLog.Text
      ' svuotiamo la DataExp sulle righe dell'ordine
      Dim Reader As TextReader
         Reader.Initialize(File.OpenInput(sdRoot, fileord))
         Dim line As String
         Dim WhereFields As Map
      Do While line <> Null 
          line = Reader.ReadLine.SubString2(0,16)
         WhereFields.Initialize
         WhereFields.Put("Seriale", line)
         DBUtils.UpdateRecord(SQL1, "Ordini", "DataExp", "NO", WhereFields)
      Loop
   Else
      If ordiniok="SI" Then
         TxtLog.Text = "File Ricevuto su FTP "& nomefile &Chr(10)&TxtLog.Text
      End If
   End If
   If incassiok="NO" Then
      TxtLog.Text = "File non Ricevuto su FTP "& nomefile &Chr(10)&TxtLog.Text
      ' svuotiamo la DataExp sulle righe dell'ordine
      Dim Reader As TextReader
         Reader.Initialize(File.OpenInput(sdRoot, fileord))
         Dim line As String
         Dim WhereFields As Map
      Do While line <> Null 
          line = Reader.ReadLine.SubString2(0,16)
         WhereFields.Initialize
         WhereFields.Put("Seriale", line)
         DBUtils.UpdateRecord(SQL1, "Ordini", "DataExp", "NO", WhereFields)
      Loop
   Else
      If incassiok="SI" Then
         TxtLog.Text = "File Ricevuto su FTP "& nomefile &Chr(10)&TxtLog.Text
      End If
   End If
End Sub

It seems that this sub is never processed!
I'm under a Passive Mode FTP connection, the strange thing is that if I do that under a wi-fi connection, it works!!!

any suggestion????
thanks!
 
Upvote 0

cirollo

Active Member
Licensed User
Longtime User
No

even in passive mode I cannot get ftp list to work

see my other thread, the situation is becoming hot
 
Upvote 0
Top