Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 580
  • Last Modified:

batch file help. Using a list of printers

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
pphreadr
Asked:
pphreadr
1 Solution
 
Bill PrewCommented:
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
 
pphreadrAuthor Commented:
I had to use 'skip=2', but worked great. thankx
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now