Batchography: Parsing INI files from a Batch file

Often times you might want to write Batch file scripts to automate system administration tasks, and in addition to that you might want to pass configuration files to your Batch scripts.

This article, inspired by the Batchography book, shows you how to parse INI files and retrieve values from a given section and key.

Quick background

An INI file (or initialization file) is a text file that has the following format:

; comment

[section_name1]
Key1Name=Value1
.
.
.
[section_name2]
Key1Name=Value1
Key2Name=Value2
.
.
.

In the MS Windows operating system, a C/C++ programmer can read/write values from the INI files using the following APIs:

But can we do the same using Batch files?

Yes and in the next section, we show you how to read values from the INI file.
flower separator
batchography-good-resDo you want to master Batch Files programming? Look no further, the Batchography is the best book on the topic and the most up to date!

Available in print or e-book editions from Amazon.

 

The INI parser Batch script

The following script, found in the Batchography’s book source code repository, illustrates how to parse INI files.

Here’s the general algorithm:

  1. Read one line at a time
  2. Check if a line starts with “[“, if that’s the case then it is a section name
    1. Furthermore, check if this is the section we are interested in
    2. If it was, then define a state variable designating that now our parser is inside a section
  3. When we are in the “insection” state, any subsequent read line is a key/value pair
    1. Tokenize the key/value line with the “=” token
    2. Compare the key with the desired key value
    3. If we found the correct key, then return the value

This is the script with its main function that reads from “settings.ini” and retrieves the values “height” and “lastsaved” from the “display” and “settings” keys respectively:

@echo off

:: 
:: The Batchography book by Elias Bachaalany
::

:main
  setlocal enabledelayedexpansion
  call :get-ini settings.ini display height result
  echo r=%result%

  call :get-ini settings.ini settings lastsaved result
  echo r=%result%

  goto :eof

:get-ini <filename> <section> <key> <result>
  set %~4=
  setlocal
  set insection=
  for /f "usebackq eol=; tokens=*" %%a in ("%~1") do (
    set line=%%a
    if defined insection (
      for /f "tokens=1,* delims==" %%b in ("!line!") do (
        if /i "%%b"=="%3" (
          endlocal
          set %~4=%%c
          goto :eof
        )
      )
    )
    if "!line:~0,1!"=="[" (
      for /f "delims=[]" %%b in ("!line!") do (
        if /i "%%b"=="%2" (
          set insection=1
        ) else (
          endlocal
          if defined insection goto :eof
        )
      )
    )
  )
  endlocal

This script is found in and fully explained in the Batchography book. You can also download the script from here.You might also like:

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.