Automating WinSCP to download from FTP server

Advertisement

prasadgov
Joined:
Posts:
6

Automating WinSCP to download from FTP server

Hi All,
I am trying to automate my WinSCP script to download files from an FTP server from Access VBA

This is my WinSCP code and text file
Private Sub Command1_Click()
Dim strQuote As String
strQuote = Chr(34)
Dim strSFTPDir As String
strSFTPDir = "C:\Program Files (x86)\Winscp\"
Dim strCommand As String
strCommand = "/script=C:\Daily\inputs\dtccwinscpfile.txt"     
Call Shell(strSFTPDir & "winscp.exe " & strQuote & strCommand & strQuote, vbNormalNoFocus)
End Sub
This is my script text file
open 10.xxx.x.xx
2852
pwd3478!#
cd ftphome
lcd DeliveryOrders
mget  *.ftprcv \\10.xxx.xx.xx\backupshare\205\Reports\DTCC\dtcc\*.txt
quit
It is not retrieving any files. Is the WinSCP command script correct?
The mget downloads the files which are in FTPRCV type and saves it as .txt
What am I doing wrong?

When I use winscp manually from GUI, I am able to get these files.
In our organization, they have a version of WinSCP on which the Generate code and other features are disabled :(
Also, how do I direct the code to a log file?

TIA

Reply with quote

Advertisement

prasadgov
Joined:
Posts:
6

I now modified the code as
open ftp://2852:pwd3478!#@10.xxx.x.xx
cd ftphome
lcd DeliveryOrders\Made
mget  20240628001.FTPRCV \\10.xxx.xx.xx\backupshare\205\Reports\DTCC\dtcc\
quit
Private Sub Command1_Click()
    Dim strQuote As String
    strQuote = Chr(34)
    Dim strSFTPDir As String
    strSFTPDir = "C:\Program Files (x86)\Winscp\"
    Dim strCommand As String
    strCommand = "/script=C:\Daily\inputs\dtccwinscpfile.txt"     
    Call Shell(strSFTPDir & "winscp.exe " & strQuote & strCommand & strQuote, vbNormalNoFocus)
End Sub
But I am unable to download.

Reply with quote

martin
Site Admin
martin avatar
Joined:
Posts:
41,262
Location:
Prague, Czechia

Please attach a full session log file showing the problem (using the latest version of WinSCP).

To generate the session log file, use /log=C:\path\to\winscp.log command-line argument. Submit the log with your post as an attachment. Note that passwords and passphrases not stored in the log. You may want to remove other data you consider sensitive though, such as host names, IP addresses, account names or file names (unless they are relevant to the problem). If you do not want to post the log publicly, you can mark the attachment as private.

Reply with quote

prasadgov
Joined:
Posts:
6

Re: Automating WinSCP to download from FTP server

hello,

I was going through few of your examples using winscp.exe.

I am trying to execute this with my Access command Click button

My issue is how to insert a log command to check the flow. Also, the command runs but opens a winscp GUI and does not download files.
Is that the correct way to use a log file? Also is the console command necessary inside the code after winscp.exe?
Private Sub Command1_Click()
Dim strQuote As String
Dim scriptpath As String
Dim result As String
Dim strSFTPDir As String
strSFTPDir = "C:\Program Files (x86)\WinSCP\WinSCP.exe"
scriptpath = "C:\Daily liquidity report\inputspipe\getdtcc.txt"
Dim strCommand As String
strCommand = """" & strSFTPDir & """ / script = """ & scriptpath & """/log=getdtcc.log & """""""
result = Shell(strCommand, vbNormalFocus)
If result = 0 Then
    MsgBox "Download Failed"
Else
    MsgBox " Download Successful!"
End If
 
End Sub
This is the content of my script file
# Connect
open ftp://usera:xxxx3478!#@10.xxx.8.xx/
# Change remote directory
cd /
cd /dtccdeliveryorders
# Change local directory
# lcd C:\software\winscp\files\
# Download file to the local directory d:\
get *.FTPRCV C:\Daily liquidity report\inputspipe\DTCC\dtcc\
# Disconnect
close
# Exit WinSCP
exit

TIA

Reply with quote

Advertisement

Advertisement

You can post new topics in this forum