I am trying to upload a text file to FTP server using a batch file. It logins successfully and shows
Port command sent successfully
but after that shows
Could not open data connection to port xxxx
Connection timed out
This is the batch script:
@echo off
for %%A in (*.csv) do set latest=%%A
echo Latest file is %latest%
echo MYUSERNAME> upload.txt
echo MYPASSWORD>> upload.txt
echo asc>>upload.txt
echo put %latest% s.txt>> upload.txt
echo quit >> upload.txt
ftp -s:upload.txt server58.hostinger.in
This looks like a typical problem with an FTP active mode. The server cannot connect back to your machine to establish a data transfer connection.
That typically happens as nowadays most client machines are behind a firewall or NAT or both, what prevents the FTP active mode from working. To make the active mode working you need to open your firewall (not recommended) and/or configure NAT routing rules.
See my article on FTP modes and configuring network for an active mode.
Or you use a passive FTP mode. The Windows ftp.exe
client does not support the passive mode though, what makes it pretty useless nowadays.
So you need to use another command-line FTP client. A majority of FTP clients do support the passive mode.
For example with WinSCP your batch file would be like:
@echo off
for %%A in (*.csv) do set latest=%%A
echo Latest file is %latest%
winscp.com /command ^
"open ftp://MYUSERNAME:MYPASSWORD@server58.hostinger.in/" ^
"put -transfer=ascii %latest% s.txt" ^
"exit"
Note that WinSCP defaults to the passive mode.
For details see WinSCP guides for:
- Automating file transfers to FTP server
- Converting Windows FTP script to WinSCP script
(I'm the author of WinSCP)