Kat
(Fresh Scripter)
2005-07-05 10:32 AM
delete all printers before connecting new ones

Hi,
I am new to Kix and as I am connecting printers I figured that if I got a mobiles user taking his laptop to different branches will end up with heaps of printer connections. So i was wondering if there is a way to delete all existing printer before connecting the one they need for the site?
I already searched arround and coulnd't fint anything - sorry if this was already covered.
Thanks for your help.
Kat


LonkeroAdministrator
(KiX Master Guru)
2005-07-05 10:52 AM
Re: delete all printers before connecting new ones

try:
delprinterconnection("")


LonkeroAdministrator
(KiX Master Guru)
2005-07-05 10:53 AM
Re: delete all printers before connecting new ones

and this is in the manual.
on your search trip, I suggest you always include the manual to it, k?


Kat
(Fresh Scripter)
2005-07-05 11:05 AM
Re: delete all printers before connecting new ones

Tried it and it is not working get an error code 30

Kat
(Fresh Scripter)
2005-07-05 11:06 AM
Re: delete all printers before connecting new ones

besides I checked the manual ...

NTDOCAdministrator
(KiX Master)
2005-07-05 11:07 AM
Re: delete all printers before connecting new ones

30 says:

The system cannot read from the specified device.
 


Kat
(Fresh Scripter)
2005-07-05 11:13 AM
Re: delete all printers before connecting new ones

yep, saw that. Is there a way to use a wild card?
I tried
deletePrinterConnection ("*")
deletePrinterConnection (*)
deletePrinterConnection ("\\*")
but nothing worked.
any ideas?


Kat
(Fresh Scripter)
2005-07-05 11:26 AM
Re: delete all printers before connecting new ones

Ok it seems like I really have to read more carefully the manual as it it must be 'DelPrinterConnection' and not 'DeletePrinterConnection'.
Still now I am getting 1801 error which means that the printer cannot be found.
@ Lonkero: Sorry, if I sounded a bit harsh I am just trying now for 3 days to get this going.
Kat


LonkeroAdministrator
(KiX Master Guru)
2005-07-05 11:43 AM
Re: delete all printers before connecting new ones

kat, sure.
what more you try without success, that more tense you become and more mistakes you make, like with the name here


Kat
(Fresh Scripter)
2005-07-05 11:51 AM
Re: delete all printers before connecting new ones

Thx,
this all gets worse as I am not very good in scripting anyway.
When I started I thought map some printers no problem and then I had to work out all kinds of stuff like how to determine in which subnet the user is ... Got that all running fine just cannot find I way to delete all printers first
At least I am getting now 'cannot find printer' which is better than the one error before.


Richard H.Administrator
(KiX Supporter)
2005-07-05 11:56 AM
Re: delete all printers before connecting new ones

Which version of KiXtart are you using?

Kat
(Fresh Scripter)
2005-07-05 11:59 AM
Re: delete all printers before connecting new ones

good question,

I just checked to make sure it's version 4.23.0.0


LonkeroAdministrator
(KiX Master Guru)
2005-07-05 01:22 PM
Re: delete all printers before connecting new ones

next question is, what version of kixtart you are using?

LonkeroAdministrator
(KiX Master Guru)
2005-07-05 01:25 PM
Re: delete all printers before connecting new ones

richie was faster.
and is there network printers available for it to demap?


Kat
(Fresh Scripter)
2005-07-05 01:36 PM
Re: delete all printers before connecting new ones

yep, there are.

LonkeroAdministrator
(KiX Master Guru)
2005-07-05 01:55 PM
Re: delete all printers before connecting new ones

k, add this code at the end of your script:
Code:

