自动化WinSCP以获取远程目录

时间:2016-10-25 01:37:38

标签: batch-file vbscript winscp

我已经阅读了很多关于自动化WinSCP的内容,但其中一些我很难理解,因为它假定了其他东西的知识,比如.NET程序集,PowerShell等。

我想知道,如果我已经下载了便携式winscp.exe,如果只是简单地打开一个远程站点,给出一个用户名,是否严格使用VBScript和批处理文件文件类型的语言和密码,并下载特定目录中的文件列表。仅限FTP协议。

1 个答案:

答案 0 :(得分:2)

example for using the Session.ListDirectory from VBScript

<job>                                                               
<reference object="WinSCP.Session"/>
<script language="VBScript">

Option Explicit

' Setup session options
Dim sessionOptions
Set sessionOptions = WScript.CreateObject("WinSCP.SessionOptions")
With sessionOptions
    .Protocol = Protocol_Ftp
    .HostName = "ftp.example.com"
    .UserName = "user"
    .Password = "mypassword"
End With

Dim session
Set session = WScript.CreateObject("WinSCP.Session")

' Connect
session.Open sessionOptions

Dim directoryInfo
Set directoryInfo = session.ListDirectory("/remote/path")

Dim fileInfo
For Each fileInfo In directoryInfo.Files
    WScript.Echo fileInfo.Name & " with size " & fileInfo.Length & _
        ", permissions " & fileInfo.FilePermissions & _
        " and last modification at " & fileInfo.LastWriteTime
Next

' Disconnect, clean up
session.Dispose

</script>
</job>

除此之外:

  • download the WinSCP .NET assembly package并将其与脚本一起提取。
  • register the assembly for COM。通常喜欢:

    %WINDIR%\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe WinSCPnet.dll /codebase /tlb:WinSCPnet32.tlb
    %WINDIR%\Microsoft.NET\Framework64\v4.0.30319\RegAsm.exe WinSCPnet.dll /codebase /tlb:WinSCPnet64.tlb
    
  • 运行脚本(list.wsf),如:

    cscript list.wsf
    

您当然也可以像winscp.com scripting那样运行:

Set shell = CreateObject("WScript.Shell")
Set exec = shell.Exec("winscp.com /command ""open ftp://username:password@ftp.example.com/"" ""ls /remote/path"" ""exit""")

WScript.Echo(exec.StdOut.ReadAll())

有关此方法的详细信息,请参阅guide to advanced FTP scripting from VB script