使用VBS对CuteFTP进行二次开发

使用VBS对CuteFTP进行二次开发

Roger Yang

近日,需要编写一个FTP客户端程序,用于定时同步本地和远端的下载和上传目录。虽说原来使用BCB开发过FTP客户端程序。但这次时间紧,而且,日后须由不熟悉BCB的开发人员来维护源码。所以,想利用现有FTP客户端软件的功能来实现需求。

起初,试用了SMARTFTP,基本能满足下载和上传得需求,而且能通过计划任务功能实现定时效果。但不足之处是自定义上还不够灵活,不能对整个处理流程进行随意控制。

    之后,试用了老牌ftp客户端软件cuteFTP(我使用的版本是CuteFTP 6.0 Professinal)。果然,姜还是老的辣,CuteFTP内置的Transfer Engine,使它从一个FTP客户端软件升级为一个开发平台。

 

 

 

What is the Transfer Engine?

Built on a modular design platform, CuteFTP 6 Professional's FTP Transfer Engine (TE) is completely independent of the main application's interface. You can control the TE through an industry standard COM (Component Object Model) interface using your favorite programming or scripting language, such as Visual Basic, Perl, ASP or JavaScript.

 

 

 

需要了解更多有关Transfer Engine的信息,可以访问CuteFTP的在线知识库

http://help.globalscape.com/help/cuteftppro6/

 

 

 

    我使用VBScript来编写TE的控制代码,不仅能实现上传下载的需求,还能把日志等信息写入数据库或本地文件。

准备过程中参考了如下的信息:

a)       TE的方法:
    http://help.globalscape.com/help/cuteftppro6/finding_a_method_by_category.htm

b)       TE的属性:
   
http://help.globalscape.com/help/cuteftppro6/finding_a_property_by_category.htm

c)       VBS的数据库操作类:
   
http://dev.csdn.net/article/31/31454.shtm

d)       VBS 函数与对象:
   
http://rivuletblog.blogchina.com/1598901.html

e)       遍历一个文件夹里的文件:
   
http://www.yingku.com/info/187.htm

f)       FileSystemObject 访问文件:
   
http://msdn.microsoft.com/library/chs/default.asp?url=/library/CHS/vbcn7/html/vbconintroductiontofilesystemobjectmodel.asp

g)       FileSystemObject 示例代码:
   
http://www.clde.net/Document/VBS/page/2005819/VBS_2005819164447.shtml

通过使用Windows计划任务来执行脚本,就能够实现定时的功能。具体的设置方法可以参考如下连接:

http://help.globalscape.com/help/cuteftppro7/running_scripts_from_the_windows_scheduled_tasks_folder.htm


   使用WinXP的计划任务来实现定时运行脚本的功能,实现方法如下:
   To schedule a task

1.       In Windows, click Start.

2.       Choose Programs > Accessories > System Tools > Scheduled Tasks.

3.       Double-click Add Scheduled Task. The Scheduled Task Wizard appears.

4.       Click Next.

5.       Click Browse. The Select Program to Schedule window appears.

6.       Select your script and click Open. You are returned to the Scheduled Task Wizard.

7.       Choose how often to run the script and click Next.

8.       Select a starting time, recurrence, and starting date and click Next.

9.       Enter a user name and password for the account that will run the script, and click Next.

10.    Click Finish.

1.       In Windows, click Start.

2.       Choose Programs > Accessories > System Tools > Scheduled Tasks.

3.       Double-click Add Scheduled Task. The Scheduled Task Wizard appears.

4.       Click Next.

5.       Click Browse. The Select Program to Schedule window appears.

6.       Select your script and click Open. You are returned to the Scheduled Task Wizard.

7.       Choose how often to run the script and click Next.

8.       Select a starting time, recurrence, and starting date and click Next.

9.       Enter a user name and password for the account that will run the script, and click Next.

10.    Click Finish.


   使用WinXP的计划任务来实现定时运行脚本的功能,实现方法如下:
   To schedule a task

1.       In Windows, click Start.

2.       Choose Programs > Accessories > System Tools > Scheduled Tasks.

3.       Double-click Add Scheduled Task. The Scheduled Task Wizard appears.

4.       Click Next.

5.       Click Browse. The Select Program to Schedule window appears.

6.       Select your script and click Open. You are returned to the Scheduled Task Wizard.

7.       Choose how often to run the script and click Next.

8.       Select a starting time, recurrence, and starting date and click Next.

9.       Enter a user name and password for the account that will run the script, and click Next.

10.    Click Finish.

1.       In Windows, click Start.

2.       Choose Programs > Accessories > System Tools > Scheduled Tasks.

3.       Double-click Add Scheduled Task. The Scheduled Task Wizard appears.

4.       Click Next.

5.       Click Browse. The Select Program to Schedule window appears.

6.       Select your script and click Open. You are returned to the Scheduled Task Wizard.

7.       Choose how often to run the script and click Next.

8.       Select a starting time, recurrence, and starting date and click Next.

9.       Enter a user name and password for the account that will run the script, and click Next.

10.    Click Finish.

 

实现代码如下:

' -----------------------------------------------------------------------------
'   下载远程目录下的所有文件,并删除远端已下载的文件
'   上载本地目录下的所有文件,并删除本地已上传的文件
'  
'   把错误日志写入数据库的方式未定
'   过滤器的处理方式未定
'   文件已存在时的处理策略未定
'
'   Edit History:
'
'       2005/09/01 - Created by RogerYang.
'
' -----------------------------------------------------------------------------

