reading a line next to a keword

Eidos Just cause 2 (xbox 360)
June 25, 2010 at 00:18:36
Specs: Windows XP, 1 bg
I have a ini file in the following format
[Server ]
[user ]

I need a batch file to read this file and return the corresponding value. For example if i ask for server it must return Please help. Thanks in advance

See More: reading a line next to a keword

Report •

June 25, 2010 at 04:06:14
Looks like there's only one entry per section.
Try the following (you'll need to edit 'set IniName=' to equal
your .ini filename):

[Code edit: one 'for' too many...]

@echo off

setlocal EnableDelayedExpansion

set IniName=somefile.ini
set IniValue=
set InSection=false
set /p SectName=Please enter section name: 
for /f "usebackq tokens=1,2 delims==" %%a in ("%IniName%") do (
  if "!InSection!" equ "true" (
    set IniValue=%%~b
    goto ExitFor
  echo %%a | findstr /i /r /c:"^\[%SectName%[ ]*\]"> nul
  if !errorlevel! neq 1 set InSection=true
if defined IniValue (
  echo %IniValue%
) else (
  echo Value not found.

Report •

June 25, 2010 at 09:33:36
Thanks a lot.. :-). I tried to set the ini path as follows.
set IniName="D:\reading_from_medconfig\Medconfig.ini"
Am I right.
Am getting error as The system cannot find the file "D:\reading_from_medconfig\Medconfig.ini".

Please help. Thanks in advance

Report •

June 25, 2010 at 10:00:11
Its working. Thanks a lot dude :-) :-)

Report •

Related Solutions

June 25, 2010 at 11:01:26
I think this s not working if we have space in the section name. Am I right?.
I am checking for the following content
[Production Server ]
Am getting error. Please help.

Report •

June 25, 2010 at 12:08:48
It works fine here with or without spaces - or are you
saying that you want some kind of wildcard match
where you enter 'server' and it picks up on anything with
server in? If that's the case then if you have more than
one section name with server in, how do you know that
you're going to get the right one, or do you want to list
them all? What is the error message you're getting?

Report •

June 25, 2010 at 12:58:58
findstr sucks whenever spaces are involved, in my experience.
I've had hours of frustration trying to use reg.exp combining spaces and never could get it to work right. (the escape char. doesn't work on the space apparently, or am i missing something?) neither does [ ] or [\ ]. That's prob'ly the source of the failure in this case. for example:
echo "hello world" | findstr /r /i "o\ world" : positive
echo "hello world" | findstr /r /i "x\ world" : false pos.
echo "hello world" | findstr /r /i "x[\ ]world" : negative
echo "hello world" | findstr /r /i "o[\ ]world" : false neg.
echo "hello world" | findstr /r /i "o[ ]world" : false neg.

Report •

June 25, 2010 at 16:51:42
nbrane: you need to be using /c:"regex" whenever
the search expression includes a space. Take your
second example, for instance, which you say is a
false positive:
echo "hello world" | findstr /r /i "x\ world"

It's actually being interpreted as find "x\" or "world"
which should be positive because "world" is in the
string that is being searched. If you change that to:

echo "hello world" | findstr /r /i /c:"x\ world"

the result is negative...

Report •

June 25, 2010 at 17:22:14
hey, thanks man! That's not documented very well in findstr /?. I understood about the space and i understood about /c: to get around it, but i thought /c meant like /L and would therefore not work in conjunction with /r.
I completely missed the correct setup in your first post.
(I knew there was a reason i lurked in this forum a lot! :-)
... snip-out
oops, nah ignore - snipped

Report •

June 25, 2010 at 20:20:05
I dont want wild card like things. The problem is if i have a section name with space i am getting error like cannot findstr [ and so on with several other symbols. However I fixed it by removing the \ from this line

echo %%a | findstr /i /r /c:"^\["Production Server"[ ]*\]"> nul


echo %%a | findstr /i /r /c:"^["Production Server"[ ]*\]"> nul

Now it is working fine :-) But I am not sure how it worked. Tried googling. no use. Please explain if possible. Ur explaination on findstr was very useful. :-)

Thanks a lot. I thought as it was the end of road after trying for a long time. ur reply saved me :-):-)

Report •

June 25, 2010 at 21:00:59
You might be in for problems down the road if you disable the
"escapes" on the [.
It looks like you got some extra quotes that are hashing things. the bad line (note the excessive profusion of dbl quotes):
echo %%a | findstr /i /r /c:"^\["Production Server"[ ]*\]"> nul
UNLESS you've put quotes around your input at the set /p prompt, which would really bolix things.
then you removed the escapes here:
echo %%a | findstr /i /r /c:"^["Production Server"[ ]*\]"> nul
This will give either false positive or completely random and
unpredictable results because [ and ] are "special" to the
reg.exp. evaluation. (refer to: findstr /?, bottom section on reg.exp overview).
but mark's original was correct (no extra quotes, escapes intact):
echo %%a | findstr /i /r /c:"^\[%SectName%[ ]*\]"> nul
the regexp he gives says:
beginning of line only: [, then your input (including internal spaces), then zero or more spaces, then ]
Yours gives: beg.of.line only:any character of the subset
" p r o d u c t i o n ... then i don't know what...
Make SURE not to put dbl-quote when you enter at the prompt of set /p.

Report •

June 26, 2010 at 02:19:41
nbrane wrote:
Make SURE not to put dbl-quote when you enter at the prompt of set /p.

Well spotted! It never occurred to me that figjam420 might
be quoting at the prompt...

figjam420: Use my original code (fresh copy and paste)
and as pointed out don't use quotes.

Report •

Ask Question