0

I am trying to write a script to install the correct display driver. Long story short on newer systems when you add a dedicated card the onboard is no longer disabled so when I run this command:

FOR /F "tokens=2 delims==" %%i IN ('wmic path win32_videocontroller get pnpdeviceid/value 2^>NUL ^| find /i "pnpdeviceid"') DO set vdcd=%%i

my vdcd variable is now set with the pnpdeviceid of the onboard, but when I add the wmic Where verb i get no result using this command:

FOR /F "tokens=2 delims==" %%i IN ('wmic path win32_videocontroller Where DeviceID="VideoController1" get pnpdeviceid /value 2^>NUL ^| find /i "pnpdeviceid"') DO set vdcd=%%i

Running just

wmic path win32_videocontroller Where DeviceID="VideoController1" get pnpdeviceid

does result with the desired primary display adapter. I can make a mess of things and "MAKE" it work but I like to keep my scripts as clean as possible.. Making it work by writing the simple command to a txt file and then running a separate for command to read the txt document.

A user on another site suggested I try this as a workaround

@echo off
wmic path win32_videocontroller get deviceid, pnpdeviceid | for /f "tokens=2" %%a in ('find /i "videocontroller1"') do set x=%%a
echo %x% 
pause

but I get an output of this

G:\Drivers\Display>set x=PCI\VEN_10DE&DEV_104A&SUBSYS_35451458&REV_A1\4&14466D94
&0&0008

ECHO is off.

Press any key to continue . . .

for some reason at the do set x=%%a it is instead echoing set x=%%a and not setting a value at all... very strange indeed...

Any ideas (other then "use a different language", which was also suggested)?

2
  • 1
    Please read the formatting help to see how to set up the formatting properly.
    – nhinkle
    Commented Feb 19, 2013 at 4:55
  • holy crazy craptastic... lol Commented Feb 19, 2013 at 6:35

2 Answers 2

1

Your problem stems from peculiarities of how CMD (batch) parses your code. The command within the FOR IN() clause gets parsed twice, and the parser ends up converting the = into a space unless it is escaped or quoted.

Here is a solution using escape:

FOR /F "tokens=2 delims==" %%i IN ('wmic path win32_videocontroller Where DeviceID^="VideoController1" get pnpdeviceid /value 2^>NUL ^| find /i "pnpdeviceid"') DO set vdcd=%%i


Here is a solution using quotes:

FOR /F "tokens=2 delims==" %%i IN ('wmic path win32_videocontroller Where "DeviceID='VideoController1'" get pnpdeviceid /value 2^>NUL ^| find /i "pnpdeviceid"') DO set vdcd=%%i


It is actually possible to use quotes around the entire command and eliminate all escapes:

FOR /F "tokens=2 delims==" %%i IN ('"wmic path win32_videocontroller Where DeviceID='VideoController1' get pnpdeviceid /value 2>NUL | find /i "pnpdeviceid""') DO set vdcd=%%i

Quoting and escaping in batch can get confusing very quickly, but trust me, there really are rules and it is totally predictable :-) It just may not seem that way.

0

The position of your >nul is the problem. find isn't going to find anything because you are directing the output of the command to nul. If you just don't want it to display the output, you have to redirect the find command to nul as it is what is displaying to con

FOR /F "tokens=2 delims==" %%i IN ('wmic path win32_videocontroller Where DeviceID="VideoController1" get pnpdeviceid /value 2 ^| find /i "pnpdeviceid"^>NUL') DO set vdcd=%%i

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .