FTPScript is a powerful yet simple scripting language specifically for FTP.
FTP commands can be listed in a text file, and then the scripting engine executes all the commands sequentially. The script can be run via edtftp.exe 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.
FTP Shell
All the scripting commands are available in the FTP Shell. This is a shell (like a DOS shell) that allows the commands to be executed one by one as they are typed in. The FTP shell makes it very easy to develop scripts (as they can be saved to a file). More details can be found here. The shell is used to run scripts as well as described below.
Command reference
The scripting commands are described in the script command reference.
Running a script
All configuration flags for edtftp.exe are listed here.
To run a script (such as the one provided below), assuming it is saved in a file called script.txt, simply supply it using the -f flag to the FTPShell, edtftp.exe, which is in the installation directory (easiest to add your installation directory to your PATH)., e.g.
edtftp.exe -f script.txt
To run a script script1.txt while supplying the user name and password on the command-line, use:
edtftp.exe -f script1.txt -u myuser -p mypassword
If your trial license has expired, you can supply new license details while running the script:
edtftp.exe -f script1.txt -u myuser -p mypassword -l trialuser -k xxx-xxx-xxx-xxx
You can also update the registry with new license details. This need only be done once for a production license:
edtftp.exe-l trialuser -k xxx-xxx-xxx-xxx -r
You can also write the license details to a configuration file for edtftp.exe (in Vista you will need administrative permissions to do so depending on where the file is written to):
edtftp.exe-l trialuser -k xxx-xxx-xxx-xxx -i
Using debug
It can be very useful to switch on debug while running scripts to diagnose problems. This is done by the -d option, e.g.
edtftp.exe -f script.txt -d
The log file can also be examined.
Command overview
Most commands are fairly self explanatory, and are described in the script command reference.
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 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.
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
# 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
# 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 directoryon
# the server
mdelete *.jar
# disconnect
close
Using ScriptEngine in code
The ScriptEngine class can be used directly in your code to run scripts via the various ExecuteScript methods. A single command can be run via RunCommand. You can also set your licensing details as shown. The ScriptResult should always be examined to confirm that the script executely successfully.
ScriptEngine engine = new ScriptEngine();
engine.LicenseProperties = new LicenseProperties("licenseowner", "xxx-xxxx-xxxx-xxxx");
ScriptResult result = engine.ExecuteScript("myscriptfile.ftp");