Lists in ScriptFTP



ScriptFTP, as many script languages, does not have data types. This makes the script language and its syntax a lot easier to learn but not as powerful as others. Think of it as being “not so descriptive” and “less complex”.┬áThe drawback, in practice, is that you cannot handle things like “lists”,”sets” etc. Everything is a plain text string and you need to deal with this limitation to get the things done.

Regarding to the lists. How can the FTP script handle a list? There is a shortcut:

$my_list = "my_item_number_1|my_item_number_2|my_item_number_3"
 
FOREACH $item_name IN $my_list
     PRINT($item_name)
END FOREACH

Running the script you get:

my_item_number_1
my_item_number_2
my_item_number_3

 

 

You can store multiple text string in the same variable using the | character, also known as the “pipe character”. If you have never typed this character note that depending on the language of your keyboard it is located in a different key. If you cannot find it try searching google for “where is the pipe character in the UK keyboard?”, for example. It seems to be a typical question.

Why the pipe character? Because it is a weird character, you will not find it in file names or directories and using simply a space characters to separate the items in the list is not a valid solution because files or directories, as you know, can contain spaces.

A real example (and the most common one) to use lists in FTP scripts are file names. In fact, there is a command in ScriptFTP (GETLIST) that builds a file list in a variable for you. For instance:

# Get the remote file listing, store it in $list
GETLIST($list,REMOTE_FILES)
FOREACH $item_name IN $list
    PRINT($item_name)
END FOREACH
 
# How the list variable looks like?
PRINT($list)

Running the script you get:

index.html
styles.css
the_file.zip
index.html|styles.css|the_file.zip

 

Of course you can use lists to download files, connect to many servers etc. Sometimes lists are even passed through the command line to the FTP script directly:

 

# Get the list of servers from the command line
$server_list=GETPARAM(3)
 
# For each server....
FOREACH $server IN $server_list
 
	# Connect
	OPENHOST($server,"my_common_user","my_common_password")
 
	# Retrieve the file listing
	GETLIST($list,REMOTE_FILES)
 
	# For each file in the root directory of the server..
	FOREACH $item_name IN $list
		GETFILE($item_name)
	END FOREACH
END FOREACH

You can call the FTP script from the command line this way:

ScriptFTP.exe the_script.ftp "192.168.1.134|theserver.com|ftp.mysite.net|192.168.2.16"

GETPARAM(3) will retrieve the third parameter which is the list of servers and put it inside the script in the variable $server_list.