1

I would like to use a batch script to automate some work that I will give customers. I want to give them an INI file WITH comments (so they understand what they are setting).

Example INI:

[General]
;Set your operating system
OS=Windows7

Read ini from windows batch file Is a good start but does not help with comments.

Thanks in advance.

Community
  • 1
  • 1
Zack M
  • 11
  • 3
  • So what you want is the code listed in the answer in the link you provided to ignore the comments? – itdoesntwork Dec 26 '12 at 16:14
  • Check out this example at Rob van der Woude: http://www.robvanderwoude.com/sourcecode.php?src=readini_nt – David Ruhmann Dec 26 '12 at 16:18
  • @ itdoesntwork I thought it would be a good base, but not what I ultimately want. Since I want the variable set to its value so I can use it. – Zack M Dec 26 '12 at 17:00
  • @ZackM - I just added an [ini utility script](http://stackoverflow.com/a/15413717/1683264) to the page you linked. It will let you read and modify ini values, and is tolerant of comments. – rojo Mar 14 '13 at 15:54
  • [Reading an ini config file from a batch file « Almanac Hackers](http://almanachackers.com/blog/2009/12/31/reading-an-ini-config-file-from-a-batch-file/) – Wolf Mar 10 '14 at 13:36

1 Answers1

0

The FOR statement has the EOL option that specifies a character that indicates the line is to be ignored if it appears in the 1st position on a line. The default EOL character is ;, so you don't have to do anything special to ignore lines that begin with ;.

Your requirements are not very clear. I'm not sure what (if anything) you want to do with the section labels like [General]. I'm going to ignore them.

You can use FIND or FINDSTR to filter out any line that does not contain =.

Here is a simple script that will load variables as defined by the .ini file. Lines that begin with ; are comments. Section headers are ignored.

@echo off
for /f "delims=" %%A in ('findstr = example.ini') do %%A
dbenham
  • 127,446
  • 28
  • 251
  • 390
  • Section labels would be used for organization and would not be needed. However, that does make another tag type other than comment. – Zack M Dec 26 '12 at 19:06
  • Might break if the comment includes a `=`, not sure though – itdoesntwork Dec 27 '12 at 00:49
  • @itdoesntwork - no, the FOR statement will ignore all comment lines (lines beginning with `;`), regardless of content. But it could falsely load section labels that contain `=`. For exampe, `[this=is loaded]`. The FINDSTR could be tailored to prevent this. – dbenham Dec 27 '12 at 01:04