Page 1 of 1 1
Topic Options
#43508 - 2003-07-29 01:36 PM removing printers
Jonas1 Offline
Lurker

Registered: 2003-07-29
Posts: 4
Loc: Dordrecht
I want to remove the 21 network printers from my profile using the following script.

break on
$Index = 0
do
$KeyName = ENUMKEY("HKEY_CURRENT_USER\Printers\Connections", $Index)
$x=left("$KeyName",13)
if $x=",,DRPDRD-KA01"
Delprinterconnection ($KeyName)
endif
$Index = $Index + 1
? $Index
? $x
? $KeyName

until @error <> 0

the ? are for me to see what is happening.
DRPDRD-KA01 is name of printserver

The problem is that after running the script only half of the printers are removed. after running script again another 5 are removed. After 4 times in a row running the script all printers are removed
why?
what did i do wrong?
I want it to happen running the script just once

Top
#43509 - 2003-07-29 02:04 PM Re: removing printers
Kdyer Offline
KiX Supporter
*****

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

Welcome to the board!

Are you doing a migration from one print server to another? If so, you may want to have a look at -

Prevent special printers from being deleted

And references this UDF():
PriMapState() - Checks for existent printerconnection

If you are simply trying to delete the printers, maybe try adding a SLEEP after Delprinterconnection ..

HTH,

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

Top
#43510 - 2003-07-29 02:18 PM Re: removing printers
Jonas1 Offline
Lurker

Registered: 2003-07-29
Posts: 4
Loc: Dordrecht
I already tried to use the sleep, did not help
Yes it is a migration to another printserver.
Shall read the links you gave me

thanks for replay

Jonas

Top
#43511 - 2003-07-29 02:24 PM Re: removing printers
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
The problem is that you are increasing the index. However, once you've removed a printer, the other printers indices will move up to fill the "empty" space.

The better approach is to use the ArrayEnumKey() UDF from the UDF Forum and enumerate all printers. Then in a second, you'd loop through the listed printers and remove them
code:
;pseudo-code
$printers=arrayenumkey('HKEY_CURRENT_USER\Printers\Connections')
for each $printer in $printers
$print=split($printer,',')
$rc=delprinterconnection($print[4])
next



[ 29. July 2003, 14:28: Message edited by: sealeopard ]
_________________________
There are two types of vessels, submarines and targets.

Top
#43512 - 2003-07-29 03:51 PM Re: removing printers
Jonas1 Offline
Lurker

Registered: 2003-07-29
Posts: 4
Loc: Dordrecht
That makes sense, will try this and thanks for the advise

Jonas

Top
#43513 - 2003-07-31 12:03 AM Re: removing printers
Jonas1 Offline
Lurker

Registered: 2003-07-29
Posts: 4
Loc: Dordrecht
Thank for the advice, i made another script which worked fine.

$Index = 0

dim $array [40]

do
$KeyName = ENUMKEY("HKEY_CURRENT_USER\Printers\Connections", $Index)
$array [$Index] = $KeyName
$Index = $Index + 1
? $Index


until @error <> 0

$z = 0
do
$x=left($array [$z],13)
RedirectOutput("c:\432.txt")
? $x
if $x=",,drpdrd-ka01"
Delprinterconnection ($array [$z])
endif
$z = $z +1
until $z = 40

the redirect and ? commands are there for me to check what is happening and can be removed

the array declaration of 40 must be changed but that is spending some more time on the script. the bottemline is that it works. refining the code is next.
Problem solved.
I hope that other readers got wiser by this example of registry behavior which caused my original problem

greetings Jonas

Top
#43514 - 2003-07-30 01:16 PM Re: removing printers
MightyR1 Offline
MM club member
*****

Registered: 1999-09-09
Posts: 1264
Loc: The Netherlands
Nicer code:

code:
Break on

Dim $printers, $printer, $rc

$printers=arrayenumkey('HKEY_CURRENT_USER\Printers\Connections')

For Each $printer In $printers
If InStr($printer,'DRPDRD-KA01')
$printer=Split($printer,',')
$printer='\\'+$printer[2]+'\'+$printer[3]

? '-I- Connection found: '+$printer

$rc=DelPrinterConnection($printer)
If @error
? '-E- Error deleting connection to '+$printer+' (EC='+@error+')'
Else
? '-I- Succesfully deleted connection to '+$printer
EndIf
EndIf
Next

Exit



;~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
;Functions!!!

;NAME ArrayEnumKey
;
;ACTION Creates an array of names of the subkeys contained in a registry key or subkey
;
;AUTHOR Jens Meyer (sealeopard@usa.net)
;
;VERSION 1.2 (added error codes)
; 1.1
;
;DATE CREATED 2001/12/05
;
;DATE MODIFIED 2003/05/17
;
;KIXTART 4.12+
;
;SYNTAX ARRAYENUMKEY($subkey)
;
;PARAMETERS SUBKEY
; Required string containing the key or subkey for which the subkeys will be enumerated
;
;RETURNS Array containing the subkeys
;
;REMARKS none
;
;DEPENDENCIES none
;
;EXAMPLE $retcode=arrayenumkey('HKEY_USERS')
;
;KIXTART BBS http://www.kixtart.org/cgi-bin/ultimatebb.cgi?ubb=get_topic&f=12&t=000064
;
Function arrayenumkey($regsubkey)
Dim $retcode, $subkeycounter, $currentsubkey, $subkeyarray

If NOT KeyExist($regsubkey)
Exit 87
EndIf

$subkeycounter=0
Do
$currentsubkey=EnumKey($regsubkey,$subkeycounter)
If NOT @ERROR
ReDim preserve $subkeyarray[$subkeycounter]
$subkeyarray[$subkeycounter]=$currentsubkey
$subkeycounter=$subkeycounter+1
EndIf
Until @ERROR

$arrayenumkey=$subkeyarray
Exit 0
EndFunction

_________________________
Greetz,
Patrick Rutten

- We'll either find a way or make one...
- Knowledge is power; knowing how to find it is more powerful...
- Problems don't exist; they are challenges...

Top
#43515 - 2003-07-30 02:13 PM Re: removing printers
Kdyer Offline
KiX Supporter
*****

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

quote:

I hope that other readers got wiser by this example of registry behavior which caused my original problem

I think the "key element" is the ARRAYENUMKEY() UDF for this issue. That is, deleting all 21 printers installed.

But, then again if you think about your initial response to me.. The Printer Migration script is even more key.

So.. That being said. Are you trying to delete all printers installed? Or, are you trying to migrate printers from one print server to another?

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

Top
#43516 - 2003-07-30 02:33 PM Re: removing printers
Sealeopard Offline
KiX Master
*****

Registered: 2001-04-25
Posts: 11165
Loc: Boston, MA, USA
Actually, the registry behaved correctly. The problem was the incorrect increment of the index, thus it was a programming (logic) error.
_________________________
There are two types of vessels, submarines and targets.

Top
Page 1 of 1 1


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

Who's Online
0 registered and 2175 anonymous users online.
Newest Members
fgarcia, Viginette, ManuvdWielNL, Sir_Barrington, batdk82
17889 Registered Users

Generated in 0.161 seconds in which 0.088 seconds were spent on a total of 12 queries. Zlib compression enabled.

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