Page 1 of 3 123>
Topic Options
#74434 - 2003-04-11 07:57 PM Re-Write of "Default Printer" FAQ
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
{Edit - Code completed!!}
Looking at doing a re-write of the FAQ for the "Default Printer." While the newest version works great for one printer.. What happens when a person has multiple network printers and they get a new machine? I started think about this as this became evident with the recent printer migration we were under. I would welcome any feedback/comments in regards to this script re-write.



CLS
 BREAK ON
 ;Re-Write of http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=10;t=000021
 ;Default Printer Configuration
 ;This is to capture the Default Printer and get all network defined printers as well
 $local='HKEY_CURRENT_USER\Network\LPT1'
 $reg='HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion'
 $dev=$reg+'\Devices'
 $ptr=$reg+'\Windows'
 $nptr=READVALUE($ptr,'Device')
 $lpt=SUBSTR($nptr,1,InStr($nptr,',')-1)
 $sysdrv=SUBSTR('%WINDIR%',1,2)
 IF EXIST('H:')
       $ps='H:\Scripts_DO_NOT_DELETE\Printsetup.ini'
       IF 0=EXIST('H:\Scripts_DO_NOT_DELETE')
             MD 'H:\Scripts_DO_NOT_DELETE'
       ENDIF
 ELSE
       $ps=$sysdrv+'\Scripts\Printsetup.ini'
       IF 0=EXIST($sysdrv+'\Scripts')
             MD $sysdrv+'\Scripts'
       ENDIF
 ENDIF

 ;Ref. - http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=12;t=000221
 ;Thanks Howard!
 $index=0
 DIM $keyname[$index]
 IF KeyExist($dev) AND EXIST($ps)
       DO
             $x=ENUMVALUE($dev,$index)
             $error=@error
             IF NOT $error AND $index>Ubound($keyname)
                   REDIM PRESERVE $keyname[$index]
             ENDIF
             IF NOT $error
                   $keyname[$index]=$x
                   IF INSTR($x,'\\')
                        ;REMOVE THE PRINTER SETTINGS FILE AS PRINTERS MAYBE ADDED/REMOVED
                        DEL $ps
                   ENDIF
                   $index=$index+1
             ENDIF
       UNTIL $error
 ELSE
       $keyname[0]=''
 ENDIF

 ;THIS CAPTURES ALL NETWORK PRINTERS INSTALLED
 $index=0
 IF KeyExist($dev)
       DO
             $x=ENUMVALUE($dev,$index)
             $error=@error
             IF NOT $error AND $index>Ubound($keyname)
                   REDIM PRESERVE $keyname[$index]
             ENDIF
             IF NOT $error
                   $keyname[$index]=$x
                   IF INSTR($x,'\\') AND $x<>$lpt
                         $rc=WRITEPROFILESTRING($ps,'Printers',$index,$x)
                   ENDIF
                   $index=$index+1
             ENDIF
       UNTIL $error
 ELSE
       $keyname[0]=''
 ENDIF

 ;CHECK FOR THE DEFAULT PRINTER
 SELECT
 CASE 
       INSTR($nptr,'\\')
       ;WRITE THE PRINTER TO THE CONFIG AS THE LOCAL NET PRINTER MAY HAVE CHANGED
       $rc=WRITEPROFILESTRING($ps,'Printers','Default',$lpt)
       $lpt1=READPROFILESTRING($ps,'Printers','Default')
       $rc=ADDPRINTERCONNECTION($lpt1)
       $rc=SETDEFAULTPRINTER($lpt1)
       USE LPT1: /delete /persistent
       USE LPT1: $lpt1 /persistent
 CASE
       $nptr='' AND EXIST($ps)
       $lpt1=READPROFILESTRING($ps,'Printers','Default')
       $rc=ADDPRINTERCONNECTION($lpt1)
       $rc=SETDEFAULTPRINTER($lpt1)
       USE LPT1: $lpt1 /persistent
       ;Ref. - http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic&f=1&t=006790
       ;Thanks Doc for the ideas and Lonkero for the fixes!
       FOR EACH $key IN split(readprofilestring($ps,'Printers',''),chr(10))
             IF $key<>'Default'
                   $nul=addprinterconnection(readprofilestring($ps,'Printers',$key))
             ENDIF
       NEXT
 CASE
       ;LOCAL PRINTER INSTALLED, BUT WE WANT TO REMOVE ANY NETWORK MAPPING
       INSTR($nptr,'LPT') AND READVALUE($local,'RemotePath')<>''
       USE LPT1: /delete /persistent
 ENDSELECT
 RETURN



Thanks,

Kent

