taking a value from a text file

February 15, 2010 at 17:14:03
Specs: Windows XP
I am a total newbie to writing batch files,but I thought what i was seeking would be easy to find. I have hunted for hours looking for a simple example showing how to take a value from a text file and write it to another text file.
For example, imagine I have a text file called myTextFile.txt located in c:\myfolder.

myTextFile.txt contains simple values on each line eg

name=fred
age=32
dateOfBirth=july1970

I need to take just a single value say the "age" value and write it to another text file.

so the only content of the new text file would be:
32

can you help, i just cannot seem to find an example


See More: taking a value from a text file

Report •


#1
February 15, 2010 at 19:48:52
it should not be hard, but any coder would need to know the specifics of the value you wish to extract: is it context-based (a "label", such as age) or is it positionally determined.
Is it always line 2, always starting at byte #5, or is it any line that starts out with "age". Once these details are nailed down, it should be fairly easy for a simple file.
here's a snippet to start out with (all on one line, the forum wrapped it):

for /f "tokens=2 delims==" %%a in ('find "age="^<myfile.txt') do >>newfile echo=%%a

post a little bit of info about how the file is arranged to obtain a working script.


Report •

#2
February 16, 2010 at 07:26:52
Many thanks, you hit the nail on the head. It was a label I needed to identify.
the script
for /f "tokens=2 delims==" %%a in ('find "age="^<myfile.txt') do >>newfile.txt echo=%%a

worked a treat, many thanks.
I thought I was home and dry.
However it has now shown me a new problem.
the information I wish to extract from the first text file is a date.
eg :
fileDate=9-28-2010

the date format here is mm-dd-yyyy
I wish to pass this date on to the new text file in the format dd-mm-yyyy

I do'nt want to be a nuisance but could I also ask if you could explain exactly how the "token=" line works, I'm confused by the explanations if have read


Report •

#3
February 16, 2010 at 13:39:54
no problem, it's coffee time anyway! :)
" do'nt want to be a nuisance but could I also ask if you could explain exactly how the "token=" line works, I'm confused by the explanations if have read"
tokens are "words" taken from a "sentence"
words can be "delimited" by a lot of things:
(spaces) . ! ? ,
(f/e: whatever! i'm done here, i'm going home. bye.)
(sounds like a girlfriend problem!)
"tokens" specifies which "word" or words you want,
"delims" specifies what char or char.s separate the words,
f/e: "tokens=2 delims=," says take the second word that
is counted using a comma.
"tokens=1,2,3 delims=,!." says take words 1, 2 and three
as counted using either , ! or .
applied to the example above you would get
"whatever" as token #1, (delimiter of !)
"i'm done here" as token #2 (delimiter of comma)
"i'm going home" as token #3 (delimiter of period)
the tokens are assigned to the variables on one-to-one basis,
so the first var. gets the first requested word, the second
gets the second requested word, f/e:
"tokens=1,3 delims=,!." %%f in (girlfriend.txt)
puts the first "word" in %%f, and the third word into %%g
(implied: var.s ascend by one value for each requested word).
here is example using your date setup above:

for /f "tokens=2,3,4 delims=-=" %%a in ('find "fileDate="^<myfile.txt') do (
set month=%%a
set day=%%b
set year=%%c
)
set newdate=%day%-%month%-%year%
>> newfile echo %newdate%

token #1 is not requested so it is skipped ("fileDate")
tokens 2,3,4 (also expressible as: 2-4) are assigned
to a, b and c respectively, then rearranged by the script.

Hth


Report •

Related Solutions

#4
February 17, 2010 at 02:48:52
Wow, what can I say.
Thank you so much, that was a great explaination of tokens and delims. Your examples made it really clear. After all the time I have spent looking on the net I have not been able to find such a clear, concise tutorial. Many many thanks

Report •

Ask Question