Batch command to check if FTP connection is succes

2020-02-13 05:03发布

问题:

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.

回答1:

Do something like this:

YourFTPCommand | find /i /v "220 server ready" && goto :ServerNotReady

Explanation:

  1. Pipe the output of your FTP command to FIND
  2. Do a case insensitive (/i) search for output that does not contain (/v) the string "220 server ready"
  3. Go to someplace if such a string is found (&&)


回答2:

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)