1
votes

I've created a script to download a file from an FTP server using VBA to fire off the script. I can connect to the server and navigate to the correct directory, but when I got to get the file I get the message:

500 Port command invalid

I've tweaked the code as much as I can but I don't understand enough about ports or FTP to know what I need to change.

The VBA code:

Dim FTPcommand As String
Dim wsh As Object

FTPcommand = "ftp -n -s:" & Chr(34) & "C:\path\to\FTP_commands.txt" & Chr(34)
Set wsh = CreateObject("WScript.Shell")
wsh.Run FTPcommand, 5, True

The script:

open ftp.blahblahbah.bl
user username password
lcd C:\Users\me\Documents\location
cd /specific location/
get "file.xlsx"

I expect the file to download but get the following messages (after the script logs in and successfully navigates to the filepath):

ftp> open ftp.address.com
Connected to ftp.address.com
220 Welcome to address.com
ftp> user USERNAME PASSWORD
---> USER USERNAME
331 User USERNAME, password please
---> PASS PASSWORD
230 Password Ok, User logged in
ftp lcd C:\Users\joe\Documents
Local directory now C:\Users\joe\Documents.
ftp> cd /location/
--->CWD /location/
250 Change directory ok
ftp get "file.xlsx"
---> PORT 192.168.1.121.242.113
500 Port command invalid
RETR file.xlsx
425 Unable to open the data connection

1
Add -d switch to ftp command-line and show us a complete output.Martin Prikryl
Thanks Martin- now updated above.joebody's business

1 Answers

1
votes

---> PORT 192.168.1.121.242.113
500 Port command invalid

It looks like the ftp is sending an internal IP address to an FTP server in another network. The server cannot connect to that IP address. I do not think, there's a way to solve it with ftp. The firewall/NAT of your network could solve it by translating IP address in the PORT command.


Or you need to use a different FTP client, which supports the FTP passive mode.

You can use WinSCP FTP client. There's a guide to converting Windows ftp script to WinSCP.

The following WinSCP code is equivalent to yours:

Dim FTPcommand As String
Dim wsh As Object

FTPcommand = """C:\Path\To\winscp.com"" /ini=nul /script=""C:\Path\To\FTP_commands.txt"""
Set wsh = CreateObject("WScript.Shell")
wsh.Run FTPcommand, 5, True

The script:

open ftp://username:[email protected]/
lcd C:\Users\me\Documents\location
cd /specific location/
get "file.xlsx"
exit

(I'm the author of WinSCP)