Page 1 of 1

Save first match in variable, ignore rest

Posted: 03 Dec 2016 08:42
by noprogrammer
Hi,

I want to set the default (local) printer in a script. Basically, what I do is checking the default printer device.
If it's "Adobe PDF", I want to look for another device and make it the default printer. I've got that part of the script already.

The line

Code: Select all

wmic Printer where name!='Adobe PDF' get name /value | findstr /r /v "^$"
gets me all installed printers. I'd like to take the first match/line only and assign it to a variable.

Is there a straightforward method without writing to temporary files etc.?

Re: Save first match in variable, ignore rest

Posted: 03 Dec 2016 14:59
by Squashman
Use a FOR /F to capture the cmd output just like the other batch files we have helped you with on this forum.

Re: Save first match in variable, ignore rest

Posted: 04 Dec 2016 06:50
by SIMMS7400
Give this a shot:

Code: Select all

@ECHO OFF
for /F "delims=" %%j in ('wmic printer where "name!='Adobe PDF'" get name /value ^| findstr /r /v "^$"') DO SET DEFAULT=%%j
ECHO %DEFAULT%