I am writing a batch command to send data via FTP. Before sending the actual data I need to find if the FTP server is active/running. How do I check that in batch command?
The server responds with "220 server ready" message when it is connected.
I am writing a batch command to send data via FTP. Before sending the actual data I need to find if the FTP server is active/running. How do I check that in batch command?
The server responds with "220 server ready" message when it is connected.
Do something like this:
YourFTPCommand | find /i /v "220 server ready" && goto :ServerNotReady
Explanation:
I do not think there's a reliable way to do this with Windows ftp.exe
.
It blindly keeps running the commands, no matter if connection or previous commands succeeded.
It won't even report the result via exit code.
All you can do is to parse the ftp.exe
output.
You should better use a 3rd-party FTP client.
For example with WinSCP scripting you can use:
@echo off
winscp.com /log=ftp.log /command ^
"open ftp://user:password@example.com/" ^
"put c:\local\path\file.txt" ^
"exit"
If connection (the open
command) fails, WinSCP won't execute the following commands (the put
).
See also Converting Windows FTP script to WinSCP FTP script.
(I'm the author of WinSCP)