Solved

Batch Script Help Needed

Posted on 2010-09-23
3
310 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Sed question 2 70
how to run my script during boot in rhel 7 14 104
shell script or linux command to upload a directory to artifactory? 2 90
cloning computer 13 68
Ok I have been working on this for some time having learned and gained certification in XenDesktop 4 along came version 5 which was released last month. Since then I have been working to deploy XenDesktop 5 in a small environment with only 2 virt…
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup" or a blinking cursor with black screen. A loop for Auto repair will start but fix nothing.  You will be panic as there are no back…
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…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

864 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

21 Experts available now in Live!

Get 1:1 Help Now