Solved PNP - Batch Find String

September 15, 2012 at 20:44:28
Specs: Windows 7
Hi,
In Windows 7 i am using PNPUtil -e to get the "Published name : oem98.inf"
For class: Printers. I am trying to make a batch file to run on each PC using
"Class : Printers" to get oem and assign it to a varaible. I was wondering if someone can help
Thanks.

Example:


Published name : oem98.inf
Driver package provider : Microsoft
Class : Printers
Driver date and version : 06/21/2006 6.1.7600.16385
Signer name : Microsoft Windows


See More: PNP - Batch Find String

Report •

✔ Best Answer
September 18, 2012 at 21:04:35
:switch to "local" (internal) variables vs global, and enables del.exp which allows !x!
@echo off & setlocal enabledelayedexpansion

: tokens=var.assignment (%%a, %%b in order), delimted by "delims" (here: ":")
:so %%a is first item in front of ":", %%b is next item after ":"
for /f "tokens=1,2* delims=:" %%a in ('pnputil -e') do (
:so, if line looks like: published name: oem89.inf
:then x is given value of %%b which is "oem89.inf"
if %%a equ "Published name " (
set x=%%b
) else (
:: now look for the "class" trigger and check for "Printers" as the second element
:: ie: line looks like: Class:Printers"
if %%a equ "Class " (
set test=%%b
:The ~21 strips off leading spaces
set test=!test:~21!
if !test! equ Printers echo !x!
)
)
)

Now, since you didn't answer my last question... :-(
if you DO want global, you need to eliminate the setlocal, as in this script:

@echo off
for /f "tokens=1,2* delims=:" %%a in ('pnputil -e') do (
if %%a equ "Published name " (
set x=%%b
) else (
if %%a equ "Class " (
set test=%%b
set test=!test:~21!
if !test! equ Printers goto :setglob
)
)
)
echo "no pattern match"
goto :eof
:setglob
set x=%x: =%
set printers=%x%
::===== end
again, note, THIS version will only pick up the FIRST instance, not the last or any other instances of the pattern, due to the "goto" exit.



#1
September 15, 2012 at 22:42:40
this might be a start. very rough draft:

::=== begin script
@echo off & setlocal enabledelayedexpansion
for /f "tokens=1,2* delims=:" %%a in ('pnputil -e') do (
if %%a equ "Published name " (
set x=%%b
) else (
if %%a equ "Class " (
set test=%%b
set test=!test:~21!
if !test! equ Printers echo !x!
)
)
)
:: ==== end


Report •

#2
September 16, 2012 at 19:18:53
Thanks for the reply.
i have to find "class: Printer" and then set a variable to the oem# (published name) of the printer. so i do not understand how above script can help.

Report •

#3
September 16, 2012 at 19:52:56
change this line:
if !test! equ Printers echo !x!
to this:
if !test! equ Printers set printers=!x!

this should capture the value you want into variable name %printers%

ps: if there's more than one, this will only get the last one, as written. it could be modified to get multiples however.


Report •

Related Solutions

#4
September 17, 2012 at 05:06:56
Hi,
Still is not working if you have windows7 and go to cmd and type in pnputil -e you can see the output.

Thanks.


Report •

#5
September 17, 2012 at 18:04:40
I think, maybe, you want the variable to be out in the environment, and not local? (ie: after running the script, you can type "set" and see "printers=oem82.inf").
The filter is working, on my win-seven, i get correct data. It is not being exported due to the setlocal constraints, which will have to be circumvented for a global variable to be created. Let me know if so, and i will fix.

Report •

#6
September 18, 2012 at 05:13:15
Thank you for your reply.
I used set command and did not see printers=oem82.inf, i tried it out few times.
all i see when i replace to echo on is printers=!x!
Could you please explain the lines in the script.

I really appreciate your help.


Report •

#7
September 18, 2012 at 21:04:35
✔ Best Answer
:switch to "local" (internal) variables vs global, and enables del.exp which allows !x!
@echo off & setlocal enabledelayedexpansion

: tokens=var.assignment (%%a, %%b in order), delimted by "delims" (here: ":")
:so %%a is first item in front of ":", %%b is next item after ":"
for /f "tokens=1,2* delims=:" %%a in ('pnputil -e') do (
:so, if line looks like: published name: oem89.inf
:then x is given value of %%b which is "oem89.inf"
if %%a equ "Published name " (
set x=%%b
) else (
:: now look for the "class" trigger and check for "Printers" as the second element
:: ie: line looks like: Class:Printers"
if %%a equ "Class " (
set test=%%b
:The ~21 strips off leading spaces
set test=!test:~21!
if !test! equ Printers echo !x!
)
)
)

Now, since you didn't answer my last question... :-(
if you DO want global, you need to eliminate the setlocal, as in this script:

@echo off
for /f "tokens=1,2* delims=:" %%a in ('pnputil -e') do (
if %%a equ "Published name " (
set x=%%b
) else (
if %%a equ "Class " (
set test=%%b
set test=!test:~21!
if !test! equ Printers goto :setglob
)
)
)
echo "no pattern match"
goto :eof
:setglob
set x=%x: =%
set printers=%x%
::===== end
again, note, THIS version will only pick up the FIRST instance, not the last or any other instances of the pattern, due to the "goto" exit.


Report •

Ask Question