Click here for important information about

for loop with wmic

Microsoft Windows xp professional w/serv...
July 21, 2010 at 04:53:10
Specs: Windows XP
I'm trying to find a specfic drive using a wmic call with diskdrive and assign the deviceid as a variable. Basically I just need to look for a certain drive and make sure its not drive0. Anyway here is my code:

for /f "usebackq delims== tokens=2" %x in ('wmic diskdrive where Description="Disk drive" get DeviceID /f
ormat:value') do echo %x

It seems like the '=' in Description="Disk drive" is not recognized in this for loop. The return back is blank but if I run the wmic command alone at a DOS prompt I get the reply back I'm looking for. I'm sure this is probably a syntax problem but I can not figure it out. I've tried escaping the = with \ and ^, I've double qouted the wmic string, double single quoated, and all other combinations I can think of. Please help

See More: for loop with wmic

July 21, 2010 at 06:02:00
I had success on the command line with:

for /f "tokens=2 delims==" %x in ('wmic diskdrive where "Description='Disk drive'" get DeviceID /format:value') do echo %x

When your only tool is a hammer, every problem looks like a nail.

Report •

July 21, 2010 at 06:10:32
FWIW, I always have delims= as my last option. I think it's because I'm paranoid about delims= taking any following FOR options literally, such as taking <blank>, t, o, k, e, n, s, =, as delimiters. Whether it actually works that way, I don't know. I never wanted to find out! ;-)

Also, the use of usebackq in the original code is using the intended command as a string, or this forum is displaying ` as '...

When your only tool is a hammer, every problem looks like a nail.

Report •

July 21, 2010 at 06:10:52
That worked. I thought I had to use the usebackq option because I had single quotes around the wmic call. Thanks for your help.

Report •
Related Solutions

Ask Question