Solved

Batch Script Help Needed

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

830 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