I'm trying to build a generic batch file that can tell errors with a line number, where the error occours.
But writing each line number in the code is a little bit annoying.
Is it possible to get the current line number, while a batch-file is running?
So that the following code can work?
@echo off
call :doSomething 1
if %errorlevel% GTR 0 (
REM Do something magic, to retrieve the lineNo
call :getCurrentLineNo currentLineNo
echo Error near %currentLineNo%
)
call :doSomething 2
if %errorlevel% GTR 0 (
call :getCurrentLineNo currentLineNo
echo Error near %currentLineNo%
)
There is always way...
I found not the perfect solution, but a good workaround I can use.
I call a function which searches the own batch file(
%~f0
) with findStr, for the function parameter<uniqueID>
, so this works only if these<uniqueID>
's are really unique for the whole batch.The linenumber is get from the result of
findstr /N
.In this sample:
6: call :getLineNumber errLine uniqueID4711 -2
The third parameter
-2
is used to add an offset to the linenumber, so the result will be4
.