Current line number being executed

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
jeff00seattle
Posts: 7
Joined: 06 Nov 2008 19:55

Current line number being executed

#1 Post by jeff00seattle » 08 Nov 2008 12:54

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

jeb
Expert
Posts: 1055
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

#2 Post by jeb » 05 Dec 2008 04:59

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

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#3 Post by DosItHelp » 05 Dec 2008 22:52

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

:)

Post Reply