1

I am using windows batch scripts to run ftp scripts automatically. Where the set of ftp commands I want run is saved in a file.

example:

@echo off 
ftp -n -i -s:c:\temp\myftpscriptfile.ftp

I have used the %ERRORLEVEL% syntax to successfully capture error conditions in the batch commands. My challenge is the ftp script command is always returning an ERRORLEVEL of 0 even when the commands inside the script fail.

I am having difficulty figuring out how to have the ftp script actually return or trap when errors occur inside it. It will simply run through the commands blindly and even though i can see the errors echoed on screen I can't capture them as an ERRORLEVEL..

Sample screen shot of trying script which fails to login and then echoing the ERRORLEVEL which shows a zero..

ftp> open fubar.test.com
Unknown host fubar.test.com
ftp> user test test
Not connected.
ftp> ascii
Not connected.
ftp> cd /home/test/dirname
Not connected.
ftp> mput C:\Test\test*.txt
Not connected.
ftp> close
Not connected.
ftp> quit
.0

2 Answers2

1

Use find:

ftp -n -i -s:c:\temp\myftpscriptfile.ftp 2>&1|find "Unknown host">nul
if %errorlevel%==0 (
  echo Error!
)
wOxxOm
  • 65,848
  • 11
  • 132
  • 136
1

Parse ftp communication using for /F command. Possible approach in next script. Caveat: there supposedly exist some ftp error message(s) not included in given test sequence. Of course, you can test positive ftp messages rather...

@ECHO OFF >NUL
SETLOCAL enableextensions enabledelayedexpansion
set "errsftp=0"
ftp -n -i -s:c:\temp\myftpscriptfile.ftp >c:\temp\31442020.err 2>&1
for /F "tokens=1*" %%G in (c:\temp\31442020.err) do (
  rem echo %%G [%%H]
  if "%%G"=="ftp>" (
    set "line=%%H"
    set "errs=0"
  ) else (
    set "reply=%%G %%H"
    Call :testreply Unknown host
    Call :testreply Connection timed out
    Call :testreply Not connected
    rem insert next tests here
    if !errs! EQU 0 echo !line! =looks OK= !reply!
  )
)
echo(
echo :errors total: %errsftp%

ENDLOCAL
goto :eof

:testreply
    set "ylper=!reply:%*=!"
    if not "!ylper!"=="!reply!" (
      echo !line! =ERROR= !reply!
      set /A "errs+=1"
      set /A "errsftp+=1"
    )
goto :eof
JosefZ
  • 28,460
  • 5
  • 44
  • 83