[ 12. April 2003, 18:33: Message edited by: kdyer ]
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#74435 - 2003-04-11 11:55 PM Re: Re-Write of "Default Printer" FAQ
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Done some testing with the latest edit on this and the results look pretty good. I added in five network printers as well as a locally installed printer. The only thing that I have not tested this with is applications like: WinFAX, RightFAX, Acrobat, etc.

Thanks,

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

Top
#74436 - 2003-04-12 12:04 AM Re: Re-Write of "Default Printer" FAQ
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Potential gotcha..

I deleted a network printer and re-read the script and it adds the network printer back..

Hmm.

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

Top
#74437 - 2003-04-12 07:51 AM Re: Re-Write of "Default Printer" FAQ
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
OK..

Think we are at a point to boil this down. Should have all the working components there. Now, we need to simplify. [Big Grin]

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

Top
#74438 - 2003-04-13 02:17 AM Re: Re-Write of "Default Printer" FAQ
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
not sure about why, but you have that keyname array there.
you don't use it in anyway though!

weird.
_________________________
!

download KiXnet

Top
#74439 - 2003-04-13 02:23 AM Re: Re-Write of "Default Printer" FAQ
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
ok, after some thinking, this scriptlet should take care of everything in your script between lines:
quote:
;Ref. - ht
and:
quote:
;CHECK
so:
code:
$index=0
do
if '\\'=left($x,2)
$priReg=$priReg+$x
endif
$index=$index+1
$x=enumvalue($dev,$index)
until @error=0 or not len($x)

if len($priReg)
if exist($ps)
del $ps
endif
for each $x in split(substr($priReg,3),'\\')
$rc=writeprofilestring($ps,'Printers',$index,'\\'+$x)
next
endif

_________________________
!

download KiXnet

Top
#74440 - 2003-04-13 03:00 AM Re: Re-Write of "Default Printer" FAQ
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
also:
code:
CASE
$nptr='' AND EXIST($ps)
$lpt1=READPROFILESTRING($ps,'Printers','Default')
$rc=ADDPRINTERCONNECTION($lpt1)
$rc=SETDEFAULTPRINTER($lpt1)
USE LPT1: $lpt1 /persistent
;Ref. - http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic&f=1&t=006790
;Thanks Doc for the ideas and Lonkero for the fixes!
FOR EACH $key IN split(readprofilestring($ps,'Printers',''),chr(10))
IF $key<>'Default'
$nul=addprinterconnection(readprofilestring($ps,'Printers',$key))
ENDIF
NEXT

is just the same as:
code:
CASE
$nptr='' AND EXIST($ps)
FOR EACH $key IN split(readprofilestring($ps,'Printers',''),chr(10))
$nul=addprinterconnection(readprofilestring($ps,'Printers',$key))
NEXT
$rc=SETDEFAULTPRINTER(READPROFILESTRING($ps,'Printers','Default'))
USE LPT1: $lpt1 /persistent

_________________________
!

download KiXnet

Top
#74441 - 2003-04-13 05:09 PM Re: Re-Write of "Default Printer" FAQ
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Jooel,

Thank you for your feedback.

Here is the code at this point:



CLS
 BREAK ON
 ;Re-Write of http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=10;t=000021
 ;Default Printer Configuration
 ;This is to capture the Default Printer and get all network defined printers as well
 ;Ref. http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic&f=1&t=006790
 ;Ref. http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=12;t=000221
 $loc='HKEY_CURRENT_USER\Network\LPT1'
 $reg='HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion'
 $dev=$reg+'\Devices'
 $ptr=$reg+'\Windows'
 $nptr=READVALUE($ptr,'Device')
 $lpt=SUBSTR($nptr,1,INSTR($nptr,',')-1)
 $sysdrv=SUBSTR('%WINDIR%',1,2)
 IF EXIST('H:')
  $ps='H:\Scripts_DO_NOT_DELETE\Printsetup.ini'
  IF 0=EXIST('H:\Scripts_DO_NOT_DELETE')
   MD 'H:\Scripts_DO_NOT_DELETE'
  ENDIF
 ELSE
  $ps=$sysdrv+'\Scripts\Printsetup.ini'
  IF 0=EXIST($sysdrv+'\Scripts')
   MD $sysdrv+'\Scripts'
  ENDIF
 ENDIF

 $index=0
 DO
  IF '\\'=LEFT($x,2)
   $priReg=$priReg+$x
  ENDIF
  $index=$index+1
  $x=ENUMVALUE($dev,$index)
 UNTIL @error=0 OR NOT LEN($x)

 IF LEN($priReg)
  IF EXIST($ps)
   DEL $ps
  ENDIF
  FOR EACH $x IN SPLIT(SUBSTR($priReg,3),'\\')
   $rc=WRITEPROFILESTRING($ps,'Printers',$index,'\\'+$x)
  NEXT
 ENDIF

 ;CHECK FOR THE DEFAULT PRINTER
 SELECT
 CASE 
  INSTR($nptr,'\\')
  ;WRITE THE PRINTER TO THE CONFIG AS THE LOCAL NET PRINTER MAY HAVE CHANGED
  $rc=WRITEPROFILESTRING($ps,'Printers','Default',$lpt)
  $lpt1=READPROFILESTRING($ps,'Printers','Default')
  USE LPT1: /delete /persistent
  USE LPT1: $lpt1 /persistent
 CASE
  $nptr='' AND EXIST($ps)
  FOR EACH $key IN split(readprofilestring($ps,'Printers',''),chr(10))
   $nul=addprinterconnection(readprofilestring($ps,'Printers',$key))
  NEXT
   $rc=SETDEFAULTPRINTER(READPROFILESTRING($ps,'Printers','Default'))
   USE LPT1: $lpt1 /persistent
 CASE
  ;LOCAL PRINTER INSTALLED, BUT WE WANT TO REMOVE ANY NETWORK MAPPING
  INSTR($nptr,'LPT') AND READVALUE($loc,'RemotePath')<>''
  USE LPT1: /delete /persistent
 ENDSELECT
 RETURN



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

