Solved

Batch Script Help Needed

Posted on 2010-09-23
3
308 Views
Last Modified: 2012-05-10
I am trying to run a command using a Batch file script to change a printer name if it exists.  Here's the rundown of the PseudoCode I am trying to accomplish.

If "Zebra LP2844" Printer name exists
  Change printer name to ComputerName_LP2844
Else
   END/Do Nothing

Now the command to change the printer name is:
     rundll32 PrintuUI.DLL,PrintUIEntry /XS /n "Zebra LP2844" PrinterName "%COMPUTERNAME%_LP2844"

This however assumes there is a printer specifically named "Zebra LP2844" otherwise it gives an error, which is something I need it to not do.  I thought an IF statement would be best but a try catch would be fine to handle the error, it doesn't matter so long as the end user never sees the error and the printer name is changed if it is incorrect.  Thanks.
0
Comment
Question by:PhillipsPlastics
3 Comments
 
LVL 1

Expert Comment

by:stupsnose
ID: 33747801
You cannot do what you want. Because you pass the printername to rundll, the error is thrown by rundll, i think as a messagebox. You cannnot hide that from commandline
0
 
LVL 14

Accepted Solution

by:
Erik Pitti earned 500 total points
ID: 33747896
This script will query for installed printers and rename the one that matches OLD_PRINTER_NAME to the value of NEW_PRINTER_NAME
@ECHO OFF
SETLOCAL

SET OLD_PRINTER_NAME="Zebra LP2844"
SET NEW_PRINTER_NAME="%COMPUTERNAME%_LP2844"

SET REGPATH=HKLM\System\CurrentControlSet\Control\Print\Printers

FOR /F "tokens=6* delims=\" %%A in ('reg query %REGPATH%') DO (
	IF "%%B" EQU %OLD_PRINTER_NAME% (
		rundll32 PrintuUI.DLL,PrintUIEntry /XS /n %OLD_PRINTER_NAME% PrinterName %NEW_PRINTER_NAME%
	)
)

ENDLOCAL

Open in new window

0
 

Author Comment

by:PhillipsPlastics
ID: 33748200
@ECHO OFF
SETLOCAL

SET OLD_PRINTER_NAME="Zebra LP2844"
SET NEW_PRINTER_NAME="%COMPUTERNAME%_LP2844"

SET REGPATH=HKLM\System\CurrentControlSet\Control\Print\Printers

FOR /F "tokens=6* delims=\" %%A in ('reg query %REGPATH%') DO (
      IF "%%B" EQU %OLD_PRINTER_NAME% (
            rundll32 PRINTUI.DLL,PrintUIEntry /Xs /n %OLD_PRINTER_NAME% PrinterName %NEW_PRINTER_NAME%
      )
)

ENDLOCAL

That was awesome works great thanks for the help!
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

If your system is showing symptoms of browser hijacks or 'google search redirects' check out my other article (http://rdsrc.us/u3GP7A) first and run the tool TDSSKiller (http://rdsrc.us/GDBBs4) to get rid of the infection. Once done, and if the …
Utilizing an array to gracefully append to a list of EmailAddresses
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

757 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now