查找在批处理脚本中运行时发生的位置或哪个行错误?

时间:2018-06-12 10:33:51

标签: windows batch-file syntax-error error-logging

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

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%

任何人都可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

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

希望有帮助

菲尔