首页 > 解决方案 > 查找在批处理脚本中运行时发生的位置或行错误?

问题描述

我想知道批处理脚本发生错误的错误行号,并且我想在批处理脚本中打印整行?

set t=%date%_%time%
set x=FORD_DLCM_T6_FTC
set a="%m%\%x%"
cd  /d "C:\PRQA\PRQA-Framework-2.1.2\common\bin"
qacli admin --set-license-server 5055@00.00.0.0 || goto :error
qacli admin --debug-level DEBUG || goto :error
goto :EOF
:error
set remark= %ERRORLEVEL%
set status= Failure
echo ProjectName: %x%
echo Status: %status%
echo Remark: %remark%
echo Date: %t%
echo %x%,%status%,traceback(),%t% > "C:\Test\Build.csv"
echo Failed with error #%errorlevel%.
exit /b %errorlevel%

谁能帮我吗?

标签: windowsbatch-filesyntax-errorerror-logging

解决方案


jeb 已经描述了搜索正确行的工作原理。如果发生错误,将触发调用,参数是一个唯一的字符串和一个偏移量,它指向正确行的缺失行数。Findstr 查找明确无误的字符串并打印该行号。有了解决方案,这又是正确的。

我整理了一些宏来表明还有其他方法可以读取正确的行。

@echo off
setlocal
set prompt=$g$s
call :setAllmacros
(call)
 rem begin in echo off - what line is it please ?
%ID-1:##==rem?% 
(call )
%ID-1:##==call%
set/a+
%ID-1:##==001% call :ErrorLogging Line:%%L Errorlevel:%errorlevel% 
echo 2

(call) || %ID-0:##==2% call :Errorlogging Line:%%L Errorlevel:%%errorlevel%% 
echo %errorlevel%
find /n  || %ID-0:##==003% call :Errorlogging Line:%%L Errorlevel:%%errorlevel%% 

%ID-0:##==008%
pushG 556
%ID-1:##==004% call :ErrorLogging Line:%%L Errorlevel:%errorlevel% 

%Line#print%:33=="

pause
exit /b

:errorlogging
@echo on
rem %*
@echo off
exit /b

:setAllmacros
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
(set ^"lf=^
%== define LF + CR ==%
)
for /f %%i in ('copy /z "%~f0" nul') do set "CR=%%i"

 rem Prints the fullline itself with LineNumber into stderr
 rem  usage: %Line#print%:UniqueID"
 rem   command || %Line#print%:01"
set "Line#print= @ <"%~f0" >&2 find /n "%%Line#print%%"

 rem Read the fullline BEFORE into Variables with LineNumber; do something ... 
 rem  usage: command 1 line before
 rem         %ID-1:##==01% [command %%L ... %%M ...]
set ID-1=  if errorlevel 1 for /f "usebackQtokens=1*delims=:" %%L in (^
 `cmd /v /c ^"findstr /n /r /c:"..*^!CR^!*^!LF^!.*ID-1:.#^=^##%%" "%~f0"^"`) do ^>^&2 echo BatchLine: %%L -- %%M ^& 

 rem Read the fullline itself into Variables with LineNumber; do something ... 
 rem  usage: command || %ID-0:##==01% [command %%L ... %%M ...]
set ID-0= @ for /f "tokens=1*delims=:" %%L in ('findstr /n /r /c:"ID-0:.#=##%%" "%~f0"') do ^>^&2 echo BatchLine: %%L -- %%M ^& 
exit /b

希望能帮助到你

菲尔


推荐阅读