退出从for循环调用的批处理文件中的问题

时间:2016-04-27 09:33:14

标签: batch-file

我遇到一个问题,即子脚本的退出状态会回到父脚本(子环由for循环调用)。

父脚本的语法:

for /f "usebackq" %%i in (`dir/b /o:d %partionHome%\tmp\queue\*.t~#`) do call "%partionHome%\conf\SQLLoader\SQL_Loader_%DSNname%
\SQLLoader_UA_SYSTEM_TABLES.bat" %1 %2 %3 %4 %5 %6 %7 "%%i" %has_prefix% "%partionHome%"
echo ERRORLEVEL_Master %ERRORLEVEL%                                                 >> %logfile%
if %ERRORLEVEL% NEQ 0 goto :failed
goto :end
:end
echo Executed loop >> %logfile%
echo got out >> %logfile%
echo Success >> %logfile%
exit 0
:failed
echo Load Failed >> %logfile%
exit 1

儿童书:

"%sqlbinn%\sqlcmd.exe" -S %dbHost% -d %databaseName% -U %userName% -P %pwd% -Q "BULK INSERT %databaseName%.dbo.%tableName% FROM '%bulkloader%\%DATA_FILE%' WITH (FIELDTERMINATOR = '|', ROWTERMINATOR = '\n', 
FORMATFILE='%bulkloader%\%CTRL_FILE%')" >> %logfile% 2>&1
echo ERRORLEVEL %ERRORLEVEL%                                                >> %logfile%
if %ERRORLEVEL% NEQ 0 goto :errors
goto :doneld
:doneld
echo i am here3 >> %logfile%
echo * Delete the control and data file from the shared directory           >> %logfile%
del %confdirectory%\%DATA_FILE%
del %confdirectory%\%CTRL_FILE%
set H=%time:~-10,1%
if %time:~-11,2% LSS 10 (set Hour=%time:~-10,1%) else (setHour=%time:~-11,2%)
set M=%time:~-8,2%
set s=%time:~-5,2%
echo **   SQLLoad ENDS  %H%:%M%:%s%                                         >> %logfile%
echo i am here4 >> %logfile%
EXIT /B 0
:errors
echo i am here5 >> %logfile%
echo *                                                                      >> %logfile%
echo WARNING! Error(s) were detected in the SQL loader utility!             >> %logfile%
echo Please check the data file, control file or the SQL loader utility     >> %logfile%
echo *                                                                      >> %logfile%
echo sqlloaderERROR check log file %logfile% >> %logDirectory%\sqlloaderlogs\LOAD_ERROR_DETECTED_%Day%-%Mth%-%Yr%_%Hour%_%Min%_%sec%_%msec%
time /t >> %logfile%
EXIT /B 1

SQL命令工作,数据上传,日志记录它完成应有的一切并输出" echo我在这里4",但由于某种原因,该过程重复约22次(运行sqlcmd.exe)然后正确退出(echo执行循环,echo master等来自主脚本)?????

我认为EXIT / B *应该向主脚本返回一个错误代码,以便对其进行评估(我有),但它没有,只是循环并试图一遍又一遍地运行sqlcmd.exe最终成功并关闭?

1 个答案:

答案 0 :(得分:0)

exit /b 1停止您的子脚本并将errorlevel设置为1.

为什么要阻止别的什么呢? 它既不会停止你的父脚本也不会停止循环 exit /b在其他语言中的作用有点像return

您可以在循环中测试errorlevel,然后停止循环(使用goto :label)。

for /f %%i in (`dir/b queue\*.t`) do (
  call child
  if errorlevel 1 goto :break
)
:break