如何在windows ftp脚本中捕获错误条件? [英] how to capture error conditions in windows ftp scripts?
问题描述
我使用windows批处理脚本自动运行ftp脚本。
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.
例子:
@echo off
ftp -n -i -s:c:\temp\myftpscriptfile.ftp
我已使用%ERRORLEVEL%语法成功捕获批处理命令中的错误条件。我的挑战是,即使脚本内的命令失败,ftp脚本命令始终返回0的ERRORLEVEL。
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.
我很难弄清楚如何使用ftp脚本实际上在内部发生错误时会返回或陷入陷阱。它会简单地通过命令盲目运行,即使我可以看到屏幕上回显的错误,我无法捕获它们作为ERRORLEVEL ..
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..
尝试屏幕截图的尝试脚本无法登录,然后回显显示零的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
推荐答案
解析 ftp
使用为/ F
命令进行通信。下一个脚本中的可能方法。警告:假设存在一些未包含在给定测试序列中的 ftp
错误消息。当然,您可以测试正面 ftp
消息,而不是...
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
这篇关于如何在windows ftp脚本中捕获错误条件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!