ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage functions / Communication / FTP functions
  • Listing the files found in a directory
FTPCommand (Example)
Listing the files found in a directory
WINDEVWEBDEV - Server codeReports and QueriesJavaUser code (UMC)Ajax
The following code is used to connect to an FTP server and to check whether a file exists in the list of directory files. Then, this file is downloaded.
// Connection identifier
nConnectionID is int
// Parameters of the FTP connection
sAddressFTPServer, sNameFTPAccount, sFTPPwd are strings
// Parameters for FTPCommand
sNameFTPCommand is string
sFileNameOnFTPServer is string
sFilePathOnFTPServer is string
sResponseFTPServer, sFTPServerData are strings
 
// Variables for tests
sAFileName is string
FileIndex is int
bFileFoundOnServer is boolean
sAddressFTPServer = "192.5.58.47"
sNameFTPAccount = "MyFTPAccount"
sFTPPwd = "pwd"
 
// FTP connection
nConnectionID = FTPConnect(sAddressFTPServer, sNameFTPAccount, sFTPPwd)
IF nConnectionID <> -1 THEN
// The connection is OK
// Name of file to download
sFileNameOnFTPServer = "PdtUpdate.zip"
sFilePathOnFTPServer = "UpdateDir"   //(path with "/" and NOT "\")
 
// Specify the directory containing the file to download
IF FTPCurrentDir(nConnectionID, sFilePathOnFTPServer) = False THEN
Error(StringBuild("Unable to position on " + ...
"the %1 directory on the FTP server.", ...
sFilePathOnFTPServer), ErrorInfo())
END
// Retrieve the list of files found in the current directory with
// the WLanguage "FTPCommand" function and the FTP "LIST" command
// Name of the FTP command
sNameFTPCommand = "LIST" // you could also use FTPListFile
// Ask for the list of files found in the current directory
IF FTPCommand(nConnectionID, "LIST", ...
sResponseFTPServer, sFTPServerData) = False THEN
  // The FTP command failed
Error(StringBuild("The %1 command failed. " + ...
"Response from the FTP server: %2", sNameFTPCommand, ...
    sResponseFTPServer), ErrorInfo())
ELSE
// Check whether the specified file name exists on the FTP server
    FileSubscript = 1
    LOOP
// Full name of the file on the FTP server
// (with the date, the time and the attributes)
sAFileName = ExtractString(sFTPServerData, FileSubscript, CR)
IF sAFileName = EOT THEN BREAK
// Name of the file only (without the date, the time or the attributes)
sAFileName ~= sAFileName[[40 À]]
// Is this the sought file? (the case is not checked)
IF sAFileName ~= sFileNameOnFTPServer THEN BREAK
FileSubscript++
    END
    // File found?
    IF bFileFoundOnServer = False THEN
// List of files found in the current directory on the server
Error(StringBuild("The %1 file was not found" +...
" on the FTP server. Here is the list of files in the current directory" +...
" on the FTP server:" + ...
    CR + "%2", sFileNameOnFTPServer, sFTPServerData))
END
END
IF bFileFoundOnServer = True THEN
// The file exists on the FTP server, download it
// retrieve the file by using the WLanguage function named "FTPCommand"
// and the FTP command named "RETR"
// Name of the FTP command
sNameFTPCommand = "RETR"   // you could also use FTPGet
IF FTPCommand(nConnectionID,sNameFTPCommand + " " + ...
sFileNameOnFTPServer, sResponseFTPServer, ...
sFTPServerData) = False THEN
// The FTP command failed
Error(StringBuild("The %1 command failed. Response from the FTP server: %2", ...
sNameFTPCommand, sResponseFTPServer), ErrorInfo())
ELSE
// Data successfully retrieved,
// The data of the "sFileNameOnFTPServer" file
// is found in "sFTPServerData"
Info(StringBuild("The %1 file was successfully downloaded, " + ...
"click OK to save it on the local disk", ...
sFileNameOnFTPServer))
// Save the file's data locally
// (in the current directory without path)
IF fSaveText(sFileNameOnFTPServer, ...
sFTPServerData) = False THEN
Error("Unable to save the downloaded data", ErrorInfo())
END
END
END
// Disconnection
FTPDisconnect(nConnectionID)
Info("FTP connection ended")
ELSE
Error("The connection to the FTP server failed", ErrorInfo())
END
Minimum version required
  • Version 9
Comments
Click [Add] to post a comment

Last update: 05/26/2022

Send a report | Local help