Dim MySite

Dim strFileList     '文件列表
Dim strFileName     '文件名
Dim i, j

Dim objFSO, objFolder, objFile

Dim objConn         ' 数据库连接

' Create TEConnection object
Set MySite = CreateObject("CuteFTPPro.TEConnection")

'disable ATL exceptions
MySite.Option("ThrowError") = false

' Initialize remote server host name, protocol, port, etc.
MySite.Host = "172.16.11.129"
MySite.Protocol = "FTP"
MySite.Port = 21
MySite.Retries = 30
MySite.Delay = 30
MySite.MaxConnections = 2
MySite.TransferType = "AUTO"
MySite.DataChannel = "DEFAULT"
MySite.AutoRename = "OFF"

' WARNING!!! SENSITIVE DATA: user name and password.
MySite.Login = "yang"
MySite.Password = "123456"
MySite.SocksInfo = ""
MySite.ProxyInfo = ""

' Connect to remote server
If CBool(MySite.Connect) Then

    ' 设置远程包含过滤器
    MySite.RemoteFilterInclude = ""
    ' 设置远程排斥过滤器
    MySite.RemoteFilterExclude = ""
    MySite.RemoteSiteFilter = ""

    ' -----------------------------------------------------------------------------
    ' 下载
    ' -----------------------------------------------------------------------------
    ' 设置远程下载目录和本地下载目录
    MySite.RemoteFolder = "/e:/private/download"
    MySite.LocalFolder = "D:/ftpdown/download"
   
    ' 判断远程下载目录是否存在
    If CBool(MySite.RemoteExists(MySite.RemoteFolder)) Then
       
        ' 判断本地下载目录是否存在
        If CBool(MySite.LocalExists(MySite.LocalFolder)) Then

            ' 获取远程下载目录的文件列表,以"|||"作为分隔符
            MySite.GetList "", "", "%NAME|||"
            strFileList = MySite.GetResult

            '  判断远程下载目录中是否有文件
            If Len(strFileList) <> 0 Then
                ' 远程下载目录中有文件,进行下载操作

                'MsgBox strFileList

                ' 通过循环,下载并删除全部文件
                i = 1
                Do While true

                    j = InStr(i, strFileList, "|||")

                    'MsgBox "i=[" & i & "]"
                    'MsgBox "j=[" & j & "]"

                    If j <= 0 Then
                        ' 从文件列表中获取文件名完毕,退出循环
                        Exit Do
                    End If

                    ' 从文件列表中得到文件名
                    strFileName = Mid(strFileList, i, j - i)

                    'MsgBox "strFileName=[" & strFileName & "]"

                    ' 下载指定文件
                    MySite.Download strFileName

                    ' 从服务器上删除指定文件
                    MySite.RemoteRemove strFileName

                    ' 加5,因为分隔符"|||"的长度为3个字符,另外还有回车和换行2个字符
                    i = j + 5

                Loop

            Else
                ' 远程下载目录中没有文件

            End If

        Else
            ' 本地下载目录不存在
            MsgBox "错误! 本地下载目录不存在"
        End If
       
    Else
        ' 远程下载目录不存在
        MsgBox "错误! 远程下载目录不存在"
    End If


    ' -----------------------------------------------------------------------------
    ' 上传
    ' -----------------------------------------------------------------------------
    ' 设置远程上传目录和本地上传目录
    MySite.RemoteFolder = "/e:/private/upload"
    MySite.LocalFolder = "D:/ftpdown/upload"

    ' 判断远程上载目录是否存在
    If CBool(MySite.RemoteExists(MySite.RemoteFolder)) Then

        ' 判断本地上载目录是否存在
        If CBool(MySite.LocalExists(MySite.LocalFolder)) Then

            Set objFSO = CreateObject("Scripting.FileSystemObject")
            Set objFolder = objFSO.GetFolder(MySite.LocalFolder)
       
            '通过循环,上载并删除全部文件
            For Each objFile in objFolder.Files
       
                'MsgBox objFile.Name
       
                ' 上载文件
                MySite.Upload objFile.Name
       
                ' 文件上载之后,就删除
                objFile.Delete
            Next
       
            Set objFolder = nothing
            Set objFSO = nothing        ' 释放FileSystemObject对象实例内存空间
        Else
            ' 本地上载目录不存在
            MsgBox "错误! 本地上载目录不存在"
        End If
       
    Else
        ' 远程上载目录不存在
        MsgBox "错误! 远程上载目录不存在"
    End If

    ' 使用如下代码可以进行数据库操作,从而把日志等信息存入数据库。
    'Set objConn = CreateObject("ADODB.Connection")
    'objConn.Open "Driver={sql server};server=172.16.11.134;database=yang;uid=sa;pwd=123456;"
    'objConn.Execute "INSERT INTO error_log(error_date,error_log) VALUES('aaa','bbb')"
    '
    'Set objConn = nothing

    ' 使用如下代码可以进行文件操作,从而把日志等信息存入本地文件。
    'Set FSO = CreateObject("Scripting.FileSystemObject")
    'Set TextStream = FSO.CreateTextFile("C:/ftptest.txt")
    'TextStream.WriteLine("FTP TEST")
    'TextStream.Close
Else
    ' 与FTP服务器连接失败
    MsgBox "错误! " & MySite.ErrorDescription
End If


MySite.Disconnect

你可能感兴趣的:(1.5,-,VB)