如何获取当前行号?

发布于 2024-10-10 16:59:51 字数 447 浏览 2 评论 0原文

我正在尝试构建一个通用批处理文件,该文件可以通过行号来告诉错误发生的位置。
但是在代码中写每个行号有点烦人。

批处理文件运行时是否可以获取当前行号?
那么下面的代码可以工作吗?

@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%
)

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%
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

甜是你 2024-10-17 16:59:51

总有办法...
我发现不是完美的解决方案,但我可以使用一个很好的解决方法。

我调用一个函数,该函数使用 findStr 搜索自己的批处理文件(%~f0),以获取函数参数 ,因此仅当这些 对于整个批次而言确实是唯一的。
行号是从findstr /N的结果中获取的。

在此示例中:
6: call :getLineNumber errLine uniqueID4711 -2

第三个参数-2用于向行号添加偏移量,因此结果将为4

@echo off
SETLOCAL EnableDelayedExpansion

dir ... > nul 2> nul
if %errorlevel% NEQ 0 (
    call :getLineNumber errLine uniqueID4711    -2
    echo ERROR: in line !errLine!
)

set /a n=0xGH 2> nul
if %errorlevel% NEQ 0 (
    call :getLineNumber errLine uniqueID4712    -2
    echo ERROR: in line !errLine!
)
goto :eof

:::::::::::::::::::::::::::::::::::::::::::::
:GetLineNumber <resultVar> <uniqueID> [LineOffset]
:: Detects the line number of the caller, the uniqueID have to be unique in the batch file
:: The lineno is return in the variable <resultVar> add with the [LineOffset]
SETLOCAL
for /F " usebackq tokens=1 delims=:" %%L IN (`findstr /N "%~2" "%~f0"`) DO set /a lineNr=%~3 + %%L
( 
  ENDLOCAL
  set "%~1=%LineNr%"
  goto :eof
)

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 be 4.

@echo off
SETLOCAL EnableDelayedExpansion

dir ... > nul 2> nul
if %errorlevel% NEQ 0 (
    call :getLineNumber errLine uniqueID4711    -2
    echo ERROR: in line !errLine!
)

set /a n=0xGH 2> nul
if %errorlevel% NEQ 0 (
    call :getLineNumber errLine uniqueID4712    -2
    echo ERROR: in line !errLine!
)
goto :eof

:::::::::::::::::::::::::::::::::::::::::::::
:GetLineNumber <resultVar> <uniqueID> [LineOffset]
:: Detects the line number of the caller, the uniqueID have to be unique in the batch file
:: The lineno is return in the variable <resultVar> add with the [LineOffset]
SETLOCAL
for /F " usebackq tokens=1 delims=:" %%L IN (`findstr /N "%~2" "%~f0"`) DO set /a lineNr=%~3 + %%L
( 
  ENDLOCAL
  set "%~1=%LineNr%"
  goto :eof
)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文