WinBatch Tech Support Home

Database Search

If you can't find the information using the categories below, post a question over in our WinBatch Tech Support Forum.

TechHome

Printing Information

Can't find the information you are looking for here? Then leave a message over on our WinBatch Tech Support Forum.

UDF to Delete a Printer

Keywords:   UDF delete a printer

This program is designed to: Delete all references to a specified printer and it's related driver . It requires the printer extender (WWPRT34I.DLL).

; *************************************************************************************************************
; *											         	 DelPrinter 1.1        						                       *
; *************************************************************************************************************
; * This program is designed to: Delete all references to a specified printer and it's related driver         *
; *																																			  *
; * Syntax: DelPrinter /? (Help) /A (All) /N (remap net printers) /P (Printer Name) /S (Run Silent)			  *
; * 						                                          														     *
; * Ex:     DelPrinter /p Xerox	/S				      																           *
; * 			This will delete all Xerox Printers																					  *
; * Written by Patrick Tyler / Paul Reynolds - GE Aircraft Engines 4/3/2002											  *
; *																																			  *
; * Revision Control:																													  *
; * 1.1 - Patrick Tyler 	5/7/2002	- Added Capability to remap network printers after delete with /n switch  *
; *************************************************************************************************************

;*******************************************************************************************************
;* Set Program to pickup all commandline arguments and not parse as parms										 *
;*******************************************************************************************************
args=IntControl (1006, 0, 0, 0, 0)

;*******************************************************************************************************
;* Add Printer Extender																								   		 *
;*******************************************************************************************************
AddExtender("WWPRT34i.DLL")

;*******************************************************************************************************
;* Initialize Variables																											 *
;*******************************************************************************************************
DisplayResults=@TRUE
DeleteAllPrinters=@FALSE
PrinterToDelete = ""
DeletedPrinters=""
reNetworkPrinters=@FALSE

;*******************************************************************************************************
;* Print Driver Delete Function																								 *
;* 																																	 *
;* Requirements 																													 *
;*  Windows NT/2000/XP: Included in Windows 2000 and later.														    *
;*******************************************************************************************************
#DefineFunction DeletePrinterDriver(DriverName)
	TimeDelay(4)
	ShellExecute('rundll32', 'printui.dll,PrintUIEntry /q /dd /m "%DriverName%" /h "Intel" /v "Windows NT 4.0 or 2000"','',@NORMAL,'')
	ShellExecute('rundll32', 'printui.dll,PrintUIEntry /q /dd /m "%DriverName%" /h "Intel" /v "Windows 2000"','',@NORMAL,'')
#EndFunction

;*******************************************************************************************************
;* Printer Delete Function																								 *
;* 																																	 *
;* Requirements 																													 *
;*  Windows NT/2000/XP: Included in Windows 2000 and later.														    *
;*******************************************************************************************************
#DefineFunction DeletePrinter(PrinterName, PrinterShare)
	if PrinterShare != ""
		ShellExecute('rundll32', 'printui.dll,PrintUIEntry /q /dn /n "%PrinterName%" /h "Intel" /v "Windows 2000"','',@NORMAL,'')
	else
		ShellExecute('rundll32', 'printui.dll,PrintUIEntry /q /dl /n "%PrinterName%" /h "Intel" /v "Windows 2000"','',@NORMAL,'')
	endif
#EndFunction

