To search for a file at FTP

2019-03-02 13:30发布

问题:

I want to find if a file exists at FTP using if-exist filename -else statement using FTP batch script which is as follows:

ftp.txt open ftp.mysite.com
ftp.txt username
ftp.txt password
ftp.txt if exist filename (echo file exists) else (echo file doesn't exist)
ftp.txt quit
ftp -s:ftp.txt

the if-exist line above does not work. Is there any other way to search?

回答1:

Don't do the logic in the FTP script.

Call the ftp.txt script from a batch file. Within your ftp.txt script, just do a GET on your file. If the file is there, it'll be downloaded to the local directory. Otherwise, it won't. After calling the FTP script, check the file's existence in your local directory using standard DOS batch commands, i.e.:

@echo off

:FETCHFILE
ftp -s:ftp.txt
IF EXIST filetocheckfor.txt (
   REM the file was there, so do something
) ELSE
   echo Trying again...
   REM ping "sleep" would go here
   GOTO FETCHFILE
)

If you want to build a delay into your retries, perform a "sleep" by pinging a bogus IP address, as described in this post: http://www.dullsharpness.com/2010/06/14/elapsed-timer-using-pure-ms-dos/