Created
December 4, 2012 21:07
-
-
Save davidruhmann/4208689 to your computer and use it in GitHub Desktop.
[Batch] Get the line number of a statement within a batch file. (Debugging)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:: Batch Code Line Debugging | |
:: Retrieve the batch file line number where the Line function is called. | |
:: by David Ruhmann | |
:: Hide Command and Isolate Scope | |
@echo off | |
setlocal | |
:: Usage Example | |
call :False xResult | |
if /i not "%xResult%"=="false" goto Done | |
rem Capture the line number of the failure by offsetting to the relative line. | |
call :Line xLine eUniqueLabel -3 | |
call :Log "" "ERROR: False failed. Line: %xLine%." 1 "%Random%" | |
:Done | |
pause | |
goto End | |
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | |
:False <xReturn> | |
:: A function to return false to emulate calling a function that has a failure. | |
setlocal | |
set "xResult=false" | |
endlocal & if not "%~1"=="" set "%~1=%xResult%" | |
goto :eof | |
:End | |
endlocal | |
:: by David Ruhmann |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: | |
:Line <xReturn> <xUniqueID> [xOffset] | |
:: Find and return the line number of the unique id adjusted by the optional offset. | |
:: Returns -1 in the case of an error or the unique id is not found in the file. | |
:: Note: The unique id cannot contain backslashes and MUST have spaces on each side. | |
:: Note: Change " %~2 " into " %~2" to only require a space before the unique id. | |
setlocal | |
set "xLine=-1" | |
for /f "usebackq tokens=1 delims=:" %%i in (`findstr /n /c:" %~2 " "%~f0"`) do set /a xLine=%~3 + %%i | |
endlocal & if not "%~1"=="" set "%~1=%xLine%" | |
goto :eof | |
:: by David Ruhmann |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment