Stack Exchange Network
Stack Exchange network consists of 183 Q&A communities including
Stack Overflow
, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.
Visit Stack Exchange
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this community
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
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)?
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.
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