Solved

batch file help. Using a list of printers

Posted on 2010-08-18
2
573 Views
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.

OUTPUT:
C:\WINDOWS\system32>\temp\temp.bat
 SYCH-TECH
.datmp\PSYCH-TECHion file for PSYCH-TECH
 SYCH-RESIDENCY-DUPLEX
.datmp\PSYCH-RESIDENCY-DUPLEX PSYCH-RESIDENCY-DUPLEX
 SYCH-RESIDENCY
.datmp\PSYCH-RESIDENCYile for PSYCH-RESIDENCY
 SYCH-DCAP-COLOR-DUPLEX
.datmp\PSYCH-DCAP-COLOR-DUPLEXPSYCH-DCAP-COLOR-DUPLEX
 SYCH-DCAP-COLOR-BW-DUPLEX
.datmp\PSYCH-DCAP-COLOR-BW-DUPLEXCH-DCAP-COLOR-BW-DUPLEX
 SYCH-DCAP-COLOR-BW
.datmp\PSYCH-DCAP-COLOR-BWfor PSYCH-DCAP-COLOR-BW
 SYCH-DCAP-COLOR
.datmp\PSYCH-DCAP-COLORle for PSYCH-DCAP-COLOR
 SYCH-DCAP
.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"
Name=PSYCH-TECH
Name=PSYCH-RESIDENCY-DUPLEX
Name=PSYCH-RESIDENCY
Name=PSYCH-DCAP-COLOR-DUPLEX
Name=PSYCH-DCAP-COLOR-BW-DUPLEX
Name=PSYCH-DCAP-COLOR-BW
Name=PSYCH-DCAP-COLOR
Name=PSYCH-DCAP
@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
	)


endlocal

Open in new window

0
Comment
Question by:pphreadr
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 54

Accepted Solution

by:
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.

~bp


@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
        )
 
endlocal

Open in new window

0
 

Author Closing Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
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 …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

752 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