Solved

batch file help. Using a list of printers

Posted on 2010-08-18
2
575 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 56

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

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
Make the most of your online learning experience.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
Progress

627 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