Top
#74442 - 2003-04-13 05:45 PM Re: Re-Write of "Default Printer" FAQ
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Jooel,

Implemented the changes suggested.

However, I added in three Network printers and only the 'Default' shows in the INI file.

I think there is something with this:

code:
 $index=0
DO
IF '\\'=LEFT($x,2)
$priReg=$priReg+$x
ENDIF
$index=$index+1
$x=ENUMVALUE($dev,$index)
UNTIL @error=0 OR NOT LEN($x)

IF LEN($priReg)
IF EXIST($ps)
DEL $ps
ENDIF
FOR EACH $x IN SPLIT(SUBSTR($priReg,3),'\\')
$rc=WRITEPROFILESTRING($ps,'Printers',$index,'\\'+$x)
NEXT
ENDIF

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

Top
#74443 - 2003-04-13 08:56 PM Re: Re-Write of "Default Printer" FAQ
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
I think I found it...

Changed the following to -

UNTIL @error<>0 OR NOT len($x)

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

Top
#74444 - 2003-04-13 09:24 PM Re: Re-Write of "Default Printer" FAQ
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
yep.
forgot to change the line when changed logic from while to do-loop.

also:
code:
CASE 
INSTR($nptr,'\\')
;WRITE THE PRINTER TO THE CONFIG AS THE LOCAL NET PRINTER MAY HAVE CHANGED
$rc=WRITEPROFILESTRING($ps,'Printers','Default',$lpt)
$lpt1=READPROFILESTRING($ps,'Printers','Default')
USE LPT1: /delete /persistent
USE LPT1: $lpt1 /persistent

can be shortened to:

CASE	INSTR($nptr,'\\')
;WRITE THE PRINTER TO THE CONFIG AS THE LOCAL NET PRINTER MAY HAVE CHANGED
$rc=WRITEPROFILESTRING($ps,'Printers','Default',$lpt)
USE LPT1: /delete /persistent
USE LPT1: $lpt /persistent

_________________________
!

download KiXnet

Top
#74445 - 2003-04-13 10:49 PM Re: Re-Write of "Default Printer" FAQ
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Jooel,

Thanks for the update..

However, found a problem. Installed 3 network printers and it only picks up two of them. Note: the change of $index=index+1.

code:
 $index=0
do
if '\\'=left($x,2)
$priReg=$priReg+$x
endif
$x=enumvalue($dev,$index)
$index=$index+1
until @error<>0 or not len($x)

if len($priReg)
if exist($ps)
del $ps
endif
for each $x in split(substr($priReg,3),'\\')
?$priReg
$rc=writeprofilestring($ps,'Printers',$index,'\\'+$x)
next
endif

Thanks,

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

Top
#74446 - 2003-04-13 11:18 PM Re: Re-Write of "Default Printer" FAQ
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
you saying that after the change it's still not working?
_________________________
!

download KiXnet

Top
#74447 - 2003-04-14 12:15 AM Re: Re-Write of "Default Printer" FAQ
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
It works but does not grab all for the INI file.

It only gets the first one of the three and the 'Default' Printer.

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

Top
#74448 - 2003-04-14 04:27 AM Re: Re-Write of "Default Printer" FAQ
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Jooel,

The INI file has:

quote:

[Printers]
3=\\PRINTSERVER\PRINTER-P8
Default=\\PRINTSERVER\PRINTER-27

So, does this mean that printer 1 was deleted? The 'Default' is created afterward.

