reading specific value from file

Lenovo Thinkpad t61 notebook
August 6, 2009 at 01:22:15
Specs: Windows XP, Core 2 duo/4GB
Hi
I want to read a specified value from a a text file
My text file contains

os=windows
machine=laptop
site=computing
file=bat

Now In my bat file I need to get value for specific key i,e If I give machine it should give laptop and I need to set that to a variable..
Please do help me to derive a command to do this operation..


See More: reading specific value from file

Report •


#1
August 6, 2009 at 02:17:15
@echo off & setLocal EnableDELAYedExpansion

set /p key=key ? :

for /f "tokens=1-2 delims==" %%a in (myfile) do (
if %%a equ !key! (
set str=%%b
)
)
echo !str!


=====================================
If at first you don't succeed, you're about average.

M2


Report •

#2
August 6, 2009 at 02:39:13
Here I have two files to read from.. ie myfile1 and my myfile2

How do I execute for within for ??


Report •

#3
August 6, 2009 at 03:33:19
@echo off & setLocal EnableDELAYedExpansion

set /p key=key ? :

for /f "tokens=* delims= " %%f in ('dir/b/a-d myfile*') do (

for /f "tokens=1-2 delims==" %%a in (%%f) do (
if %%a equ !key! (
set str=%%b
)
)
echo !str!

)


=====================================
If at first you don't succeed, you're about average.

M2


Report •

Related Solutions

#4
August 6, 2009 at 04:08:59
What is set /p key=key ? : in your script ??
I didnt get that..

And my "myfile" is in subdirectories of current directory.. Can I give full path in for loop ??
For ex can I give
for /f "tokens=1-2 delims==" %%m in (%CURRENT_DIR%\dir1\dir2\myfile.txt) do
(

)

Will it work ??

And u gave set str=%%b , But You haven declared 'b'.. Then what will be the value of ''b" ?


Report •

#5
August 6, 2009 at 04:37:11
Hey I got that output.. It works fine..
Thanks

Report •


Ask Question