THE INFORMATION IN THIS ARTICLE APPLIES TO:
DISCUSSION
When incorporating CuteFTP's Transfer Engine into an automated process solution by way of an executable file (such as a Visual FoxPro or Visual Basic project) or script (such as a VBScript), it can be very helpful to create consolidated log files to track progress, status, successes, failures, etc. However, it's possible that you want the log to contain certain information that may not be possible for CuteFTP to log, such as if object creation failed or to log in patterns that it may not support by default, such as a single, daily log file.
One of the easiest ways to accomplish this goal is to use objects built into the Windows Script Host, such as the FileSystemObject object. It allows you to perform file system functions such as opening a new text file for editing and writing information to that text file. Of course, others may want to log to an ODBC source instead or perhaps to some other destination, such as the Event Log. Some WSH objects already exist, while some require you to provide your own tools. In the example below, we'll examine writing lines to a text file, thereby achieving a log.
Our plan is to have a daily log file. We will attempt to trap most errors, write CuteFTP's own log data to disk, and include timestamps on everything. It will hopefully be easy and clear enough for human operators to read and understand what happened and when. Please note that the following VBScript code was not written by a professional developer, but an interested hobbyist. It has been tested and verified to work.
'Declare our variables
Dim YYYY, MM, DD, M, D
Dim
fso, tf
Dim MySite
'Prepare a consistent YYYY-MM-DD format
YYYY = Year(Date)
M = Month(Date)
if
(Len(M)=1) then MM = "0" & M
else MM = "" & M
D = Day(Date)
if
(Len(D)=1) then DD = "0" & D
else DD = "" & D
'Allow for error trapping
On Error Resume Next
'Create the FileSystemObject object and echo failures to the console
if run from the command line via 'cscript or to prompts if run interactively
Set fso = CreateObject("Scripting.FileSystemObject")
If err.Number <> 0 Then
WScript.Echo (Now & " - Failed to
create FileSystem Object!")
WScript.Echo (Now & " - Description: " & Err.Description)
WScript.Echo (Now & " - Error Source: " & Err.Source)
WScript.Quit(255)
End If
' Open text file (create if necessary) for writing
called something like CuteFTP_2006-03-24.log in C:\
Set tf = fso.OpenTextFile("C:\CuteFTP_"
& YYYY & "-" & MM & "-" & DD & ".log", 8, True)
If err.Number <> 0 Then
WScript.Echo (Now & " - Failed to
open file for writing!")
WScript.Echo (Now & " - Description: " & Err.Description)
WScript.Echo (Now & " - Error Source: " & Err.Source)
WScript.Quit(255)
End If
'Now that this has been successful, we can begin writing lines to our
log file
'Create TEConnection object
Set MySite = CreateObject("CuteFTPPro.TEConnection")
If err.Number <> 0 Then
tf.WriteLine (Now & " - Failed to
create TEConnection object!")
tf.WriteLine (Now & " - Description: " & Err.Description)
tf.WriteLine (Now & " - Error Source: " & Err.Source)
tf.Close WScript.Quit(255)
End If
MySite.Host = "ftp.example.com"
MySite.Protocol = "FTP"
MySite.Port = 21
tf.WriteLine (Now &
"*********************************************")
tf.WriteLine (Now & " - Connecting...")
MySite.Connect
If err.Number <> 0 Then
tf.WriteLine (Now & " - Connection
Error!")
tf.WriteLine (Now & " - Description: " & Err.Description)
tf.WriteLine (Now & " - Error Source: " & Err.Source)
tf.WriteLine (Now & " - LOG FOLLOWS:")
tf.WriteLine MySite.Log
Else
tf.WriteLine (Now & " - Connected
successfully!")
tf.WriteLine (Now & " - LOG FOLLOWS:")
tf.WriteLine MySite.Log
End If
tf.WriteLine (Now &
"*********************************************")
tf.Close
MySite.Close
That will result in output like the following:
3/24/2006 3:38:06 PM -
*********************************************
3/24/2006 3:38:06 PM - Connecting...
3/24/2006 3:38:08 PM - Connection Error!
3/24/2006 3:38:08 PM - Description: Connect failed: "ftp.example.com".
3/24/2006 3:38:08 PM - Error Source: CuteFTPPro.TEConnection.6
3/24/2006 3:38:08 PM - LOG FOLLOWS:
*** CuteFTP 7.0 - build Jun 7 2005 ***
STATUS:> [3/24/2006 3:38:06 PM] Resolving host
name ftp.example.com...
ERROR:> [3/24/2006 3:38:08 PM] Can't resolve "ftp.example.com".
3/24/2006 3:38:08 PM -
*********************************************
All tf.WriteLine statements will be made to today's log. Of course, it's recommended that timestamps are enabled in the Log Files section CuteFTP Pro's Global Options. Additionally, the MySite.Log property isn't always available depending on your Logs settings. See the following screenshot for an example working configuration: