Read Batch File Script Info

Recently I was updating a rather complex DOS batch file that we track the changes on. Each time we update it, we update a comment in the batch file for the version number. Since we sometimes get logs sent to us about the script’s operation I figured it would be useful to add the version number to the log file.

Whilst I could have set the script to use a variable with the version number in it I decided to create an option to work with the existing comment based format.

The approach I adopted was to have the batch file read through itself to a particular line, then read the line into a variable and take the text from the end of the line - the version number.

The example script below uses the routine ReadSelf_LineNo to read through the file line by line to the one specified by the parameter passed into it. In this case we’re passing in ‘3’, so it will find the third line. It then sets the variable SCRIPT_VERSION_LINE to the content of that variable.

With the variable set, and the routine complete the script outputs the content of the line. Since we know what the starting character position of the version number is we can use that to set the variable SCRIPT_VERSION to the version number by capturing a number of characters (I arbitrarily chose 5 to allow for 4 digits and a period) from that point on in the text. The script outputs the version number and completes.

So the output when run looks like this:

@ECHO OFF
REM Can I read information from the next line?
REM VERSION: 2.13

SET LINE_NO_TO_READ=3
CALL :ReadSelf_LineNo %LINE_NO_TO_READ%
ECHO Line number %LINE_NO_TO_READ% reads as follows "%SCRIPT_VERSION_LINE%"
ECHO.

SET SCRIPT_VERSION=%SCRIPT_VERSION_LINE:~13,5%
ECHO Script Version=%SCRIPT_VERSION%
ECHO.
PAUSE
GOTO :EOF


:ReadSelf_LineNo
SET VER_LINE_NO=%1
SET /a VER_LINE_NO-=1
FOR /f "usebackq delims=" %%a in (`MORE +%VER_LINE_NO% %~dpnx0`) DO (
  SET SCRIPT_VERSION_LINE=%%a
  GOTO :EOF
)
GOTO :EOF

So that’s all there is to it. Of course you could use this to retrieve information from anywhere in the file as long as you know the line number and structure of the line of course. You could use it to pick out information such as dates, authors, copyright, etc.

Of course this does seem like a lot of effort for what could be handled by a simple variable change in the script - after all it gets assigned to a variable in the end anyway. However my reasoning was simply that I was used to adding this sort of meta information into comments in the top of the file and the structure of the particular script that prompted this post is such that I’ve moved all variables that can’t be automatically “deduced” by the script to an optional external override file … so setting the version number as a variable simply conflicted with that and I figured it should be straight forward enough to use the existing format. At the end of the day the original script reads nicely with just comments at the start and the version number gets set automatically during the run.

Author: Stephen Millard
Tags: | shell |

Buy me a coffeeBuy me a coffee



Related posts that you may also like to read