I have a project building an automatic backup downloading tool in B4J but have hit a major snag...
The App contains a B4XTable with information about a download enabling a login to a given server and a path from which it pulls the latest (backup) file and saves it.
To pull the given file, I iterate through the b4xtable and access the server with the right credentials:
This makes the right connection and then I list the files in the specified Path which then raises the _ListCompleted Event
The right file is found and passed to geFile so I can download the file.
HOWEVER
This only works when I have a single entry in the B4XTable, otherwise the code just ploughs on through to the next entry in the B4XTable, registering that there are no files in the given folder of the first and second entries on the B4XTable, but it then starts successfully downloading the third.
I assume this is because the sftp.FileDownload action is asynchronous...
Can I / Should I wrap sftp.DownloadFile(Path, BackupLocation, Filename) in a Wait For?
Your advice / experience greatly appreciated.
Simon+
The App contains a B4XTable with information about a download enabling a login to a given server and a path from which it pulls the latest (backup) file and saves it.
To pull the given file, I iterate through the b4xtable and access the server with the right credentials:
B4XMainPage:
Private Sub ftpGo(Server As String, Login As String, Password As String, Path As String)
sftp.Initialize("sftp", Login, Password, Server, 22)
sftp.SetKnownHostsStore(File.DirData("cmSFTP"), "hosts.txt")
sftp.List(Path)
End Sub
This makes the right connection and then I list the files in the specified Path which then raises the _ListCompleted Event
B4XMainPage:
Sub sftp_ListCompleted (ServerPath As String, Success As Boolean, Folders() As SFtpEntry, Files() As SFtpEntry)
If Files.Length> 0 Then
Log("There are " & Files.Length & " items in this folder")
Log(Files(Files.Length-1).Name & " date is " & DateTime.Date(Files(Files.Length-1).Timestamp))
If ServerPath.CharAt(ServerPath.Length - 1) <> "/" Then
Log("Adding final forward slash")
ServerPath=ServerPath & "/"
End If
lblFile.Text = "File: " & Files(Files.Length-1).Name
getFile(ServerPath & Files(Files.Length-1).Name, Files(Files.Length-1).Name)
Else
Log("No Files found in " & ServerPath)
End If
End Sub
Sub getFile(Path As String, Filename As String)
sftp.DownloadFile(Path, BackupLocation, Filename)
End Sub
Sub sftp_DownloadCompleted (ServerPath As String, Success As Boolean)
Log(ServerPath & ", Success=" & Success)
If Success = False Then Log("Download Error: " & LastException.Message)
sftp.Close
End Sub
The right file is found and passed to geFile so I can download the file.
HOWEVER
This only works when I have a single entry in the B4XTable, otherwise the code just ploughs on through to the next entry in the B4XTable, registering that there are no files in the given folder of the first and second entries on the B4XTable, but it then starts successfully downloading the third.
I assume this is because the sftp.FileDownload action is asynchronous...
Can I / Should I wrap sftp.DownloadFile(Path, BackupLocation, Filename) in a Wait For?
Your advice / experience greatly appreciated.
Simon+