Save first match in variable, ignore rest

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
noprogrammer
Posts: 36
Joined: 29 Oct 2009 11:55

Save first match in variable, ignore rest

#1 Post by noprogrammer » 03 Dec 2016 08:42

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.?

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Save first match in variable, ignore rest

#2 Post by Squashman » 03 Dec 2016 14:59

Use a FOR /F to capture the cmd output just like the other batch files we have helped you with on this forum.

SIMMS7400
Posts: 546
Joined: 07 Jan 2016 07:47

Re: Save first match in variable, ignore rest

#3 Post by SIMMS7400 » 04 Dec 2016 06:50

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%

Post Reply