Extract certain text from a text file

May 15, 2010 at 12:00:25
Specs: Windows PE/Vista
Hello batch file experts! I have a problem I hope you guys can help me with.

Running this command:
wmic csproduct list full > c:\output.txt

Creates a text file that contains this exact text (when run on my Thinkpad T500)

Description=Computer System Product
Version=ThinkPad T500

What I would like to do is put into variables the values of the "IdentifiyingNumber", "Name", "Vendor", and "Version" fields.
In other words, (using the above output.txt file's values), 4 variables. One would equal "ABC1345", a second would equal "205545U", a third would equal "LENOVO" and the fourth would equal "ThinkPad T500"

Not sure if it affects the code to be written, but when dumping that output to a text file, it puts two empty lines at the top, and two at the bottom inside the text file.

What I need this for is to be able to have a batch file that determines what hardware I am running on, which then based on what hardware it is, run the next appropriate commands.

The batch would would be running under Win PE 2.1, but it should be able to run under Windows XP as well.

Thanks guys!

See More: Extract certain text from a text file

May 15, 2010 at 14:05:12
::--- edit : fixed
for /f "tokens=*" %%a in (output.txt) do set %%a
echo identifyingNumber: %identifyingnumber%
echo Name: %name%
echo Vendor: %vendor%
echo Version: %version%

Report •

May 15, 2010 at 15:39:31
Thanks for the quick reply! I tried your code, but when I run it, all I get is an error stating:

"set was unexpected at this time"

I verified I am pointing to the correct file, but no matter what I try, I get the above error each time.

Report •

May 15, 2010 at 16:04:52
I noticed you did not put in the "do" command before the word "set". I was not sure if that was needed or not, so I put it in, and now the command does run, however the variables are not populated with any data, so the next lines in your code, where it echos out the variables, they all display empty.

And ideas? I'm stuck :)

And again, thanks for your help, I really appreciate it!

Report •

Related Solutions

May 15, 2010 at 17:05:21
ok, yeah i just noticed the absent "do". apologies for my
The other is more of a problem, as I ran my test using your
exact data (as cut/pasted), including a couple blanklines,
and got the desired results.
delayed exp not the culprit, the loop is finished, and var names are not case-sensitive.
it should not be an opsys thing, but that's the only thing i can think of (mine was tested on winXP sp2).

might just take a stab at this:
for /f "tokens=1* delims==" %%a in (output.txt) do set %%a=%%b

and see if you get anything.

Report •

May 15, 2010 at 18:30:15
I tried your new code, but same results. so far I have the same issue under XP SP-3, WinPE 2.1 and Win7. Both the previous code or this newer one you sent me provides the same results.
The output looks exactly like this:


If the variables would be correctly populated, then with my Thinkpad T500, what I should see is:

version:ThinkPad T500

I did make sure command extensions are enabled (via the cmd.exe /x command) (not sure if it's needed though). Not sure what else it could be.

Report •

May 15, 2010 at 18:49:59
try code from #4 into a code block and add echos of %%a and %%b:
for /f... do (
echo A:%%a
echo B:%%b

and see if they're coming into the script. I, for one, am stumped. also please post the entire script as it now stands,
maybe there's something i'll see in it.

Report •

May 15, 2010 at 21:13:53
Here is the code I have:

@echo off

del output.txt

wmic csproduct list full > output.txt

for /f "tokens=1* delims==" %%a in (output.txt) do set %%a=%%b

echo A:%%a
echo B:%%b
echo identifyingNumber: %identifyingnumber%
echo Name: %Name%
echo vendor: %vendor%
echo version: %version%

And here is the output when I run the code listed above:



Thats it.

Here is the actual contents of the output.txt file that is created by the wmic command running in the batch file.

Description=Computer System Product
Version=ThinkPad T500

Report •

May 15, 2010 at 21:22:07
sorry for misunderstanding, about code-blocks.
(still don't see where the glitch is!!!)
run this one, verbatim:

for /f "tokens=1* delims==" %%a in (output.txt) do (
echo a:%%a
echo b:%%b
::----- end

just post the output.
also, with %% vars, i think case IS important, so maintain
case consistancy in this test!

Report •

May 15, 2010 at 21:54:43
I pasted your exact test into a new file, ran it, and I get no output. From a the DOS shell I type in "file.cmd" and the cursor goes right to the end line. No output, no errors, no anything!

(Same result on XP SP-3, Win7 and PE 2.1)

Report •

May 15, 2010 at 22:21:30
"i believe we have reached an impasse".
your script is .cmd. That's the only difference, and i tried it
that way too. I feel kind of dumb...
I hope someone will step up to the plate and hit a homer,
for both our sakes! Everything, including this last test, worked
over here: all values were regurgitated correctly, completely,
and precisely as expected, using duplicate of "output.txt"
as given. The only way i could duplicate what you got was
to remove the closing parenthesis ")", then i got no errors or
Sorry I could not help more!

Report •

May 15, 2010 at 22:29:05
Well, thanks for all your efforts, I really do appreciate it! It is really wierd though that the same batch file runs different on our systems! The systems I am using is: Win7 32bit, Win7 64bit, XP SP-3, and Win PE2.1 (which is 32bit as well)
These are the 4 systems I was trying this one, and I get the exact same results on all four!

You certainly have no reason to feel dumb, you know more about this than I do. I just wish I could figure out what is different from my systems than yours. That is the odd thing! I did check the parenthesis, and it is as it should be, so even that is not the issue!

But like you said, hopefully somebody else will hopefully have a suggestion! Thanks again very much for your help!

Report •

May 15, 2010 at 22:48:59
I'll be watching with interest. Good luck Brother!
ps: suggest you re-post as new. lots of times people ignore "answered" posts. I want to see what the dickens is going on!

Report •

May 15, 2010 at 23:54:42
Thats a good idea (to repost it).

By the way, I got the results I was looking for, but a different way. My way required a whole lot more junk to write than is needed, but at least for now, it will do.

Read this, and don't laugh too hard :) It's quite lengthy, but it does achieve the same result (which is to populate these variables with the data from the output file)

I documented this pretty well, so you will see clearly what each crazy line is doing. hehehe

@echo off
wmic csproduct list full > c:\prep\baddata.txt
wmic csproduct list full > c:\prep\cs.txt

REM Get the "Name", "Vendor" and "Version" fields into variables
REM ============================================================
find /n "Name" c:\prep\cs.txt > name.txt
find /n "Vendor" c:\prep\cs.txt > vendor.txt
find /n "Version" c:\prep\cs.txt > version.txt

REM Populate variables with the hardware specifics
REM ==============================================
for /f "skip=1 tokens=*" %%a in ('type c:\prep\name.txt 2^>NUL') do set name=%%a
for /f "skip=1 tokens=*" %%a in ('type c:\prep\vendor.txt 2^>NUL') do set vendor=%%a
for /f "skip=1 tokens=*" %%a in ('type c:\prep\version.txt 2^>NUL') do set version=%%a

REM Remove ALL spaces from variables
REM ================================
set name=%name: =%
set vendor=%vendor: =%
set version=%version: =%

REM Remove unneeded characters from the variables
REM =============================================

set name=%name:~8%
set vendor=%vendor:~10%
set version=%version:~11%

echo 'name' = %name%
echo 'vendor' = %vendor%
echo 'version' = %version%

Pretty crazy huh? :) This one I wrote so that it has to run from c:\prep but obviously that can be changed. I am simply going to change it so that it works from x:\ since I will need this to run from a WinPE disk.

You see, what I need this thing to do is decide which model computer PE has booted from, because I am building a hardware independent WinXP ghost image for my company. the idea is that I build a WinXP system that dual boots to PE. So when a tech images a machine and boots it, it would boot PE, which will then run this script, which will let it make a decision as to what hardware it's on. (The only variable I really need is 'version' because I will simply have a list of the hardware the image will support. The batch file will simply compare it's "version' variable against a list of known hardware types, and once it finds a match, it will then copy the correct windows drivers to a folder on the C: drive. It will then run a command to delete WinPE from the partition, and issue a reboot. The system will reboot, which will launch XP and run sysprep. Sysprep will now run, and use the drivers that WinPE had copied into the correct place, and the system will now load the drivers which are correct for this particular model.

Report •

May 16, 2010 at 06:53:58
hey, if it works, it works, so don't knock it! glad you found a
"workaround". (I've seen, and written, much worse code than this).
My last thought was the file might be unicode, but you have
probably already tested for that. (A lot of diag/analysis outputs
come out as unicode).
Also, there might be another means of discovering which model is being booted... not sure yet

Report •

May 16, 2010 at 14:13:35
hello, again. Yep, sure enough, it is unicode (i discovered i
had "wmic" on my system so I could generate the file.)
with that knowledge, now all you need to do go full circle back
to resp#1 and make it like this:

for /f "tokens=*" %%a in ('type output.txt') do set %%a

then just select the var.s you want and format them as needed.

Report •

May 16, 2010 at 16:54:33
That worked!!!!!

Wow! I didn't even think about the text being unicode!!

Thanks!! Now instead of that crazy huge code I showed you, this whole thing can be done with just a few lines of code!

I really appreciate all your help!!!

Report •

Ask Question