Ref. FAQ - Default Printer

I am interested in cleaning up the following script..

It seems to work great..
  • What this does is to read in the default printer from the registry.
  • Checks to see if it is not Adobe Writer, Winfax, etc. exists. If so, it leaves.
  • If no printer is found, there is a check made for the INI file.
  • If the INI file is found, ADDPRINTERCONNECTION and SETDEFAULTPRINTER are initiated.
  • If the printer already exists, it skips the ADDPRINTERCONNECTION and SETDEFAULTPRINTER.
  • Once this done, it captures the printer to the INI file.
  • Lastly, a USE to LPT1 is done as we are doing more with Citrix.
code:
 ; -- Printer Configuration
; -- Kent Dyer
; -- Updated 8 August 2002
; -- Originally coded 15 August 2001
; -- 8 August 2002 - Fixed USE Commmand - Doh!
; -- 13 March 2002 - Added in debug flag. Thanks MCA!
; -- Updated on 25 February 2002 for the OS check
; -- This sets up for printers
; -- Captures the local Printer and writes to an INI file
; -- The reason for the INI is that the computer system may change location
; -- Also, if a new person logs in under a new profile, they will get the same printer
; -- This was written in frustration with Q252388
; -- http://support.microsoft.com/default.aspx?scid=kb;EN-US;q252388
; -- Comments and feedback are welcome to - dyerkb@myrealbox.com
; -- Code was inspired from - http://members.ams.chello.nl/a.westra2/800/prtshr8i.htm (no, I don't know Dutch :))

BREAK ON
CLS
:setprinter
IF "%OS%" <> "Windows_NT" ; -- is it winnt or 2K?
RETURN ; Not Windows NT/2K/XP (ADDRPRINTERCONNECTION AND SETDEFAULTPRINTER are not supported), so return
ELSE
$debug = "No" ; -- Set this to Yes for displaying the results
$ps = "%windir%\Printsetup.ini" ; -- Print Configuration File
; -- Read default printer string from registry
$subkey = "HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\Windows"
$value = ReadValue( $subkey, "Device" )
;?$value
; -- We Need to test for the existence of a Network Share being used
; -- This is done in the event of someone using Acrobat Writer
; -- Or a FAX driver instead of a networked printer as default
IF 0 = InStr($value, "\\")
RETURN ; -- If they are using a local printer driver, return
; -- Otherwise, go on to delete the networked printer and re-add it
ENDIF

; -- If $VALUE = "", does a printer exist in the Registry?
IF $value = ""
; -- We should check for the INI File
IF 1 = Exist($ps) ; -- The INI File does exist, let's read it and use the info
$def=READPROFILESTRING($ps,"Printers","Default")
$ptr=READPROFILESTRING($ps,"Printers","PtrName")
AddPrinterConnection ($def) ; -- Add the printer (only available on NT/2K)
SetDefaultPrinter ($ptr) ; -- Set the Printer to Default
;ELSE
; RETURN ; -- Printer does not exist in the Registry nor in the INI File, maybe a message here?
ENDIF
ENDIF

$offset = InStr( $value, "," ) ; Parse UNC only from string
IF $debug = "Yes"
?$offset + " Offset"
ENDIF
$len = $offset - 1
IF $debug = "Yes"
?$len + " Length"
ENDIF
$lpt1 = SUBSTR( $value, 1, $len )

IF $debug = "Yes"
; Display UNC value found
? "LPT1 = " + $LPT1 + " (Default Printer)"
ENDIF

; -- Now, we have the Default Printer, let's capture it!!
; -- While we have the chance, let's capture the printer for any new users that may use the machine
;IF 0 <> EXIST"%windir%\Printsetup.ini" ; -- Uncomment this line as well as the next endif to leave the INI file alone
$RC=WRITEPROFILESTRING($ps,"Printers","Default",$lpt1) ; -- Get the whole UNC Path and Printer

$nlen = $offset - 3
IF $debug = "Yes"
?$nlen + " New Length"
ENDIF

; OK, So now let's remove the \\
$prtn = SUBSTR( $value, 3, $nlen )
IF $debug = "Yes"
?$prtn + " Removed \\"
ENDIF

; Now, Let's grab the printer name
$offset1 = INSTR( $prtn, "\" )
$offset1 = $offset1 + 1
IF $debug = "Yes"
?$offset1 + " Offset1"
ENDIF
$len1 = $offset1
IF $debug = "Yes"
?$len1 + " Second New Length"
ENDIF

$lptn = SUBSTR( $prtn, $offset1, $len1 )
IF $debug = "Yes"
?$lptn + " Printer Name"
ENDIF
$RC=WRITEPROFILESTRING($ps,"Printers","PtrName",$lptn) ; -- Capture the Name of the Printer
;EndIf ; -- Done writing to the INI file

; -- Actual mapping - Re-Map the printer for LPT1
; -- CHECK TO SEE IF LPT1 WAS DONE WITH A NET USE
IF 0 = ExistKey("HKEY_CURRENT_USER\Network\LPT1")
; -- If the printer has been mapped then delete it to refresh the connection
; -- Delete the printer
USE LPT1: $lpt1 /del /persistent
; -- Refresh the connection to the printer as LPT1 with the string captured above
USE LPT1: $lpt1 /persistent
RETURN
ELSE
; -- ASSUME THE PRINTER HAS NOT BEEN DONE WITH A NET USE COMMAND
; -- Add it in as LPT1 anyway
USE LPT1: $lpt1 /persistent
RETURN
ENDIF
RETURN
ENDIF

I look at this code and it seems pretty intensive for a relatively simple task.

Questions:
  • Am I doing too much?
  • Should the approach be done on an array?
  • Could this be done in a different way?
Thank you.
Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's