How to use FTP scripting

edtFTPj/PRO includes a powerful scripting engine that allows users to list FTP commands in a text file, and then execute all the commands sequentially. The script can be be run via a batch file (or Unix shell script), or the ScriptEngine class can be used within an application to run a script.

The script engine supports the FTP, FTPS (both implicit and explicit) and SFTP protocols. For FTPS, both client and server validation is supported.

Sample code

A sample script is shown below:

# Sample FTP script using FTP initially
# then changing to FTPS
#
set remotehost=edtmobile
set port=21
set user=javaftp
set password=javaftp
set connectmode=passive
set timeout=30
set protocol=ftp
set localdir=D:\work\tmp
    
# client and server validation for FTPS
set servercert=d:\work\ftp-ssl\test\conf\server.cert.pem
set clientcert=d:\work\ftp-ssl\test\conf\client.pem
set certpassword=password
    
# logging off = 0, highest level = 5
set loglevel=5
set logfile=D:\work\ftp-ssl\tmp\script.log
    
# connect to the server
# optional remotehost and port supplied
# override the params set earlier
open edtmobile 21
cd remote/test
    
# change to secure mode
auth tls
    
# force binary mode transfers
binary
    
# put the local file test.jar to the server as mytest.jar
put test.jar mytest.jar
    
# rename it
rename mytest.jar test2.jar
    
# and get it back as test3.jar in the local working directory
get test2.jar test3.jar
    
# delete all *.jar files in the current directory on
# the server
mdelete *.jar
    
# disconnect
close

Running a script

To run the above script, assuming it is saved in a file called script.txt, simply supply it as the first argument to the ScriptEngine class

java -classpath %CP% com.enterprisedt.net.ftp.script.ScriptEngine script.txt

(where %CP% contains edtftpjpro.jar and license.jar).

Alternatively, a comprehensive set of command line parameters can be used as described below:

[-h] [-c configfile] [-f scriptfile] [-u user] [-p password] [-v]
    
-f scriptfile : script to execute.
-c configfile : config script to execute before main scriptfile.
-h            : display this help
-u user       : set the user name (optional)
-p password   : set the user password (optional)
-v            : print version details 

For example:

java -classpath %CP% com.enterprisedt.net.ftp.script.ScriptEngine -c config.txt -f script.txt 

In this example, a configuration file is supplied in addition to the script file to run. The settings in the configuration file are loaded before the script file is executed, so the file can be used for common settings.

If the runscript.bat (or runscript on Unix) batch script is used:

runscript -c config.txt -f script.txt 

If the ScriptEngine is used directly from code to run scripts via the executeScript() methods, the ScriptResult object that is returned can be examined for various statistics collected about the script execution.

Command overview

Most commands are fairly self explanatory.

The set command is used to set various parameters that are used during the session, such as username, password, timeout and so on. Many of these parameters are optional (e.g. port, timeout). The most important parameter is the protocol.

The local working directory is by default the standard Java working directory for the application, but the set localdir command can be used to set a different local working directory. All uploads and downloads that do not specify a full path will use the set local working directory.

Command reference

The scripting commands are described in the command reference.