如何获得当前行号? [英] How to get the current line number?

查看:128
本文介绍了如何获得当前行号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图建立一个通用的批处理文件,可以用行号,那里的错误occours告诉错误。结果
但是写在code分别行号是有点讨厌。

是否有可能获得当前行号,一个批处理文件运行时?结果
所以,下面的code能工作吗?

 关闭@echo
致电:DoSomething的1如果%ERRORLEVEL%GTR 0(
    REM做这样神奇的东西,检索lineNo的
    拨打:getCurrentLineNo currentLineNo
    近%currentLineNo%回声错误
)拨打电话:DoSomething的2如果%ERRORLEVEL%GTR 0(
    拨打:getCurrentLineNo currentLineNo
    近%currentLineNo%回声错误


解决方案

有总是这样......结果
我发现没有完美的解决方案,但一个好的解决办法,我可以使用。

我把它搜索自己的批处理文件中的函数(%〜F0 )与FINDSTR时,函数参数< UNIQUEID> ,所以这只有当这些< UNIQUEID方式> 的是整批真正独特的结果
该行号是从 FINDSTR / N 的结果得到的。

在此示例:搜索
6:拨打:getLineNumber errLine uniqueID4711 -2

第三个参数 -2 是用来添加一个偏移到行号,那么结果将是 4

 关闭@echo
SETLOCAL EnableDelayedExpansion目录...> NUL 2 - ; NUL
如果%ERRORLEVEL%NEQ 0(
    拨打:getLineNumber errLine uniqueID4711 -2
    回声错误:行errLine!
)集/ A N = 0xGH 2 - ; NUL
如果%ERRORLEVEL%NEQ 0(
    拨打:getLineNumber errLine uniqueID4712 -2
    回声错误:行errLine!

GOTO:EOF:::::::::::::::::::::::::::::::::::::::::::::
:GetLineNumber< resultVar> <&UNIQUEID GT; [LineOffset]
::检测呼叫者的行数,唯一ID必须在批处理文件独特
::该LINENO在变量&LT回报; resultVar>添加具有[LineOffset]
SETLOCAL
FOR / F令牌有usebackq = 1 delims =%% L IN(`FINDSTR / N%〜2%〜F0`)DO设置/ A lineNr =%〜3 + %%大号

  ENDLOCAL
  集%〜1 =%LineNr%
  GOTO:EOF

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

这篇关于如何获得当前行号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