Printers should be as follows:

\\PRINTSERVER\PRINTER-P8 > Test
\\PRINTSERVER\PRINTER-P19 > Color
\\PRINTSERVER\PRINTER-27 > Default

Thanks,

Kent

[ 14. April 2003, 04:28: Message edited by: kdyer ]
_________________________
Utilize these resources:
UDFs (Full List)
KiXtart FAQ & How to's

Top
#74449 - 2003-04-14 05:00 AM Re: Re-Write of "Default Printer" FAQ
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
have to check it more.
actually, it craps just the last one...
mm...

default printer should be listed also as printer in index too...
_________________________
!

download KiXnet

Top
#74450 - 2003-04-14 05:27 AM Re: Re-Write of "Default Printer" FAQ
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
k, tried the registry part.
it works just fine for me.

anyway, you can drop the len() part from the until.
resulting in just:
code:
 UNTIL @error 

_________________________
!

download KiXnet

Top
#74451 - 2003-04-14 10:21 AM Re: Re-Write of "Default Printer" FAQ
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
k, scanned the whole script and now it should work.


;Re-Write of http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=10;t=000021
;Default Printer Configuration
;This is to capture the Default Printer and get all network defined printers as well
;Ref. http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic&f=1&t=006790
;Ref. http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=12;t=000221
$loc='HKEY_CURRENT_USER\Network\LPT1'
$reg='HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion'
$dev=$reg+'\Devices'
$ptr=$reg+'\Windows'
$nptr=READVALUE($ptr,'Device')
$lpt=split($nptr',')[0]
$sysdrv=SUBSTR('%WINDIR%',1,2)
dim $priReg$x$i$rc$ps

IF EXIST('H:')
 $ps='H:\Scripts_DO_NOT_DELETE'
ELSE
 $ps=$sysdrv+'\Scripts'
ENDIF
IF 0=EXIST($ps)
 MD $ps
ENDIF
$ps=$ps+'\Printsetup.ini'

$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','','';cleans old data
 $priReg=split($priReg,'\\')
 FOR $i=1 to ubound($priReg)
  $rc=WRITEPROFILESTRING($ps,'Printers',$i,'\\'+$priReg[$i])
 NEXT
ENDIF

;CHECK FOR THE DEFAULT PRINTER
SELECT
 CASE 
  INSTR($nptr,'\\')
  ;WRITE THE PRINTER TO THE CONFIG AS THE LOCAL NET PRINTER MAY HAVE CHANGED
  $rc=WRITEPROFILESTRING($ps,'Printers','Default',$lpt)
  USE LPT1: /delete /persistent
  USE LPT1: $lpt /persistent
 CASE
  $nptr='' 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
   $lpt=READPROFILESTRING($ps,'Printers','Default')
   $rc=SETDEFAULTPRINTER($lpt)
   USE LPT1: $lpt /persistent
 CASE
  ;LOCAL PRINTER INSTALLED, BUT WE WANT TO REMOVE ANY NETWORK MAPPING
  INSTR($nptr,'LPT') AND READVALUE($loc,'RemotePath')<>''
  USE LPT1: /delete /persistent
ENDSELECT

_________________________
!

download KiXnet

Top
#74452 - 2003-04-14 02:13 PM Re: Re-Write of "Default Printer" FAQ
Kdyer Offline
KiX Supporter
*****

Registered: 2001-01-03
Posts: 6241
Loc: Tigard, OR
Jooel,

One minor thing (KiX 4.20+ Rocks!)..

Changed -

$lpt=split($nptr',')[0]

To be -

$lpt=split($nptr,',')[0]

Otherwise, this works awesome!

[Cool] [Cool] [Big Grin]

Can I rate you a 12?

[Big Grin] [Big Grin]

Thanks,

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

Top
#74453 - 2003-04-14 02:15 PM Re: Re-Write of "Default Printer" FAQ
Lonkero Administrator Offline
KiX Master Guru
*****

Registered: 2001-06-05
Posts: 22346
Loc: OK
yea, forgot that.

sure you can! [Big Grin]
_________________________
!

download KiXnet

Top
Page 1 of 3 123>


Moderator:  Glenn Barnas, NTDOC, Arend_, Jochen, Radimus, Allen, ShaneEP, Ruud van Velsen, Mart 
Hop to:
Shout Box

Who's Online
1 registered (Allen) and 2145 anonymous users online.
Newest Members
BeeEm, min_seow, Audio, Hoschi, Comet
17882 Registered Users

Generated in 0.076 seconds in which 0.028 seconds were spent on a total of 13 queries. Zlib compression enabled.

Search the board with:
superb Board Search
or try with google:
Google
Web kixtart.org