ScriptFTP Scripting Guide
ScriptFTP Scripting Guide
Copyright 2004-2008
Carlos Andrés Martínez
Comparasion operators_________________________________________________ 48
Logical operators_______________________________________________________ 49
2
Commands for server connection ____________________________ 52
OPENHOST ______________________________________________ 53
CLOSEHOST _____________________________________________ 56
SETPROTOCOL ___________________________________________ 58
SETPORT ________________________________________________ 61
4
TEXTCUT _______________________________________________ 136
TEXTLENGTH____________________________________________ 138
5
Getting started
6
Lesson 1: Your first script
ScriptFTP is a script-driven FTP client. It works like traditional FTP clients but does not
require any user interaction while running, instead it works automatically using a text
file which contains the actions ScriptFTP has to execute. Let us consider a simple script:
OPENHOST("ftp.myhost.com","myuser","mypassword")
GETFILE("myfile.zip")
CLOSEHOST
Click on File → New → Empty Script, paste the above script into the editor window and
replace "ftp.myhost.com", "myuser" and "mypassword" with your settings. Then click
RUN in the ScriptFTP window. Do not forget to save your modifications in the editor
window first.
As you can see this script connects to the FTP server, downloads a file and then
disconnects. It is a fairly simple script.
7
In order to familiarize yourself with the commands supported by ScriptFTP take a look
at the command list. You may also take a look at GETFILE, PUTFILE or OPENHOST.
Lesson 1 has finished now. However, before moving on to the next lesson take a look at
the script examples section on the ScriptFTP homepage. There you will find a collection
of scripts for various purposes.
8
Lesson 2: Transferring files
The commands GETFILE, PUTFILE and SYNC are used for file transfers. Since the
purpose of ScriptFTP is transferring files, it is important to understand the use of these
commands before you write your own script.
The GETFILE command is used for downloading a set of files from the FTP server. It
has two parameters - the first one indicates the file or files you want to download. If
you also need to download files from subdirectories add the second parameter:
SUBDIRS.
# Connect to server
OPENHOST("ftp.myhost.com","myuser","mypassword")
# Download a.jpg
GETFILE("a.jpg")
# Close connection
CLOSEHOST
Note that when using the SUBDIRS parameter ScriptFTP will preserve the original
directory structure and will therefore create the appropriate directories on the local
drive.
Other commonly used script commands are CHDIR and LOCALCHDIR. The first one sets
the current remote directory and the second one is used to set the current local
directory. ScriptFTP will download all files to the current local directory so you had
better use LOCALCHDIR before calling GETFILE. Let us look at an example
demonstrating the use of both commands:
9
# Connect to server
OPENHOST("ftp.myhost.com","myuser","mypassword")
# Close connection
CLOSEHOST
The PUTFILE command obeys the same syntax. Let us look at an example:
# Connect to server
OPENHOST("ftp.myhost.com","myuser","mypassword")
# Close connection
CLOSEHOST
And finally the SYNC command. Use this command for synchronizing directories. In
ScriptFTP the term syncronization means "Get an exact copy of a directory transferring
new and modified files only. Delete unwanted files.". This definition of syncronization
may sound complex, but you will understand it as soon as you see the SYNC command
in action:
10
# Connect to server
OPENHOST("ftp.myhost.com","myuser","mypassword")
# Close connection
CLOSEHOST
The first parameter indicates a local directory, the second one a remote directory and
the third one the direction of the synchronization. You can synchronize both a local
directory with a remote directory (DOWNLOAD) and a remote directory with a local one
(UPLOAD). For example, if you want to publish a web site the command is:
SYNC("C:\local_webdir","/www",UPLOAD)
The SYNC command also has a fourth parameter which is optional: SUBDIRS. Its
meaning is the same as in GETFILE or PUTFILE.
For further information take a look at the commands help reference: GETFILE, PUTFILE,
SYNC. Also see Transferring modified files only.
11
Lesson 3: Variables
If you have done some programming before you will probably be familiar with the term
"variable". However, this tutorial is intended to teach the concept of variables from the
ground up. So here is a definition: "A variable is a language item to store something".
In ScriptFTP that something means just text. Let us look at an example:
Hello world
A variable does not need to be declared before use. In other words, you do not need to
state explicitly in your script that the word "my_number" is a variable you are going to
use. Just use the variable. The only restriction on variable names is that command
names are not allowed.
You can use variables for storing the parameters of a command and then call the
command using these variables.
$host="ftp.myhost.com"
$user="myuser"
$password="123456"
OPENHOST($host,$user,$password)
PUTFILE("*.*",SUBDIRS)
CLOSEHOST
12
$result=OPENHOST(host,user,password)
Just like commands ScriptFTP features a set of operators for calculating arithmetical or
logical expressions. In the above example we used the == operator in order to check
whether the content of the variable is equal to "OK". Operators also include +,-,*,/
among others. Have a look at Operators for more information.
The use of IF is also described in the next section. See also Error handling.
As mentioned before every ScriptFTP variable holds a text string and therefore it is
necessary to enclose the text with quotes, however, if you supply a number, the quotes
may be omitted.
$myvariable=1
PRINT($myvariable)
PRINT("2")
PRINT(-3)
1
2
-3
13
Lesson 4: Using IF, WHILE and GOTO
IF and WHILE are language elements that evaluate conditions. You may use them for
evaluating error conditions, performing actions a fixed number of times etc. Since every
variable in ScriptFTP contains text, the text "TRUE" means true. Any other text is
evaluated to false. For example:
IF("TRUE")
PRINT("ScriptFTP rules")
END IF
IF("fdasfdas")
PRINT("hi")
END IF
$myvariable="TRUE"
if($myvariable)
PRINT("ScriptFTP rules")
END IF
ScriptFTP rules
ScriptFTP rules
As you can see from the script output the IF statement only executes its body if the
condition equals the text "TRUE".
Let us look at a more useful example. In the following script the IF statement is used to
check whether OPENHOST was successful:
14
$user="carl"
$password="1234567890"
# do the stuff
LOCALCHDIR("C:\localwww\")
CHDIR("www")
GETFILE("*.*")
CLOSEHOST()
PRINT("FINISHED")
EXIT
The WHILE statement will execute the commands in its body as long as the condition
equals "TRUE". In the following script the WHILE statement will never terminate and
will therefore keep a local directory synchronized with an FTP site:
# Loop indefinitely
WHILE("TRUE")
$result=GETFILE("*.*")
IF($result!="OK")
PRINT("ERROR in GetFile, exiting")
# Exit with error code 1
EXIT(1)
ELSE
PRINT("All files downloaded. Waiting 10 seconds")
SLEEP(10)
END IF
END WHILE
16
Lesson 5: Handling file lists
Sometimes it is useful to perform different actions for each item in a set of files. For
example, if you do the following:
# Connect to server
OPENHOST("ftp.myhost.com","joe","123456")
It may happen that GETFILE fails and remote files will be deleted although they
have never been downloaded. In order to avoid this issue we will retrieve a file
listing with the GETLIST command, call GETFILE for each file in the listing and
delete this remote file using DELETEFILE if no error has occured.
# Connect to server
OPENHOST("ftp.myhost.com","joe","123456")
17
IF($result=="OK")
DELETEFILE($item)
ELSE
STOP
END IF
END FOREACH
GETLIST commands are usually followed by FOREACH loops. See GETLIST for
further reference about parameters and options.
18
HOW TOs
19
Transferring modified files only
Synchronization is one of the most complex operations ScriptFTP can carry out. This is
because the command used for this purpose (SYNC) may sometimes appear complex
and confusing. It is important to understand that in ScriptFTP synchronization can only
be a one-way operation. This means that depending on the SYNC method you choose
ScriptFTP will upload new and modified local files to a remote directory *or* download
new and modified remote files to a local directory. These tasks are mutually exclusive.
Optionally SYNC can also delete orphaned files. Before you start wondering about the
meaning of "orphaned" let us look at a very simple example:
Once you have clicked the Run button ScriptFTP will display the following messages:
OPENHOST("ftp.host.com","myuser",******)
Connecting to ftp.host.com
Connected.
SYNC("C:\LocalWebFolder","/www",UPLOAD_DELETE)
Uploading temporary file to calculate server-client clock time
difference.
Clock time difference is -7201 seconds.
Synchronizing remote directory /www from C:\LocalWebFolder
Deleting (remote file is orphaned) orphaned_file.txt
Uploading (remote file not found) about.html
Skipping (remote file is uptodate) back.jpg
Uploading (remote file is older) contact.html
Uploading (remote file not found) index.html
Skipping (remote file is uptodate) notes.txt
20
CLOSEHOST
Disconnected.
The remote file orphaned_file.txt has been deleted because it could not be found in
C:\LocalWebFolder, this is why it is called an orphaned file. The next file about.html has
been uploaded because it did not exist in the remote directory /www. When a file is
found in both remote and local locations ScriptFTP can perform two actions: retransfer
it or simply ignore it (skip). The choice of action depends on the file modification date.
If the file is newer ScriptFTP will transfer it.
As you can see a synchronization can take three different actions for each file: transfer,
delete or skip. Thus ScriptFTP will achieve an exact copy of a local or remote directory
by transferring only the files needed. The method by which ScriptFTP finds all modified
files is comparing local and remote file modification time stamps. If the computer
ScriptFTP is running on and the FTP server share exactly the same time (which usually
never happens) ScriptFTP will only have to compare the time stamps of the files. In the
real world with computers spread around the world having different local times, it is
more complex to determine whether a remote file is older or newer than a given local
one. In order to solve this time difference problem ScriptFTP has the ability to
determine the time difference between server and client. This is what you will notice:
(...)
Uploading temporary file to calculate server-client clock time
difference.
Clock time difference is 3684 seconds.
(...)
3684 seconds comprise approximately one hour. This is the server-client time difference.
ScriptFTP will use this value to determine which file is older and will transfer it only if
necessary.
Sometimes it may happen that ScriptFTP cannot determine this value. This is usually
due to access restrictions. For example:
21
In this case you will have to manually tell ScriptFTP the time difference using
the SETCLOCKDIFF command:
SETCLOCKDIFF(-3600)
OPENHOST("ftp.host.com","myuser","mypassword")
SYNC("C:\LocalWebFolder","/www",UPLOAD_DELETE)
CLOSEHOST
22
Making a backup
This section will show you how to make a backup of a local directory. The backup will
be performed by creating a ZIP file of the local directory and then uploading it to an
FTP site.
We will use the EXEC command for creating the ZIP file. This command is used to call
external programs from within the script. In the following examples the EXEC command
will call the free command-line tool Info-Zip to create the ZIP file. However, you may
use any other command-line ZIP utility, for example, PkZip or PowerArchiver. We will
use the EXEC command like this:
When ScriptFTP reaches this line it will call C:\Info-Zip\zip.exe and wait for the external
program to finish. Once zip.exe has finished we will have MyBackupFile.zip with the
contents of C:\MyFolder in the local working directory (change it with LOCALCHDIR)
and will be ready to upload it to the FTP site:
# Connect to server
OPENHOST("ftp.myhost.com","myuser","mypass")
# Close host
CLOSEHOST
This method will replace the zip file everytime it is uploaded to the FTP site. If you want
to avoid this problem we can add the current date to the ZIP filename:
23
# concatenating text strings
$zip_file_name="MyBackupFile-".$current_date.".zip"
# Connect to server
OPENHOST("ftp.myhost.com","myuser","mypass")
# Close connection
CLOSEHOST
You may add even more features to this script file. The following script will make the
backup and also delete the intermediate zip file after successful upload. It will also log
the script run and it includes some error handling.
# Build the command line used to call the external ZIP tool
$backup_command_line=$info_zip_path."\zip.exe -r
".$zip_file_name." ".$backup_dir
25
END IF
# Once this point is reached the ZIP file will already be created
CLOSEHOST
PRINT("-----------------------------------------------")
26
PRINT("Backup finished. Closing ScriptFTP in 5 seconds.")
PRINT("-----------------------------------------------")
SLEEP(5)
EXIT(0)
27
Logging ScriptFTP messages
The LOGTO command is used to log ScriptFTP output to a text file. After calling this
command, every message shown on the ScriptFTP window will be added to the
specified text file (log file). With this feature you can check if your scheduled job has
been carried out correctly.
# Connect to server
OPENHOST("ftp.host.com","myuser","mypassword")
# Close connection
CLOSEHOST()
In the above example ScriptFTP overwrites the log file each time it is executed. If you
want to keep the history of subsequent runs you may append the current date to the
log file name:
# Build the log file name and path using the current date
$log_file_name="C:\log_file_name-".GETDATE(FORMAT2).".txt"
# Connect to server
OPENHOST("ftp.host.com","myuser","mypassword")
28
# Download some files
GETFILE("ClientDocs/*.*",SUBDIRS)
# Close connection
CLOSEHOST()
You may also supply the optional parameter APPEND to the LOGTO command. Thus
ScriptFTP will append its output to the log file each time the script is executed:
# Connect to server
OPENHOST("ftp.host.com","myuser","mypassword")
# Close connection
CLOSEHOST()
As a hint, if you find the script's output too detailed use the SILENT command to have
ScriptFTP display the essential information only.
29
Sending emails from within a script
ScriptFTP has been designed for unattended operation and usually you will know what
has happened during a script run just by taking a look at the ScriptFTP window.
Additionally, you may log a script's output messages to a text file and even have it send
emails to you which contain information about the run. This section is dedicated to this
topic.
In ScriptFTP there is no built-in command for sending emails because it is not needed.
We will rather use the EXEC command to call an external command-line program that
will send the email. The following examples use Blat, a free and tremendously useful
command-line mail program for all kinds of batch jobs. However, you can use any other
mail program just by adjusting the EXEC call.
The following example script will send an email if it cannot connect to the FTP server or
if the file synchronization fails. Note that we first setup the way how Blat will be invoked.
We construct two different commands for calling Blat, one for each type of error, since
the user should tell from the email what happened exactly and not be confused by a
generic error message.
$blat_path="C:\blat\blat.exe"
$smtp_server="smtp.myserver.com"
$smtp_user="myuser_at_myserver.com"
$smtp_password="mypassword"
30
$email_from="scriptftp@myserver.com"
$email_to="me@myserver"
$email_subject="ScriptFTP error message"
$email_body1="Could not connect to FTP server"
$email_body2="Could not syncronize files"
# Build the log file path by retrieving Windows' temp path from
# the system environment variable TEMP and appending the current
# date to the file name "logfile-". For Example:
# C:\windows\temp\logfile-20070424.txt
$log_file_path=GETENV("TEMP")."\logfile-".GETDATE(FORMAT3).".txt"
31
".$smtp_user." -pw "
$common_part_2=$smtp_password." -f ".$email_from." -to
".$email_to." -subject ".'"'
$common_part_3=$email_subject.'"'
32
EXIT(0)
33
Error handling
Every ScriptFTP command returns a text string. If everything goes well during the
execution of the command it will return the text "OK", if something goes wrong you will
get an error code. Evaluating this return value and taking the appropiate measures you
can make your file transfers fault-tolerant.
In the following example the output of OPENHOST is stored in a variable called $result.
If $result is "OK" we will continue, if $result is different from "OK" we will show a
message and try to reconnect to the FTP server.
# Shows a message
PRINT("_____Connecting_____")
# Close connection
CLOSEHOST
34
The example above will try to connect to the FTP server indefinitely. Let us add some
code in order to make only three attempts:
# Display a message
PRINT("Connecting. Attempt number ".$attempts)
35
# Close connection
CLOSEHOST
The command PUTFILE also outputs a return value. We will evaluate it in order to check
whether the upload has been performed successfully:
$webserver="www.whatever.com"
$myuser="me"
$mypassword="13579"
OPENHOST($webserver,$myuser,$mypassword)
$my_result_put=PUTFILE("*.*")
:allright
PRINT("All right")
# Close connection
CLOSEHOST()
# Wait 60 seconds
SLEEP(60)
# Close ScriptFTP
EXIT
:failed_put
PRINT("Error found putting files")
# Close connection
CLOSEHOST()
# Wait 60 seconds
36
SLEEP(60)
# Close ScriptFTP
EXIT
You can find more examples of error handling in the examples section of the website.
The advanced scripts therein cover this issue in detail.
37
ScriptFTP on the command line
ScriptFTP usually runs in a window but there is also a command line version of it. It
works exactly the same way as the usual ScriptFTP except that it will not display any
windows, it will just write the script messages to the command prompt. The program
file is called scriptftp_console.exe and you can find it in the directory where you
installed ScriptFTP. As this command prompt version does not display any windows or
dialog boxes you must specify which script you want to run:
As usual in the command line tools, the symbols < and > indicate a mandatory
paramater whereas [ and ] are used for optional parameters.
For example:
ScriptFTP_console.exe C:\MyScripts\upload.ftp
If the script path contains spaces remember to use double quotes to enclose it:
The custom parameters are optional and can be used in order to pass information to
your script, for example, the FTP login or the files to be uploaded. You can access these
parameters from within your script file using the GETPARAM command. Note that
ScriptFTP.exe (the GUI version of ScriptFTP) obeys the same syntax and also accepts
command line parameters.
38
# This script is used for uploading the specified file
# to the FTP server. Use it this way:
# ScriptFTP.exe this_script.ftp the_file_to_upload
# Note:
# The GETPARAM command was added in ScriptFTP 2.1
# Build March 14th 2006.
$param3 = GETPARAM(3)
OPENHOST("127.0.0.1","carl","123456")
PUTFILE($param3)
CLOSEHOST
39
If you need to get ScriptFTP completely hidden in the background use ScriptFTP.exe
(not scriptftp_console.exe) with /HIDE as the second parameter:
You can even start ScriptFTP.exe (not scriptftp_console.exe) as an icon in the system
tray:
If you want ScriptFTP.exe to close itself once the script has finished use the EXIT
command in your script or the /AUTOCLOSE command line parameter:
40
Updating 1.x scripts
The 3.x script language is identical to the 1.x version except from the following minor
differences:
• Every variable must start with the character "$". Put $ at the beginning of every
variable.
• Every label must start with the character ":". Put : at the beginning of every
labe.
• The commands ISEQUAL, NOT, ADD and CONCAT are still supported but they
are deprecated. As the 3.x script language supports operators you might want
to replace them with their corresponding arithmetical symbols.
• Some commands have changed their names and now the synchronization
feature has been turned into a separate command (SYNC). See the table below
for command correspondence.
Old command
New name Syntax changes
name
FTPOPENHOST OPENHOST no changes
FTPCLOSEHOST CLOSEHOST no changes
Synchronization/mirror
feature has been
FTPGETFILE GETFILE
moved to the new
SYNC command
Synchronization/mirror
feature has been
FTPPUTFILE PUTFILE
moved to the new
SYNC command
Same syntax. Added
TRANSFERMODE SETTYPE
EBCDIC.
LOCALCWD LOCALCWDIR no changes.
FTPCHDIR CHDIR no changes.
FTPCWD CWDIR no changes.
FTPMKDIR MKDIR no changes
41
FTPRMDIR RMDIR no changes.
FTPFILERENAME RENAMEFILE no changes.
FTPFILEDEL DELETEFILE no changes.
FTPLS GETLIST no changes.
FTPCHMOD CHMOD no changes.
42
Updating 2.x scripts
Changes in the script language compared to its 2.x version are minimal, but it is
required to change bits and pieces of a script file in order to have it working in the 3.x
series:
• Every variable must start with the character "$". Put $ at the beginning
of every variable. See the example below.
# Variables:
#
# 2.x:
myuser="joe"
mypass="1234"
myserver="ftp.host.com"
myresult=OPENHOST(myserver,myuser,mypass)
# 3.x
$myuser="joe"
$mypass="1234"
$myserver="ftp.host.com"
$myresult=OPENHOST($myserver,$myuser,$mypass)
• Every label must start with the character ":". Put : at the beginning of
every label. See the example below.
# Labels:
#
# 2.x
:mylabel
GOTO mylabel
# 3.x
:mylabel
GOTO :mylabel
43
• The commands ISEQUAL, NOT, ADD and CONCAT are no longer
supported. You have to replace them with their corresponding symbol. See
the example below:
IF(NOT(ISEQUAL(result,"OK")))
PRINT("operation failed")
END IF
IF(ISEQUAL(result,"12451"))
PRINT("Access error")
END IF
mymessage=CONCAT("hello ",name)
PRINT(mymessage)
# 3.x
$num=$num+3
IF($result!="OK")
PRINT("operation failed")
END IF
IF($result==12451)
PRINT("Access error")
END IF
$mymessage="hello ".$name
PRINT($mymessage)
# ScriptFTP 2.x:
remote_file_listing=LIST()
44
PRINT(remote_file_listing)
# ScriptFTP 3.x:
GETLIST($list,REMOTE_FILES)
FOREACH $item IN $list
PRINT($item)
END FOREACH
45
Encrypting script files
Script files may contain sensitive information such as passwords, user names or remote
server information. When ScriptFTP is deployed on many workstations it is advisable to
encrypt the script files in order to prevent users from reading the script content.
For encrypting a script file you will have to ask for a small tool at
scriptftp@scriptftp.com. The encryption process will leave the file name and extension
unaltered and renders the content of the script file unreadable. ScriptFTP will treat the
encrypted file as a normal script file.
This feature was added in ScriptFTP 2.1 build March 14th 2006
46
Operators
Operators are ScriptFTP symbols used for performing arithmetical or logical operations,
there are also operators for comparasion and text string concatenation. The behavior of
operators, like in any othe programming language, is very similar to commands. They
usually take two values or variables and return one. The operators supported by
ScriptFTP are the following:
Arithmetical operators
+ Sum.
- Substract.
* Multiply.
/ Divide.
47
As the ScriptFTP language is intended for batch operations and automatic transfers,
more complex operations such as square roots or powers are not supported.
Comparasion operators
The ScriptFTP language is typeless. There are no data types and every value is stored
as a text string, even numbers. Every logical operation like the IF statement or the
logical operators below rely on the interpretation of true as the text "TRUE" and false as
"FALSE". This means that the logical operators will indeed return a text value containing
"TRUE" or "FALSE". Let's see an example:
48
PRINT("The value stored in $B is not greater than the one
stored in $A")
END IF
Logical operators
The logical operators returns "TRUE" or "FALSE", this is because, as stated before,
ScriptFTP language is typeless. Every value is stored as text and the logical values true
or false are stored as "TRUE" or "FALSE" respectively.
49
$result3=PUTFILE("C:\backup.zip")
CLOSEHOST()
50
Text value operators
$text1="this"
$text2="is"
$text3="ScriptFTP"
51
Commands for server connection
52
OPENHOST
Syntax: OPENHOST(server,user,password)
Remarks:
• Use SETPROTOCOL before calling OPENHOST if you want to use secure FTP.
• If no user name and password are supplied ScriptFTP will try to login
anonymously.
• If you do not want your FTP password to show up in clear text you can encrypt
the entire script file. See Encrypting script files.
Return value:
OPENHOST will return "OK" as soon as the login has been successful and the
connection is established.
If the operation fails it will return an error code. You may check the return value in
order to try again. See Error handling or the last example on this page.
See also:
CLOSEHOST
SETPROTOCOL
SETPORT
SETTYPE
SETPASSIVE
Examples:
53
# Connect to ftp.myhost.com as myuser and download all the files
OPENHOST("ftp.myhost.com","myuser","mypassword")
GETFILE("*.*")
CLOSEHOST
OPENHOST("ftp.funet.fi")
GETFILE("README")
CLOSEHOST
OPENHOST("ftp.myhost.com","myuser","mypassword")
PUTFILE("README")
CLOSEHOST
$result=OPENHOST("ftp.myhost.com","myuser","mypassword")
54
IF($result!="OK")
END IF
# Download notes.doc
GETFILE("notes.doc")
55
CLOSEHOST
Syntax: CLOSEHOST()
Remarks:
As this command does not accept any parameters the brackets are optional.
Return value:
CLOSEHOST will return "OK" if the connection has been closed successfully. If the
operation fails it will return an error code. You may retrieve the return value and
execute various operations depending on this value. See Error handling.
See also:
OPENHOST
Example:
OPENHOST("ftp.funet.fi")
GETFILE("README")
CLOSEHOST
OPENHOST("ftp.myhost.com","myuser","mypassword")
56
PUTFILE("README")
CLOSEHOST
57
SETPROTOCOL
Set the protocol used for the FTP connection. Use this command to
enable secure FTP (FTPS).
Note: ScriptFTP currently supports FTP (common FTP) and FTPS (FTP over SSL).
SFTP will be supported in future versions.
Syntax: SETPROTOCOL(protocol)
• protocol:
58
(FTPS_EXPICIT and
FTPS_EXPLICIT_ENCRYPT_DATA) is more
widely adopted by FTP servers..
Remarks:
Changes will take effect the next time OPENHOST is invoked in the script.
Standard FTP is used by default. There is no need to invoke SETPROTOCOL in your
script if you do not want to use FTP over SSL (FTPS).
Depending on the selected protocol SETPROTOCOL will change the TCP port used for
connecting to the FTP server. Use SETPORT after calling SETPROTOCOL if you want to
set your own value.
Command History:
FTPS_EXPLICIT_ENCRYPT_DATA was added in ScriptFTP 2.1 build March 14th 2006
Return value:
This command always returns "OK".
See also:
OPENHOST
SETPORT
Example:
59
# Connect to ftp.myhost.com using secure FTP
# download sales.xls and upload it to a local server
GETFILE("sales.xls")
CLOSEHOST
CLOSEHOST
# Delete sales.xls
EXEC("del sales.xls")
60
SETPORT
Syntax: SETPORT(port)
Remarks:
Changes will take effect the next time OPENHOST is invoked in the script.
See also:
OPENHOST
SETPROTOCOL
Return value:
This command always returns "OK".
Examples:
61
Commands for file transfer
62
GETFILE
Syntax: GETFILE(file,SUBDIRS)
• file: individual file name or wildcard expression for multiple files. The
expression may include a remote directory, for example "/htdocs/*.gif"
• SUBDIRS (optional): Download subdirectories.
Remarks:
This command will overwrite local files as needed.
Use LOCALCHDIR to set the current local directory before calling GETFILE. Files will be
downloaded to that directory.
Use SETPASSIVE to disable passive transfer mode if you encounter firewall problems.
Use SETTYPE to change the data transfer type. Default is Binary (most common).
Use SYNC to download new or modified files only.
Return value:
GETFILE will return "OK" if every file has been downloaded successfully.
If the operation fails it will return an error code. You may retrieve the return value and
execute various operations depending on this value. See Error Handling.
See also:
SETPASSIVE
SETTYPE
PUTFILE
SYNC
Example:
# Connect to ftp.myftp.com
OPENHOST("ftp.myftp.com","myuser","mypassword")
63
# Download PriceList.xls
GETFILE("PriceList.xls")
# Close connection
CLOSEHOST
64
PUTFILE
Syntax: PUTFILE(file,SUBDIRS)
• file: individual file name or wildcard expression for multiple files. This
parameter may include both absolute and relative paths, for example
"C:\htdocs\*.gif or "htdocs\*.gif"
• SUBDIRS (optional): Upload subdirectories. If this parameter is supplied
PUTFILE will upload entire directory trees.
Remarks:
Return value:
PUTFILE will return "OK" if every file has been uploaded successfully. If the operation
fails it will return an error code. You may retrieve the return value and execute various
operations depending on this value. See Error Handling.
See also:
SETPASSIVE
SETTYPE
65
GETFILE
SYNC
Example:
# Connect to ftp.myftp.com
OPENHOST("ftp.myftp.com","myuser","mypassword")
# Upload C:\htdocs\mydir\PriceList.xls
# to the remote directory /excel_docs
CHDIR("/excel_docs")
PUTFILE("C:\htdocs\mydir\PriceList.xls")
# Upload all zip files with a file name starting with "backup-"
followed
# by two arbitrary characters and ending with "-old". For
example,
# backup-54-old.zip
PUTFILE("backup-??-old.zip")
CLOSEHOST
66
SYNC
Transfer new and modified files only. Optionally delete orphaned files.
67
• wildcard (optional): Use this parameter to tell SYNC to synchronize only those
files whose names match the wildcard. If this parameter is not used SYNC will
synchronize all files.
Remarks:
The SYNC command always needs write permissions on the FTP site because it will
create a temporary remote file for calculating the client-server time difference. This
value is needed to find out whether a local file is newer than its remote counterpart (or
vice versa). However, if you want to synchronize a local directory from a remote one
(download) and do not have write privileges on the server you can set this time
difference manually using the SETCLOCKDIFF command.
If DOWNLOAD_DELETE or UPLOAD_DELETE is used and SYNC is unable to delete
certain orphaned files or directories it will merely display a warning. It will not stop and
throw an error in this case.
SYNC will check whether local_dir and remote_dir already exist before starting the
synchronization. If this check fails it will display an error and abort.
Command compatibility:
ScriptFTP 2.1 Build Feb 2th 2006: The optional parameter "wildcard" is added.
ScriptFTP 2.2 Build April 4th 2006: The meaning of the third parameter has been
changed. The UPLOAD and DOWNLOAD parameters do no longer perform any file
deletion, use UPLOAD_DELETE and DOWNLOAD_DELETE instead.
Return value:
SYNC will return "OK" if all synchronization operations except from deleting orphaned
files were successful.
If SYNC fails it will return an error code. You may retrieve the return value and execute
various operations depending on this value. See Error Handling.
See also:
GETFILE
PUTFILE
SETCLOCKDIFF
68
Example:
# Close connection
CLOSEHOST
# Close connection
CLOSEHOST
# Connect to server
OPENHOST("ftp.myhost.com","myuser","mypassword")
# Close connection
CLOSEHOST
# Connect to server
OPENHOST("ftp.myhost.com","myuser","mypassword")
# Disconnect
CLOSEHOST
69
70
ADDEXCLUSION
Add files to the exclusion list. Use this command to have ScriptFTP ignore
a set of files when transferring data.
Syntax: ADDEXCLUSION(list,filename,path)
• list: ScriptFTP maintains two exclusion lists: one for uploads and another one
for downloads. Use this parameter to indicate which list you want to add the file
to.
• filename: local or remote filename (depending on the list you have selected).
Wildcards are also supported. Do not include any path in this parameter,
only file names.
• path (optional): local or remote path to the file you want to exclude. The
path must be a full path, for example C:\Docs\Whatever\ or
/remote_dir/whatever/. If you do not supply this parameter ScriptFTP will
ignore all files matching the given file name regardless of their path.
Return value:
This command will return "OK" unless wrong parameters are supplied.
Remarks:
This command is capable of ignoring directories as well. Use "/" or "\" at the end of the
file name to indicate that it is a directory. See the examples.
This command supports wildcards. You can use a wildcard expression in the file name
parameter, for Example: "*.JPG" for ignoring all jpeg files. Note that file names on FTP
servers are usually case sensitive; for example, *.jpg and *.JPG refer to different file
extensions.
71
See also:
SYNC
GETFILE
PUTFILE
CLEAREXCLUSION
Example:
# Connect to ftp.myftp.com
OPENHOST("ftp.myftp.com","myuser","mypassword")
# Download
# Do not download tmp files, ignore the directory cgi-bin
ADDEXCLUSION(DOWNLOAD,"*.tmp")
ADDEXCLUSION(DOWNLOAD,"cgi-bin/")
LOCALCHDIR("C:\dest_dir\")
GETFILE("*.*",SUBDIRS)
# Upload
# Do not upload the backup directory, ignore all
# jpg files in C:\mydir\photos
LOCALCHDIR("c:\mydir")
ADDEXCLUSION(UPLOAD,"backup\")
ADDEXCLUSION(UPLOAD,"*.jpg","C:\mydir\photos")
PUTFILE("*.*",SUBDIRS)
CLOSEHOST
OPENHOST("ftp.myserver.com","myuser","123456")
72
CLEAREXCLUSION
Syntax: CLEAREXCLUSION()
Return value:
This command always returns "OK".
Remarks:
As this command does not accept any parameters the brackets are optional.
See also:
ADDEXCLUSION
Example:
OPENHOST("ftp.myserver.com","myuser","123456")
73
SETTYPE
Syntax: SETTYPE(type)
• type:
BINARY Default and most common transfer type. Files will be transferred without
applying any transformation.
ASCII In text files Unix and Windows systems use different characters like line
feed or carriage return to indicate new lines etc. Choose this transfer type
if you want special characters contained in text files to be transformed
automatically, thus keeping text files readable on both Unix and Windows
systems.
EBCDIC Some IBM mainframes employ EBCDIC for representing text characters.
Use this transfer type to automatically transform characters and keep text
files readable.
Remarks:
Binary transfer type is used by default. If you do not use a transfer type other than
binary there will be no need to invoke SETTYPE in your script.
See also:
SETPASSIVE
GETFILE
PUTFILE
SYNC
Return value:
This command always returns "OK".
Example:
74
# Go back to BINARY transfer type and download all image files
LOCALCHDIR("C:\dest_dir")
OPENHOST("ftp.myhost.com","myuser","mypassword")
SETTYPE(ASCII)
GETFILE("textfiles/*.*")
SETTYPE(BINARY)
GETFILE("images/*.*")
CLOSEHOST
75
SETPASSIVE
Syntax: SETPASSIVE(mode)
mode:
Return value:
This command always returns "OK".
Remarks:
When connecting to an FTP server the client usually opens port 21 on the server where
the server is listening and waiting for incoming connections. You may change your FTP
server configuration such that it listens on a different port, however, port 21 is the
standard. Once the connection has been established the client will authenticate to the
server and then this connection is the one client and server will use to 'chat' with each
other. For file transfers this connection will not be used, rather a new connection will be
established for each file in order to transport the file's data. There are two methods for
opening these new data channels: Active and Passive. The purpose of the SETPASSIVE
command is to select the method that ScriptFTP will use. By default ScriptFTP uses
passive mode.
• Active Mode:
In Active mode (also called non passive) the client starts listening on port N+1
and sends the FTP command PORT N+1 to the FTP server. The server will then
connect back to this data port of the client using its own local data port, which
is port 20. The file's data will then be transferred using this connection.
76
From the client's perspective the following communication channels need to
be allowed in its own firewall in order to support active mode FTP:
In order to avoid the server having to initiate the connection to the client a
different method for FTP connections was developed. This is known as Passive
mode and it is the mode that ScriptFTP uses by default.
In Passive mode the FTP client initiates the connection to the server, thereby
solving the problem that a firewall has to filter the incoming connection from
the server to the client's data port.
• Passive mode:
The client will issue the PASV command whenever file data needs to be
transferred. As a result the server will open a random unprivileged port (P >
1024) and send a PORT P command back to the client. The client will then
initiate the connection to port number P on the server in order to transfer the
file data.
See also:
GETFILE
PUTFILE
SYNC
Example:
OPENHOST("ftp.myhost.com","myuser","mypassword")
# Use active transfer mode
SETPASSIVE(DISABLED)
GETFILE("sales.xls")
CLOSEHOST
OPENHOST("192.168.1.53")
# Go back to passive mode
SETPASSIVE(ENABLED)
78
PUTFILE("sales.xls")
CLOSEHOST
# Delete sales.xls
EXEC("del sales.xls")
79
SETSPEED
Syntax: SETSPEED(speed)
Return value:
This command always returns "OK".
See also:
GETFILE
PUTFILE
SYNC
Example:
80
SETCLOCKDIFF
Syntax: SETCLOCKDIFF(seconds)
• seconds: time difference in seconds between the FTP server and the PC where
ScriptFTP is running. It is calculated via the formula: Server time - PC time.
Remarks:
In order to synchronize (SYNC) files ScriptFTP needs to know the time difference
between the PC it is running on and the FTP server. By default the calculation of this
time difference is performed automatically when you call SYNC. However, if SYNC
reports an error calculating this time difference you will have to use SETCLOCKDIFF in
order to set it manually. Also see Transferring modified files only.
Return value:
This command always returns "OK".
Example:
# Close connection
CLOSEHOST
81
SETUPLOADMODE
Syntax: SETUPLOADMODE(mode)
TEMP This is the default mode. ScriptFTP will upload the files with a temporary filename
(appending .part to the filename). As soon as the upload has completed ScriptFTP
will rename the file to its original filename (removing the .part suffix)
DIRECT Use this mode to upload the files directly with their original filename.
Remarks:
Uploading files with DIRECT mode is not recommended. If ScriptFTP is uploading a file
that already exists on the server it will be overwritten. If the transfer is interrupted the
remote file will become corrupted. However, some FTP servers store the files being
uploaded in a temporary directory until the upload is completed by the client. If this is
true in your environment no file corruption will occur even if the transfer is interrupted
and the mode has been set to DIRECT.
See also:
SYNC
PUTFILE
Example:
# Connect to ftp.myftp.com
OPENHOST("ftp.myftp.com","myuser","mypassword")
82
# Upload all files in the directory images
PUTFILE("C:\htdocs\help\help\images\*.*")
83
Commands for directory handling
84
LOCALCHDIR
Syntax: LOCALCHDIR(path)
Remarks:
Use LOCALCHDIR to set the current local directory before calling GETFILE. Files will be
downloaded to that directory.
Return value:
LOCALCHDIR will return "OK" if ScriptFTP can access the remote directory.
If the operation fails it will return an error code. You may retrieve the return value and
execute various operations depending on this value. See Error handling.
See also:
CHDIR
LOCALCWDIR
LOCALRMDIR
LOCALMKDIR
Examples:
# Connect to server
OPENHOST("ftp.myftp.com")
# Download files
GETFILE("web/*.*",SUBDIRS)
85
# Download files
GETFILE("pricelist.xls")
# Close connection
CLOSEHOST
86
LOCALCWDIR
Syntax: LOCALCWDIR()
Remarks:
As this command does not accept any parameters the brackets are optional.
This command does not produce any output in the ScriptFTP window. It will always be
silent.
Return value:
This command never reports an error. The return value always is the current local
directory.
See also:
LOCALRMDIR
LOCALMKDIR
CWDIR
CHDIR
Example:
# Connect to server
OPENHOST("ftp.myhost.com","myuser","mypassword")
87
PRINT($a)
88
LOCALMKDIR
Syntax: LOCALMKDIR(directory)
Return value:
LOCALMKDIR will return "OK" if the local directory has been created successfully.
If the operation fails it will return an error code. You may retrieve the return value and
execute various operations depending on this value. See Error handling.
See also:
MKDIR
LOCALCWDIR
LOCALRMDIR
LOCALCHDIR
Example:
# Connect to ftp.myftp.com
OPENHOST("ftp.myftp.com","myuser","mypassword")
# Go to C:\dest_dir
LOCALCHDIR("C:\dest_dir\")
# Create C:\dest_dir\images
LOCALMKDIR("images")
CLOSEHOST
89
LOCALRMDIR
Syntax: LOCALRMDIR(directory)
• directory: relative or absolute path to the local directory you want to delete
Remarks:
Relative path means that if the current directory is c:\ you only need to give the
subdirectory, for example "windows". An absolute one means a complete path, for
example "C:\windows\".
See also:
RMDIR
LOCALMKDIR
LOCALCWDIR
LOCALCHDIR
Return value:
If this command is executed successfully it will return "OK" . If it encounters an error it
will return an error code.
Example:
90
CHDIR
Syntax: CHDIR(path)
Return value:
If this command is executed successfully it will return "OK" . If it encounters an error it
will return an error code.
See also:
CWDIR
MKDIR
RMDIR
Example:
# Connect to ftp.myftp.com
OPENHOST("ftp.myftp.com","myuser","mypassword")
CLOSEHOST
91
CWDIR
Syntax: CWDIR()
Remarks:
As this command does not accept any parameters the brackets are optional.
This command will not produce any output in the ScriptFTP window. It will always be
silent.
Return value:
The return value is the current remote directory. If ScriptFTP is not connected to an FTP
server it will return nothing.
See also:
CHDIR
MKDIR
RMDIR
LOCALCWDIR
Example:
# Connect to server
OPENHOST("ftp.myhost.com","myuser","mypassword")
# Create mydir
MKDIR("mydir")
# Go to mydir
CHDIR("mydir")
92
# Close the connection
CLOSEHOST
93
MKDIR
Syntax: MKDIR(directory)
Return value:
MKDIR will return "OK" as soon as the remote directory has been created successfully.
If the operation fails it will return an error code. You may retrieve the return value and
execute various operations depending on this value. See Error handling.
See also:
LOCALMKDIR
CHDIR
CWDIR
RMDIR
Example:
# Connect to server
OPENHOST("ftp.myhost.com","myuser","mypassword")
# Create mydir
MKDIR("mydir")
# Go to mydir
CHDIR("mydir")
94
95
RMDIR
Syntax: RMDIR(directory)
Remarks:
If the remote directory is not empty ScriptFTP will delete all files and subdirectories
within that remote directory.
Return value:
RMDIR will return "OK" as soon as the remote directory has been removed successfully.
If the operation fails it will return an error code. You may retrieve the return value and
execute various operations depending on this value. See Error handling
See also:
LOCALRMDIR
Example:
# Connect to server
OPENHOST("ftp.myhost.com","myuser","mypassword")
96
Commands for file handling
97
RENAMEFILE
Syntax: RENAMEFILE(original_name,new_name)
Remarks:
For renaming multiple files only wildcard expressions of the form *.extension are
allowed in original_name and new_name.
Return value:
RENAMEFILE will return "OK" once the files have been renamed successfully.
If the operation fails it will return an error code. You may retrieve the return value and
execute various operations depending on this value. See Error handling.
Examples:
# Connect to server
OPENHOST("ftp.myhost.com","myuser","mypassword")
# Rename foo.txt
RENAMEFILE("foo.txt","foo2.sav")
# Close connection
CLOSEHOST
# Connect to server
OPENHOST("127.0.0.1","myuser","mypass")
98
RENAMEFILE("lenore.jpg","lenore.sav")
# Shorter way:
RENAMEFILE("notes.txt","notes-".$current_date.".txt")
# Close connection
CLOSEHOST
99
DELETEFILE
Syntax: DELETEFILE(file)
Remarks:
In order to delete an entire remote directory tree use RMDIR
Return value:
If this command has been executed successfully it will return "OK" . If it encounters an
error it will return an error code.
See also:
RMDIR
Example:
# Connect to ftp.myserver.com
OPENHOST("ftp.myserver.com","myuser","mypass")
100
# Delete every html file under
# /www/images
DELETEFILE("images/*.html")
101
CHMOD
Syntax: CHMOD(mode,filename,SUBDIRS)
Remarks:
If you want to apply CHMOD to a remote directory put the directory name in the file
name field and append "/". For Example:
CHMOD(488,"/mydir/mysubdir/")
Command History:
ScriptFTP 2.0.1 Build 14 Feb 2006: Added support for applying CHMOD to directories.
ScriptFTP 2.0.1 Build 21 Feb 2006: Added the optional parameter SUBDIR for applying
CHMOD to files in subdirectories.
Return value:
CHMOD will return "OK" if the file permissions have been applied correctly. If it
encounters an error it will return an error code.
Example:
# Connect to server
OPENHOST("ftp.scriptftp.com","john","123456")
102
# Upload some files
PUTFILE("*.php",SUBDIRS)
# Close connection
CLOSEHOST
103
Handling local files
ScriptFTP does not provide any commands for copying, deleting or moving local files
because these commands are already included in the operating system. The way to
access them is using the EXEC command. For Example:
OPENHOST("ftp.host.com","myuser","mypassword")
SYNC("C:\LocalWebFolder","/www",UPLOAD)
CLOSEHOST
In the above script, we use the EXEC command to call the external program "del". The
result is the same as if you had opened a command line window and typed "del /Q
C:\LocalWebFolder". For further information about using the Windows command line
programs type "help" at the command line.
Note that some external commands such as copy, rename and del need the file
name enclosed in double quotes when it contains spaces. For example, if we
need to copy a file named Report 2008.xls to a directory whose path is
C:\Reports:
The first attempt returned an error because copy were trying to find the files
Report and 2008.xls and they do not exist. In the second attempt we enclosed
104
the file name between quotes and it worked. Copy understood because of the
quotes that both text strings were part of the same file name.
In this case, to execute the external command copy within ScriptFTP we also
have to use double quotes to enclose the file name but the problem is that it
seems that ScriptFTP also uses double quotes to separate command parameters.
So, how can this command be written in a script without making a syntax error?
Just use single quotes, ScriptFTP accepts both:
# Correct
EXEC('copy "Report 2008.xls" C:\Reports')
105
Hint: Moving remote files
The FTP protocol is not capable of moving files on the FTP server. It was merely
designed with file transfer in mind, but there is a simple trick that most FTP servers
accept for moving a file: renaming. Let us look ats an Example:
OPENHOST("ftp.myserver.com","carl","123456")
RENAMEFILE("a.txt","destination_folder/a.txt")
CLOSEHOST
Note that this trick does not work for every FTP server. It usually works for Unix/Linux
FTP servers only. If your server does not support it you can also do it downloading the
file, removing it from its original remote directory and upload it again to its destination
directory. It's a very slow way to move a remote file but at least works in every FTP
server:
106
In the above script it is advisable to add some error handling. We don't want to delete
the file from the original location if the upload or any other step failed:
107
# Upload the file filetobemoved.zip
# to its destination directory
$result=PUTFILE("filetobemoved.zip")
108
Script output commands
109
PRINT
Syntax: PRINT(text)
Return value:
This command always returns "OK".
Example:
# Shorter way:
PRINT("hello world ".GETDATE(FORMAT3))
110
111
SILENT
Syntax: Silent(mode)
• mode:
ON: When silent is enabled ScriptFTP will only show errors, transferred files
and the PRINT messages. All information other than that about the script run
will be suppressed.
OFF: Default. All messages are shown.
Return value:
This command always returns "OK".
See also:
VERBOSE
LOGTO
PRINT
Example:
112
The script output of the above example is the following. Note that the commands are
not shown:
Example:
# Connect to server
OPENHOST("ftp.host.com","myuser","mypassword")
113
SILENT(ON)
# Upload files
PUTFILE("C:\PICS\*.gif")
# Close connection
CLOSEHOST
114
VERBOSE
Syntax: VERBOSE(mode)
• mode:
OFF: This is the default. ScriptFTP will show the command calls, command
messages and any errors encountered during the script run.
ON: ScriptFTP will also show the dialog with the FTP server, DNS resolving
issues and connection loss messages. Under certain circumstances it may also
show internal messages.
Remarks:
Use this command to find out in detail what is happening during the script run.
ScriptFTP will show the following information:
Return value:
This command always returns "OK".
See also:
SILENT
LOGTO
PRINT
115
Example:
# Connect to server
OPENHOST("127.0.0.1","myuser","mypassword")
# Upload files
PUTFILE("*.*")
# Close connection
CLOSEHOST
116
LOGTO
Syntax: LOGTO(file,APPEND)
Return value:
This command will return "OK" or else an error code if something went wrong opening
the log file.
Remarks:
If the file exists it will be overwritten, use the APPEND parameter to change this
behaviour.
See also:
Logging ScriptFTP messages
Example:
LOGTO("C:\logs\TransferLog.txt",APPEND)
OPENHOST("ftp.host.com","myuser","mypassword")
PUTFILE("C:\PICS\*.jpg")
CLOSEHOST
117
# Start logging
LOGTO($logfile)
OPENHOST("ftp.host.com","myuser","mypassword")
PUTFILE("C:\PICS\*.jpg")
CLOSEHOST
118
Miscellaneous commands
119
GETLIST
Syntax: GETLIST(variable_name,list_type,filter)
• variable_name: variable GETLIST will save the resulting list to. Once GETLIST
has finished you can use this variable in FOREACH
• list_type: type of list to get.
• filter (optional): if this parameter is used GETLIST will only include items in the
listing which match the supplied wildcard.
Remarks:
Once a list is stored in a variable using this command you can perform a different action
for each item in the list using the FOREACH loop.
As all ScriptFTP variables are of type string a file listing is also regarded as a string.
Therefore GETLIST concatenates all item identifiers (file or directory names) using the
character "|" as a separator before it saves them to the variable. You may check this
output format by printing a file listing to the ScriptFTP window using PRINT. This format
is recognized by FOREACH loops.
Return value:
This command will return "OK" if the file listing has been received successfully. If your
permissions do not suffice for a file listing or if you are not connected to the server
GETLIST will return an error code. See Error handling.
Command history:
This command was added in ScriptFTP 3.0 build July 28th 2008
120
See also:
FOREACH
Examples:
# Connect to server
OPENHOST("ftp.myhost.com","joe","123456")
# Connect to server
OPENHOST("127.0.0.1","carl","123456")
121
# Get the local directory listing, store it in $list
GETLIST($list,LOCAL_DIRECTORIES)
122
GETDATE
Syntax: GetDate(format)
• FORMAT1 • DD_MM_YYYY-hh_mm
• FORMAT2 • YYYY_MM_DD-hh_mm
• FORMAT3 • YYYYMMDD
• FORMAT4 • hhmm
• YEAR • Current year in four digits format
• MONTH • Current month
• DAY • Current day
• HOUR • Current hour
• MIN • Current minute
• SEC • Current second
• WEEKDAY • Current day of week
• (hh: Hour, mm: Minute, DD: Day, MM: Month, YYYY: Year )
Return value:
The requested date data.
Remarks:
The format parameter is optional, you may call GETDATE without any
parameters. In this case it will default to FORMAT1.
Command History:
YEAR, MONTH, DAY, HOUR, MIN, SEC and WEEKDAY formats were added in
ScriptFTP v2.2 Build 4th April 2006.
Examples:
123
# Print the current date in different formats:
PRINT(GETDATE(FORMAT1))
PRINT(GETDATE(FORMAT2))
PRINT(GETDATE(FORMAT3))
PRINT(GETDATE(FORMAT4))
# Start logging
LOGTO($logfile)
OPENHOST("ftp.host.com","myuser","mypassword")
PUTFILE("C:\PICS\*.jpg")
CLOSEHOST
# Concatenate text
$mydate=$mymonth."-".$myyear
PRINT($mydate)
# Shorter way:
PRINT(GETDATE(MONTH)."-".GETDATE(YEAR))
124
GETENV
Syntax: GETENV(variable)
Examples:
# Log to C:\windows\temp\log-YYYYMMDD.txt
$logfile=GETENV("TEMP")."log-".GETDATE(FORMAT3).".txt"
LOGTO($logfile)
OPENHOST("ftp.scriptftp.com","john","123456")
PUTFILE("C:\ClientDocs\*.*",SUBDIRS)
CLOSEHOST
125
GETPARAM
Syntax: GETPARAM(param_number)
Remarks:
Use this command to pass your own command line parameters to your script file. The
first parameter is the path of the ScriptFTP executable, the second one is the script file
and all subsequent parameters are four your own use.
Note that if you call ScriptFTP from a batch file or from the command line you will have
to use double quotes in order to delimit the parameters that contain spaces. This may
sound slightly complex but is pretty simple actually. For example:
GETPARAM(5) will return parameter instead of parameter five and GETPARAM(6) will
return five. In order to solve this issue you just have to insert double quotes:
Return value:
The parameter requested. If the parameter has not been supplied on the command line
GETPARAM will return nothing.
See also:
ScriptFTP in the command line
Command History:
This command was added in ScriptFTP 2.1 build March 14th 2006
126
Example:
$param1 = GETPARAM(1)
$param2 = GETPARAM(2)
$param3 = GETPARAM(3)
$param4 = GETPARAM(4)
$param5 = GETPARAM(5)
PRINT($param1)
PRINT($param2)
PRINT($param3)
PRINT($param4)
PRINT($param5)
$param3 = GETPARAM(3)
OPENHOST("127.0.0.1","carl","123456")
PUTFILE($param3)
CLOSEHOST
127
RAWCOMMAND
Syntax: RAWCOMMAND(command)
Remarks: Some FTP servers are capable of special or non-standard operations that are
not supported directly by the set of commands ScriptFTP provides. RAWCOMMAND
allows you to execute these operations by sending the given command to the FTP
server. ScriptFTP will not carry out any further processing of the command you indicate.
It will just pass it on to the server via the FTP control connection and then wait for a
response.
Return value:
RAWCOMMAND will return the reply code sent by the FTP server.
Example:
OPENHOST("ftp.myhost.com","myuser","mypassword")
# List users
RAWCOMMAND("SITE LISTUSER")
CLOSEHOST
128
EXEC
Syntax: EXEC(command)
Remarks:
Some external commands require parameters to be enclosed in double quotes. If you
merely fill in the double quotes ScriptFTP will display a syntax error, for example:
In order to resolve this problem you will have to use ' instead of ". For example:
Return value:
This command will return the called program's exit code.
Example:
$result=EXEC("del *.mf")
IF($result!=0)
IF($result==1)
129
print("No *.mf files found. Errorcode: ".$result)
ELSE
print("Del failed. Errorcode: ".$result)
exit(1969)
END IF
ELSE
print("deleting files was successful")
END IF
130
SLEEP
Syntax: SLEEP(n)
Remarks:
If seconds is not a valid numeric value SLEEP will wait for 0 seconds.
Example:
131
STOP
Syntax: STOP()
Remarks:
Brackets are optional.
Command history:
This command is available from ScriptFTP 3.0.
Example:
$result=OPENHOST("ftp.myhost.com","myuser","mypass")
$result=OPENHOST("127.0.0.1","carlos","123456")
132
$result=LOCALCHDIR("C:\destination_dir")
IF($result!="OK")
GOTO :failed_change_local_dir
END IF
133
EXIT
Close ScriptFTP.
Syntax: EXIT(exit_status)
Remarks:
If the parameter exit_status is not used the exit status will be 0.
See also:
STOP
Example:
134
# Upload the doc files in the current local directory
$result=PUTFILE("*.doc")
# Close connection
CLOSEHOST
135
TEXTCUT
Syntax: TEXTCUT(text,start_position,length)
Return value:
This command returns the specified part of the source text. On error it returns an
empty text value.
Remarks:
The start_position parameter is not zero-based. The first character is 1, the second 2
and so on.
Use -1 as the length parameter to get the rest of the text from the start position.
If length is higher than the rest of the available text from the start position the full
remaining text will be returned.
See also:
TEXTLENGTH
Command History:
Added in ScriptFTP 3.1 build October 1th 2008
Example:
# Prints -this-
$part=TEXTCUT("this is ScriptFTP",1,4)
PRINT($part)
136
# Prints -La Mancha-
$part=TEXTCUT("In a village of La Mancha",17,9)
PRINT($part)
# Prints -123456789.txt-
$filename="prefix123456789.txt"
$filename2=TEXTCUT($filename,7,-1)
PRINT($filename2)
# Prints -123456789-
$text1="123456789"
$text2=TEXTCUT($text1,1,999)
PRINT($text2)
137
TEXTLENGTH
Syntax: TEXTLENGTH(text)
Return value:
The number of characters of the parameter text.
See also:
TEXTCUT
Command History:
Added in ScriptFTP 3.1 build October 1th 2008
Example:
# Prints 12
$count=TEXTLENGTH("whatever 123")
PRINT($count)
138
Advanced topics and rare features
This help topic documents some rare and not very commonly used ScriptFTP features.
Some users may find them useful and eventually, if any become popular, they may get
its own help topic.
It's very similar to the syntax of scriptftp_console.exe shown on this topic, but you can
also use /TRAY, /HIDE or /AUTOCLOSE in the second parameter to change the way
ScriptFTP starts or closes:
Special characters
Added in ScriptFTP 3.1. The command CHR() can be used to get a character from its
ASCII value. For example:
139
# It should print:
# abc
# def
$string = "abc".CHR(13)."def"
PRINT($string)
SYNC("C:\mylocaldir","/myremotedir",UPLOAD)
IF(GETTRANSFEREDFILESCOUNT()>0)
PRINT("Some files were transfered")
ELSE
PRINT("No files transfered")
END IF
140
Stop file logging
OPENHOST("ftp.myhost.com","myuser","123456")
LOGTO("MyDownloadlog.txt")
GETFILE("*.*")
STOPLOG()
CLOSEHOST
141
License Agreement
The following terms and conditions must be read before using a ScriptFTP Software
product. Unless you have a different license agreement signed by ScriptFTP Software
you indicate your acceptance of this license agreement and the warranty conditions by
making use of this ScriptFTP Software product.
This is not free software. You are hereby granted a license to use this software for
evaluation purposes for a period of 30 days free of charge. If you continue to use this
software after the 30-day evaluation period, a registration fee is required. When your
payment is received a registration code will be sent to you by email.
ScriptFTP single user licenses are intended for individual use of this software and you
are not allowed to install ScriptFTP on multiple workstations or servers if you purchased
this license.
ScriptFTP site licenses grant you the right to install ScriptFTP on multiple computers
that belong to the same institution. The number of installations is unlimited. You are
also eligible for priority technical support.
ScriptFTP custom development licenses belong to a personalized license agreement
signed by ScriptFTP Software. They permit you to use a customized and adapted
version of ScriptFTP.
Unregistered use of the software after the 30-day evaluation period is in violation of U.S.
and international copyright laws.
Disclaimer Warranty
This software and the accompanying files are sold "as is" and without warranties as to
performance of merchantability or any other warranties whether expressed or implied.
142
Upgrades
Upgrades are provided for free for registered customers. License agreements of later
versions supersede any prior agreements.
Distribution
Provided verification that you are distributing the Shareware Version you are hereby
licensed to make as many copies of the Shareware version of this software and
documentation as needed; distribute identical copies of the original Shareware version;
and distribute the Shareware version of the software and documentation in its
unmodified form via electronic means. There is no additional charge for any of the
above.
You are specifically prohibited from charging, or requesting donations, for any such
copies, however made; and from distributing the software and/or documentation with
other products (commercial or otherwise) without prior written permission.
143