Search code examples
windowsbatch-filesyntax-errorerror-logging

find where or which line error is occur while running in Batch Script?


I want to know error line number where error was occure by batch script and also i want to print whole line in batch script?

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 [email protected] || 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%

can any one help me out?


Solution

  • jeb has described how the search for the right line works. in the event of an error, a call is triggered and the parameters are a unique string and an offset, which points to the number of missing lines to the correct line. Findstr looks for the unmistakable string and prints that line number. With the settlement that's right again.

    I've put together some macros to show that there are other ways to read the right line.

    @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
    

    hope it helps

    Phil