function DelPrinterConnections()
dim $c,$bk,$conn
$c=0
$bk="HKEY_CURRENT_USER\Printers\Connections"
$conn=enumkey($bk,$c)
while @error=0
$c=$c+1
$conn=delkey($bk+"\"+$conn)
$conn=enumkey($bk,$c)
loop
endfunction



and, where you did call the delprinterconnection(""), replace it with DelPrinterConnections()
let us know did that work, k?


Kat
(Fresh Scripter)
2005-07-05 02:45 PM
Re: delete all printers before connecting new ones

works so far fine just it deletes only 2 of the current 4 network printers. But I think I go ahead tomorrow as it is quite late here (10.30pm) and I have to be back to work at 8.30am.
Thx for the help - be back tomorrow and tell you how go.
Kat


LonkeroAdministrator
(KiX Master Guru)
2005-07-05 02:48 PM
Re: delete all printers before connecting new ones

2 was left?
so, I would concentrate on those two.
most likely they are the cause why kixtart's delprinterconnection is failing.


Kat
(Fresh Scripter)
2005-07-06 01:37 AM
Re: delete all printers before connecting new ones

Hi Lonkero,

got it going! All I needed to change on your function was the counter. It seems like the key numbering are going in .5 steps and not in 1. Therfore every second printer was skiped. I changed your counter $c=$c+0.5 and it works perfectly!

Thanks heaps for that! now I can start writing the rest.

Kat


Les
(KiX Master)
2005-07-06 04:38 AM
Re: delete all printers before connecting new ones

LOL
Jooel won't admit there is anything wrong with his UDF. It deletes evey second printer because whenever a printer is deleted, it skews the index.


LonkeroAdministrator
(KiX Master Guru)
2005-07-06 06:06 AM
Re: delete all printers before connecting new ones

wow.
and this was not fixed 2 years ago when the UDF was really used?

kat, good to hear it works.
still, it's an issue of kix and it makes me worry.


Kat
(Fresh Scripter)
2005-07-06 08:02 AM
Re: delete all printers before connecting new ones

All what counts to me is that it is working now. and that you guys are able to help.
I still have so many other thinks to solve that everything which works is good!

Kat


Richard H.Administrator
(KiX Supporter)
2005-07-06 09:20 AM
Re: delete all printers before connecting new ones


got it going! All I needed to change on your function was the counter. It seems like the key numbering are going in .5 steps and not in 1. Therfore every second printer was skiped. I changed your counter $c=$c+0.5 and it works perfectly!


Because $c is an integer the "0.5" is cast to a value of "0", so your statment becomes $c = $c + 0 - don't worry too much though, because this is what you want.

Jooel has fixed the UDF in the UDF forum, I recommend that you get his updated version.


Kat
(Fresh Scripter)
2005-07-06 09:50 AM
Re: delete all printers before connecting new ones

Good idea, done that and it works,
thanks all for your help - I am sure I'll be back!
Kat


Sealeopard
(KiX Master)
2005-07-09 10:17 PM
Re: delete all printers before connecting new ones

It has to do with increasing the index in ENUMKEY if the current key is being deleted. That results in the next key moving up one index, thus becoming the current key still under the current index. This is basic ENUM functionality!

LonkeroAdministrator
(KiX Master Guru)
2005-07-09 11:11 PM
Re: delete all printers before connecting new ones

that is, but if the udf has worked before, this enum functionality has changed at some point.

saamwan
(Lurker)
2006-08-25 05:06 AM
Re: delete all printers before connecting new ones

This worked really well except that it wont delete printers with names over 13 characters can you suggest how to modify the script for these

Greg_Wilson
(Fresh Scripter)
2006-08-25 08:27 AM
Re: delete all printers before connecting new ones

I've just tried this code in a MS 2003 terminal server environment and am having exactly the same intermittent result as delprinterconnection("").

The script works reliably when called from a mapped drive in a TS session but doesn't remove printers when run as a login script.

Any tips? I'm mystified.

Thanks,

Greg.


Richard H.Administrator
(KiX Supporter)
2006-08-25 09:07 AM
Re: delete all printers before connecting new ones

Are you certain the login script is running?

Are you running login scripts synchronously?

Try adding debug code to ensure that the printers are being enumerated.


LonkeroAdministrator
(KiX Master Guru)
2006-08-25 05:07 PM
Re: delete all printers before connecting new ones

and try posting to your own thread...

bertus02
(Just in Town)
2010-02-04 05:53 PM
Re: delete all printers before connecting new ones

Hello all, I am in the middle of a migration and can't seem to get the scripting to work to delete all network printers. this is what I have any help would be greatlly appreciated....

IF INGROUP("Domain Users")

function DelPrinterConnections()
dim $c,$bk,$conn
$c=0
$bk="HKEY_CURRENT_USER\Printers\Connections"
$conn=enumkey($bk,$c)
while @error=0
$c=$c+0.5
$conn=delkey($bk+"\"+$conn)
$conn=enumkey($bk,$c)
loop
endfunction

ENDIF



Thanks again


Glenn BarnasAdministrator
(KiX Supporter)
2010-02-04 06:09 PM
Re: delete all printers before connecting new ones

Welcome to KORG!

This won't work as written.

You are enumerating a list of objects (registry keys representing printers). The enumeration pointer ($C) is incremented each time. When you delete a key, they all shift down a position, so this method never points to all values.

To illustrate this, put 5 coins on your desk, lined up in a row. Point to the first coin with a pen - this represents the enumeration pointer $C.

Since the pen is pointing to a coin, we remove (delete) it and move the pointer to the next position. Point to the second coin, remove the first coin, but then move the 4 remaining coins into the original location. Your pointer has now moved past the first position, where the second value now exists.

The solution is to enumerate all of the keys into an array, and then loop throuth the array to delete the values.
 Code:
Function DelPrinters()

  Dim $aPrinters[0]	; array of connections to delete
  Dim $P, $A		; pointers
  Dim $Key		; reg key
  Dim $Tmp		; tmp var
  Dim $Rv		; Return Value

  $A = -1
  $P = 0

  $Key = 'HKCU\Printers\Connections'
  $Tmp = EnumKey($Key, $P)
  If $Tmp			; at least one printer?
    While Not @ERROR 
      $A = $A + 1
      ReDim Preserve $aPrinters[$A]
      $aPrinters[$A] = $Tmp
      $P = $P + 1
      $Tmp = EnumKey($Key, $P)
    Loop

    For Each $P in $aPrinters
      'delete ' $Key '\' $P ?
      ;$Rv = DelKey($Key + '\' + $P)
    Next

  EndIf

EndFunction
Note that as shown above, it displays what it will delete without deleting. After confirming it does what you want, comment the message line and uncomment the DelKey line.

Glenn


bertus02
(Just in Town)
2010-02-04 06:53 PM
Re: delete all printers before connecting new ones

wow! i appreciate the prompt respose. you guys rule. however the script did not work and the reg keys remain.. here is what i have \:\(

IF INGROUP("Domain Users")

Function DelPrinters()

Dim $aPrinters[0] ; array of connections to delete
Dim $P, $A ; pointers
Dim $Key ; reg key
Dim $Tmp ; tmp var
Dim $Rv ; Return Value

$A = -1
$P = 0

$Key = 'HKCU\Printers\Connections'
$Tmp = EnumKey($Key, $P)
If $Tmp
While Not @ERROR
$A = $A + 1
ReDim Preserve $aPrinters[$A]
$aPrinters[$A] = $Tmp
$P = $P + 1
$Tmp = EnumKey($Key, $P)
Loop

For Each $P in $aPrinters
'delete ' $Key '\' $P ?
$Rv = DelKey($Key + '\' + $P)
Next

EndIf

EndFunction

ENDIF


Glenn BarnasAdministrator
(KiX Supporter)
2010-02-04 07:03 PM
Re: delete all printers before connecting new ones

You aren't calling the function!
 Code:
If InGroup('this group')
  DelPrinters() ; call the function
EndIf

Function DelPrinters()  ; define the function
  ; code goes here...
EndFunction
You DEFINE the function at the beginning or end of your main code, then you CALL the functions in your main code, as I've illustrated above.

A common mistake. \:\)

Glenn


bertus02
(Just in Town)
2010-02-04 07:17 PM
Re: delete all printers before connecting new ones

wow! you trully are a lifesaver!

Glenn BarnasAdministrator
(KiX Supporter)
2010-02-04 07:30 PM
Re: delete all printers before connecting new ones

 Originally Posted By: bertus02
wow! you trully are a lifesaver!
Yup - Butter-Rum if I had a choice! \:\)

Glenn


LeRoy
(Just in Town)
2013-01-15 09:15 PM
Re: delete all printers before connecting new ones

I see that the script deletes the registry keys, thus deleting the printers from the printer list.

How does this compare to the delPrinterConnection ("\\Server\Printer") command instead?
Are they both doing the same thing?
How would you go about delete Local TCP/IP Port Mapped printers?