;*******************************************************************************************************
;* Parse the Commandline Arguments																							 *
;*******************************************************************************************************
argcount = ItemCount(args, "/")
if argcount > 0
	for xx =2 to argcount
		parm=ItemExtract(xx, args, "/")
		commandarg=StrUpper(StrSub(Parm,1,1))
		
		if commandarg =="?"
			HelpMessageString="Remove all Printers using a searchable name and all drivers associated."
			HelpMessageString=StrCat(HelpMessageString,@CRLF,@CRLF,"DelPrinter [/A] [/N] [/P] [/S] [/?]",@CRLF,@CRLF)
			HelpMessageString=StrCat(HelpMessageString,@TAB,"/A", @TAB,@TAB,"Delete All Printers and Drivers",@CRLF,@CRLF)
			HelpMessageString=StrCat(HelpMessageString,@TAB,"/N", @TAB,@TAB,"Re-Add Network Printers after Deleting",@CRLF,@CRLF)
			HelpMessageString=StrCat(HelpMessageString,@TAB,"/P {Partial Name}",@TAB,"Delete Specified Printers using driver name",@CRLF,@CRLF)
			HelpMessageString=StrCat(HelpMessageString,@TAB,"/S", @TAB,@TAB,"Run Silent (No Message Box Will Display)",@CRLF,@CRLF)
			HelpMessageString=StrCat(HelpMessageString,@TAB,"/?",@TAB,@TAB,"This HELP Screen",@CRLF)
			HelpMessageString=StrCat(HelpMessageString,@CRLF,@TAB,"Example:",@TAB,"DelPrinter /P Xerox /S")
			HelpMessageString=StrCat(HelpMessageString,@CRLF,@TAB,"Note:",@TAB,"This would delete all Xerox Printers and Drivers")
  			Message("DelPrinter HELP",HelpMessageString)
         goto exit
		endif
		
	  	if commandarg=="P" 
			PrinterToDelete=StrSub(parm,3,-1)
			if PrinterToDelete == "" 
				Message("Syntax Error!", "When Specifying the /p switch, you must specify a Driver Name")
				Goto exit
			endif
		endif
   
	  	if commandarg=="A" then DeleteAllPrinters=@TRUE
		
		if commandarg=="N" then reNetworkPrinters=@TRUE

	  	if commandarg=="S" THEN DisplayResults=@FALSE

	endfor
endif

;*******************************************************************************************************
;* Main Program Section																											 *
;*******************************************************************************************************
;Get Printer List for Display
printers = pGetPrtList(1)
if PrinterToDelete == "" && DeleteAllPrinters == @FALSE 
	PrinterX=AskItemList("Select Printers to Delete", printers, @tab, @sorted, @single)

	;Set Variables
	pName=ItemExtract(1,PrinterX,"|")
	pShare = ItemExtract(4,PrinterX,"|")
	pDriverName = itemextract(3,PrinterX,"|")

	;Remove Local or Network Printer
	DeletePrinter(pName, pShare)
	DeletePrinterDriver(pDriverName)
else
	NumberOfPrinters=ItemCount(Printers,@TAB)
	for xx = 1 to NumberOfPrinters
		PrinterX=ItemExtract(xx,printers,@TAB)

		;Set Variables
		pName=ItemExtract(1,PrinterX,"|")
		pShare = ItemExtract(4,PrinterX,"|")
		pDriverName = itemextract(3,PrinterX,"|")

		if DeleteAllPrinters || (StrIndexNc(pDriverName, PrinterToDelete, 1, @FWDSCAN)) > 0
	 		DeletePrinter(pName,pShare)
			DeletePrinterDriver(pDriverName)
			if DeletedPrinters == ""
				DeletedPrinters=PrinterX
			else
				DeletedPrinters=Strcat(DeletedPrinters,@TAB,PrinterX)
			endif
		endif
   next
endif

If reNetworkPrinters
	DelPrinterCount=ItemCount(DeletedPrinters,@TAB)
	Message("DelPrinter Count",DelPrinterCount)
	for yy = 1 to DelPrinterCount
		PrinterX=ItemExtract(yy,DeletedPrinters,@TAB)

		;Set Variables
		pName=ItemExtract(1,PrinterX,"|")
		pPort=ItemExtract(2,PrinterX,"|")
		pDriverName = itemextract(3,PrinterX,"|")
		pShare = ItemExtract(4,PrinterX,"|")
		if pShare != ""
			srchend=StrScan(pShare, "\", 3, @FWDSCAN)
			pServer=StrSub(pShare,1,srchend-1)
			pShareName=StrSub(pShare,srchend+1,-1)
			Message("Server and SharePoint","%pServer%%@TAB%%pShareName%")
			pAddPrtConn(pServer, pShareName)
		endif
	next
endif
		
		

:Exit
exit





Article ID:   W15275
File Created: 2002:09:05:13:50:56
Last Updated: 2002:09:05:13:50:56