Page 1 of 1

Current line number being executed

Posted: 08 Nov 2008 12:54
by jeff00seattle
In PHP and C, there is predefined constate __LINE__ which tell whats line number is currently being executed.

Is there an equivalent in DOS commands that can be used within a BATCH command file?

Thanks

Jeff in Seattle

Posted: 05 Dec 2008 04:59
by jeb
Hi Jeff,

I suppose there is no simple sulotion for __LINE__, but I use a pseudo line
function for error messages like "Error in line 123: Invalid parameter".

Code: Select all

@echo off
setlocal enableextensions
rem setlocal enabledelayedexpansion

if "%~1"=="" (
  call :echoLine "0815" "Parameter 1 missing" -1
)

sort "%~1" 2> NUL
if ERRORLEVEL 1 (
  call :echoLine "4711" "sort not succesful" -2
)
goto :eof


:::::::::::
:: Params
:: ID - must be a unique string
:: ErrorText
:: LineNumber Offset, will be added to the linenr of the calling line
:echoLine
setlocal
rem echo "%0", "%~0" , "%~f0"
for /F "usebackq tokens=1 delims=:" %%l in (`findstr /n "%~1" "%~f0"`) do set /a lineNr=%%l
if NOT "%~3" == "" set /a lineNr=lineNr+%~3
echo Error in %~nx0 line %lineNr%: %~2
endlocal


hope it helps

jeb

Posted: 05 Dec 2008 22:52
by DosItHelp
Great jeb!

The :echoLine function has been added added it to the function library (some minor modifications).
http://www.dostips.com/DtCodeCmdLib.php#Functions_echoLine

Code: Select all

:echoLine uniqueStr formatter offset -- outputs a formatted string, substitutes file name and line number
::      -- uniqueStr [in]     - a unique string to identify the line
::      -- formatter [in,opt] - a string using __FILE__ and/or __LINE__ to be substituted and echoed
::      -- offset    [in,opt] - offset to be added to the line number
:$reference http://www.dostips.com/forum/viewtopic.php?t=369
:$created 20080512 :$changed 20080512
:$source http://www.dostips.com
Setlocal Disabledelayedexpansion
Set "Fmt=%~2"
If Not Defined Fmt Set "Fmt=__FILE__(__LINE__): ERROR"
For /F "Delims=:" %%A In ('"Findstr /N "%~1" "%~f0""') Do Set /A "lineNr=%%A+%~30/10"
Call Set "Fmt=%%Fmt:__LINE__=%lineNr%%%"
Call Echo.%%Fmt:__FILE__=%~nx0%%
EXIT /b

:)