Topic Options
#1332 - 12/23/00 08:35 AM Chapter H – FTP system
Luke Tomasello Administrator Online   content
Member

Registered: 09/17/00
Posts: 739
Loc: San Jose, CA., USA
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
}
_________________________
Regards,
Luke Tomasello

Top
#1333 - 09/07/01 12:05 PM Re: Chapter H – FTP system
Luke Tomasello Administrator Online   content
Member

Registered: 09/17/00
Posts: 739
Loc: San Jose, CA., USA
Chapter H - FTP system, Part II

In this example, we use only the BASIC FTP support. If you wish to see a complete example that utilizes the advanced features, please see ftp_master_test.tg located in the Sample Scripts folder.

## ftp_basic_test.tg
# Perform many FTP functions while handling exceptions.
# Basically, this script does Get, Put, and Delete as well as Rename and Test.
# script assumes:
# 1. There is a temp/foo directory on the remote server
# 2. There is a temp/foo/test_file.dat file on the remote server
# 3. There is a c:\temp\foo local directory (tree) filled with all the files and directories
# you wish to move back and forth.
# 4. 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 this script or hard-code the
# values passed to -ftp connect.
{
-name ftp_basic_test

# schedule to run every minute starting now
-start -inc 0 0 1 0

# tell user what we are about to do
-action -print ftp basic test

# setup local environment
-action -set LOCAL_DIR=c:\temp\foo

# I set these in my global environment, you can set them here
#-action -set USER.SERVER=xxx
#-action -set USER.USERID=yyy
#-action -set USER.PASSWORD=zzz

# CONNECT TO FTP SERVER
-action -print connecting to remote ftp server
-action -onerror connect_fail
-action -ftp connect handle %USER.SERVER% 21 %USER.USERID% %USER.PASSWORD%

# DEBUG
#-action -print lastline: %FTP.LASTLINE%

# TEST FOR "test-file" ON REMOTE SERVER
-action -print testing for "test-file" on remote ftp server
-action -set OPERATION=test1
-action -onerror general_failure
-action -ftp test handle temp/foo/test_file.dat

# DEBUG
#-action -print lastline: %FTP.LASTLINE%

# GET A FILE FROM REMOTE SERVER
-action -print getting file from remote ftp server
-action -set OPERATION=get
-action -onerror general_failure
-action -ftp get handle temp/foo/test_file.dat %LOCAL_DIR%\test_file.dat 0

# DEBUG
#-action -popup Did we get test_file.dat to %LOCAL_DIR%\test_file.dat?
#-action -print lastline: %FTP.LASTLINE%

# DELETE THE test-file FROM REMOTE SERVER
-action -print deleting the file from remote ftp server
-action -set OPERATION=del
-action -onerror general_failure
-action -ftp del handle temp/foo/test_file.dat

# DEBUG
#-action -popup Has temp/foo/test_file.dat been deleted?
#-action -print lastline: %FTP.LASTLINE%

# PUT A FILE ON REMOTE SERVER
-action -print putting file back on remote ftp server
-action -set OPERATION=put
-action -onerror general_failure
-action -ftp put handle %LOCAL_DIR%\test_file.dat temp/foo/test_file.dat 0

# DEBUG
#-action -popup Is temp/foo/test_file.dat back again?
#-action -print lastline: %FTP.LASTLINE%

# TEST FOR "test-file" ON REMOTE SERVER
-action -print testing for "test-file" on remote ftp server
-action -set OPERATION=test2
-action -onerror general_failure
-action -ftp test handle temp/foo/test_file.dat

# DEBUG
#-action -print lastline: %FTP.LASTLINE%

# RENAME "test-file" ON REMOTE SERVER
-action -print rename the test-file on remote ftp server
-action -set OPERATION=rename
-action -onerror general_failure
-action -ftp rename handle temp/foo/test_file.dat temp/foo/test_file.bak

# DEBUG
#-action -popup Was temp/foo/test_file.dat renamed to temp/foo/test_file.bak?
#-action -print lastline: %FTP.LASTLINE%

# TEST FOR test_file.bak ON REMOTE SERVER
-action -print testing for test-file.bak on remote ftp server
-action -set OPERATION=test3
-action -onerror general_failure
-action -ftp test handle temp/foo/test_file.bak

# DEBUG
#-action -print lastline: %FTP.LASTLINE%

# RENAME "test-file" ON REMOTE SERVER
-action -print rename the test-file on remote ftp server
-action -set OPERATION=rename
-action -onerror general_failure
-action -ftp rename handle temp/foo/test_file.bak temp/foo/test_file.dat

# DEBUG
#-action -popup has temp/foo/test_file.dat been restored?
#-action -print lastline: %FTP.LASTLINE%

# TEST FOR "test-file" ON REMOTE SERVER
-action -print testing for "test-file" on remote ftp server
-action -set OPERATION=test4
-action -onerror general_failure
-action -ftp test handle temp/foo/test_file.dat

# DEBUG
#-action -print lastline: %FTP.LASTLINE%

# CLOSE FTP CONNECTION
-action -print closing ftp connection
-action -ftp close handle

# DEBUG
#-action -print lastline: %FTP.LASTLINE%

# cool, we made it!
# We'll run again in one minute
-action -print done!
}

# connection failed
{
-name connect_fail
-action -print FTP open connection failed with error:
-action -print %FTP.ERROR%
-action -return abort
}

# general failed
{
-name general_failure
-action -print FTP %OPERATION% failed with error:
-action -print %TG.LAST_ERROR%
-action -print Closing connection.
-action -ftp close handle
-action -return abort
}
_________________________
Regards,
Luke Tomasello

Top


Moderator:  Luke Tomasello