batch file help. Using a list of printers

Posted on 2010-08-18
Last Modified: 2012-05-10
I have a batch that will get a list of printers installed on a local machine. The output is where my problem is. The batch gets the list and will print it to the screen, however in the for loop if there is a space or any other charactor after the %%a then the output wraps to the beginning of the line.

here is the output of the code below. I would like to pass the list into the for loop for use in creating printer config files....but it will not work with the current output.

.datmp\PSYCH-TECHion file for PSYCH-TECH
.datmp\PSYCH-DCAPion file for PSYCH-DCAP

Here is the output of the wmic command:
C:\WINDOWS\system32>wmic /node:"%computername%" printer get name /value | findstr /i /c:"=" | findstr /i /v /c:"onenote" /c:"xps"  /c:"pdf"
@echo off

setlocal enableextensions enabledelayedexpansion

set printname=wmic /node:"%computername%" printer get name /value ^| findstr /i /c:"=" ^| findstr /i /v /c:"onenote"  /c:"xps"  /c:"pdf" 

for /f "delims== tokens=2" %%a in ( '!printname!' ) do (
	echo %%a 
	echo saving configuration file for %%a \temp\%%a.dat
	REM rundll32 printui.dll,printuientry /ss /n "%%a" /a "c:\temp\%%a.dat" g d


Open in new window

Question by:pphreadr
LVL 54

Accepted Solution

Bill Prew earned 125 total points
ID: 33469081
Give the attached a try, this is a work around for somewhat quirky formatting that WMIC does.  My approach is first to use the CSV output format from WMIC when I script, this elliminates some of the extra lines, and produces output that is easy to parse.  To see this in action try the following from a command line:

wmic /node:"%computername%" printer get name /value /format:csv

Notice that is does add the computer name as the first column all the time.

To get around the carriage return at the end of the line that WMIC returns, I just add a field to the GET after the field that I want, and then just grab the one field (token) that I am interested in in the FOR.  Also I skip the first line, since it is a field name line.

Let me know if this doesn't work, or doesn't make sense.


@echo off
setlocal enableextensions enabledelayedexpansion
set printname=wmic /node:"%computername%" printer get name^,workoffline /value /format:csv ^| findstr /i /v /c:"onenote" /c:"xps" /c:"pdf" 
for /f "skip=1 delims=, tokens=2" %%a in ( '!printname!' ) do (
        echo %%a 
        echo saving configuration file for %%a \temp\%%a.dat
        REM rundll32 printui.dll,printuientry /ss /n "%%a" /a "c:\temp\%%a.dat" g d

Open in new window


Author Closing Comment

ID: 33496178
I had to use 'skip=2', but worked great. thankx

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question