Archives
- July 2019
- January 2019
- November 2018
- August 2018
- July 2018
- February 2018
- November 2017
- October 2017
- July 2017
- June 2017
- May 2017
- April 2017
- January 2017
- December 2016
- November 2016
- August 2016
- January 2015
- December 2014
- March 2014
- April 2013
- December 2010
- November 2009
- September 2009
- June 2009
- March 2009
- February 2009
- November 2008
- October 2008
- August 2008
- July 2008
- January 2008
Lists in ScriptFTP
24th November 2016ScriptFTP, 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.