Trying to create a script to capture all network printers to a file, delete them, and re-add..

However, it does not delete the drivers listed at -

HKLM\SYSTEM\CurrentControlSet\Control\Print\Environments\Windows NT x86\Drivers\Version-3

I think this needs some clean up as I am a bit stuck right now. [Frown]

Here is the code -

code:
 CLS
BREAK ON

$ps=@scriptdir+'\Printsetup.ini'
?'Backing up all printers to a file'
$j=0
DO ENUMVALUE('HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices',$j)
IF primapstate($key)=2
$rc=WRITEPROFILESTRING($ps,'Printers','Default',$key)
ELSE
$i=0
DO
IF '\\'=LEFT($x,2)
$prireg=$prireg+$x
ENDIF
$x=ENUMVALUE($dev,$i)
$i=$i+1
UNTIL @error
IF LEN($prireg)
$rc=WRITEPROFILESTRING($ps,'Printers','','')
$prireg=split($prireg,'\\')
FOR $i=1 TO ubound($prireg)
$rc=WRITEPROFILESTRING($ps,'Printers',$i,'\\'+$prireg[$i])
NEXT
ENDIF
ENDIF
$y=ENUMVALUE('HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices',$j)
$j=$j+1
UNTIL @error

?'undoing lpt1'
USE LPT1 /delete /persistent

?'Deleting all network printers'
Delprinterconnections

?'Re-Mapping all network printers'
REMAP($nprt,$ps)

?'Deleting work file'
DEL $ps

?'All done - closing in two seconds'
SLEEP 2

FUNCTION REMAP($nprt,$ps)
DIM $rc, $nprt, $ps
SELECT
CASE
INSTR($nprt,'\\') ;WRITE THE DEFAULT PRINTER TO THE CONFIG
$rc=WRITEPROFILESTRING($ps,'Printers','Default',$nprt)
USE LPT1: /delete /persistent
USE LPT1: $nprt /persistent
CASE
$nprt='' AND EXIST($ps) ;NO DEFAULT PRINTER, LOAD PRINTERS FROM SAVED INFO
FOR EACH $key IN split(readprofilestring($ps,'Printers',''),chr(10))
$nul=addprinterconnection(readprofilestring($ps,'Printers',$key))
NEXT
$nprt=READPROFILESTRING($ps,'Printers','Default')
$rc=SETDEFAULTPRINTER($nprt)
USE LPT1: $nprt /persistent
CASE
NOT INSTR($nprt,'\\') ;LOCAL PRINTER DEFAULT, SO WE WANT TO REMOVE NETWORK MAPPING
USE LPT1: /delete /persistent
ENDSELECT
ENDFUNCTION

FUNCTION DelPrinterConnections()
DIM $c,$bk,$conn
$c=0
$bk="HKCU\Printers\Connections"
$conn=enumkey($bk,$counter)
WHILE @error=0
$c=$c+1
$nconn='\\'+SPLIT($conn,',')[2]+'\'+SPLIT($conn,',')[3]
$rc=DELPRINTERCONNECTION($nconn)
$conn=delkey($bk+"\"+$conn)
$conn=enumkey($bk,$counter)
LOOP
ENDFUNCTION


FUNCTION PriMapState($_pri)
IF len(readvalue("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Devices",$_pri))
IF instr(readvalue("HKCU\Software\Microsoft\Windows NT\CurrentVersion\Windows","Device"),$_pri)
$primapstate=2
ELSE
$primapstate=1
ENDIF
ENDIF
ENDFUNCTION

Thanks,

Kent
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's