Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Batch Script Help Needed

Posted on 2010-09-23
3
Medium Priority
?
315 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
[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
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 2000 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

Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

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.
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…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

715 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