FTP, like several other plugin services, is dynamically loaded by the main WinCron interpreter.
There are two variants of the FTP support in WinCron: WS_FTP (commercially available 3rd party FTP client) and standard Windows built-in FTP support.
Currently the support for WS_FTP is far more powerful than the BASIC FTP because you get recursive directory traversal and wildcard pattern matching. This however, will not always be the case as I plan on adding the richness of WS_FTP to the BASIC FTP.
The basic flow is as follows:
# get a file
{
-start
-stop
-action -ftp
connect handle %USER.SERVER% 21 %USER.USERID% %USER.PASSWORD%
-action -ftp
get handle temp/foo/test_file.dat c:\temp\foo\test_file.dat 0
-action -ftp
close handle
}
Now I’ve removed my usual error handling, print statements and comments so you can see how easy FTP can be.
Before I show you the fully decorated version of this script, lets talk about the
handle parameter shown here.
One of the truly beautiful things about WinCron scripts is the typelessness, and dynamic interpretation of variables.
That is, the variable
handle is never declared, you have no idea what type it is, and you never need to look at it. You simply create a variable dynamically by using it, which keeps your scripts small and to-the-point.
So when you think about handle usage in WinCron, think about them as a way to distinguish between two or more named sessions.
For Example:
# get a file
{
-start
-stop
-action -ftp connect session_1 %USER.SERVER% 21 %USER.USERID% %USER.PASSWORD%
-action -ftp connect session_2 %USER.SERVER% 21 %USER.USERID% %USER.PASSWORD%
-action -ftp get session_1 temp/foo/test_file_1.dat c:\temp\foo\test_file_1.dat 0
-action -ftp get session_2 temp/foo/test_file_2.dat c:\temp\foo\test_file_2.dat 0
-action -ftp close session_1
-action -ftp close session_2
}
Okay, enough about handles, lets look at the complete FTP ‘get’ script complete with error handling.
## ftp_get.tg
# file Get
# script assumes:
# 1. There is a temp/foo/test_file.dat file on the remote server.
# 2. There is a c:\temp\foo local directory.
# 3. the environment variables: USER.SERVER, USER.USERID, and USER.PASSWORD are set elsewhere,
# perhaps in the global (computer) environment or the local environment of WinCron via
# Config.tg or AutoRun.tg. You could also set them here in this script or hard-code the
# values passed to -ftp connect.
# get a file
{
-name ftp_get
-start
-stop
-action -print connecting to remote ftp server
-action -onerror connect_fail
-action -ftp connect handle %USER.SERVER% 21 %USER.USERID% %USER.PASSWORD%
-action -print getting file from remote ftp server
-action -onerror operation_fail
-action -ftp get handle temp/foo/test_file.dat c:\temp\foo\test_file.dat 0
-action -ftp close handle
-action -print done!
}
# connection failed
{
-name connect_fail
-action -print FTP open connection failed with error:
-action -print %TG.LAST_ERROR%
-action -return abort
}
# operation failed
{
-name operation_fail
-action -print FTP get failed with error:
-action -print %TG.LAST_ERROR%
-action -print Closing connection.
-action -ftp close handle
-action -return abort
}