How to capture ftp error code in batch scripts?

I have a few related but different questions here .

I have a batch script ( *.bat) file , for example:

@ftp -i -s:"%~f0"&GOTO:EOF
open ftp.myhost.com
myuser
mypassword
!:--- FTP commands below here ---
lcd "C:\myfolder"
cd  /testdir
binary
put "myfile.zip"
disconnect
bye

Basically this is a script that uploads a zip file to an ftp site. My question is that the download operation may fail from time to time (remote ftp is not available, "myfile.zip" does not exist, the download operation is aborted and much more), and when such unsuccessful things happen, I want my bat file return 1 ( exit 1).

It would be great if my download were not successful, ftp would throw an exception (yes, like an exception in C ++), and I would have a catch-all exception that catches it, and then exit 1, but I don’t think that it is available in the script package.

What is the best way to do what I need here?

+5
source share
2 answers

You can redirect the output to a log file, and after the ftp session ends, the file can be analyzed.

@ftp -i -s:"%~f0" > log.txt & GOTO :parse
open ftp.myhost.com
myuser
mypassword
!:--- FTP commands below here ---
lcd "C:\myfolder"
cd  /testdir
binary
put "myfile.zip"
disconnect
bye

:parse
for /F "delims=" %%L in (log.txt) Do (
  ... parse each line
)
+2
source

The only option in batch files that I know of is to use the "IF ERRORLEVEL" syntax, which requires your ftp client to return a non-zero error code.

http://www.robvanderwoude.com/errorlevel.php .

, , ftp Windows , , . , , , , FIND .

0

All Articles