1
votes

I've read a lot about automating WinSCP, but some of it I had trouble understanding because it presumed knowledge of other things, like .NET assembly, PowerShell, etc.

I'm wondering if, speaking strictly in VBScript and batch file file type of lingo, once I've downloaded the portable winscp.exe, how to simply open a remote site, give a user name and password, and download a list of the files in a particular directory. FTP protocol only.

1

1 Answers

2
votes

There's an 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>

Other than that:

  • download the WinSCP .NET assembly package and extract it along with the script.
  • register the assembly for COM. Typically like:

    %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
    
  • Run the script (list.wsf) like:

    cscript list.wsf
    

You can of course also just run winscp.com scripting like:

Set shell = CreateObject("WScript.Shell")
Set exec = shell.Exec("winscp.com /command ""open ftp://username:[email protected]/"" ""ls /remote/path"" ""exit""")

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

For more details on this approach see guide to advanced FTP scripting from VB script.