Page 1 of 1 1
Topic Options
#198082 - 2010-03-17 02:56 AM Easy help with printer kix i found
metatron Offline
Fresh Scripter

Registered: 2008-05-06
Posts: 27
Loc: london
hi

can anyone tell me how i can get this script to map a printer if it has a space in between the words that make up its name?

here is the code for the script

 Code:
Select
Case InGroup( 'Logon A' )
  $sPrinterList='\\apple\BW \\apple\BW colour'
 Case 1						; catch all, clear printerlist
  $sPrinterList = ''
EndSelect

If $sPrinterList				; if sPrinterList is defined, perform this logic
  For Each $sPrinter in Split($sPrinterList)
    'Checking printer ' + $sPrinter + @CRLF
    If PriMapState($sPrinter)
      '  Already connected' + @CRLF
    Else
      '  Connecting...' + @CRLF
      If AddPrinterConnection($sPrinter)
         '   **Error connecting: [' + @ERROR + '] ' + @SERROR + @CRLF
      Else
         '   Success!' + @CRLF
      EndIf
    EndIf
  Next
EndIf

Exit 0


very easy to many, a long time and sore eyes for me \:\( thanks


Edited by Richard H. (2010-03-18 02:26 PM)
Edit Reason: Added code tags

Top
#198084 - 2010-03-17 06:21 AM Re: Easy help with printer kix i found [Re: metatron]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4567
Loc: USA
I'd say just make the array and forget the split.

 Code:
$sPrinterList='\\apple\BW','\\apple\BW colour'
..
..
..
For Each $sPrinter in $sPrinterList


Also, Please see the FAQs - http://www.kixtart.org/forums/ubbthreads.php?ubb=postlist&Board=5&page=1

Specifically, HOWTO: Submit code to / copy code from the board
http://www.kixtart.org/forums/ubbthreads.php?ubb=showflat&Number=81607&site_id=1#import

Top
#198126 - 2010-03-22 02:55 PM Re: Easy help with printer kix i found [Re: Allen]
metatron Offline
Fresh Scripter

Registered: 2008-05-06
Posts: 27
Loc: london
so all i needed was a comma? damnit thanks for your help allen and sorry about the code paste \:\)
Top
#198127 - 2010-03-22 04:02 PM Re: Easy help with printer kix i found [Re: metatron]
Arend_ Moderator Offline
MM club member
*****

Registered: 2005-01-17
Posts: 1896
Loc: Hilversum, The Netherlands
And if you really want to split it into a proper array, tell split what it should split.
 Code:
$string1 = "printer1","printer2"
$string2 = "printer1" "printer2"
$array1 = Split($string1,",")
$array2 = Split($string2," ")

Top
#198128 - 2010-03-22 07:03 PM Re: Easy help with printer kix i found [Re: Arend_]
Allen Administrator Online   shocked
KiX Supporter
*****

Registered: 2003-04-19
Posts: 4567
Loc: USA
Arend, you might want to check that code out... I don't think its going to work the way you have it. \:\)
Top
#198129 - 2010-03-22 07:30 PM Re: Easy help with printer kix i found [Re: Allen]
Glenn Barnas Administrator Offline
KiX Supporter
*****

Registered: 2003-01-28
Posts: 4402
Loc: New Jersey
LOL - had to look twice myself to catch that! Good eye, Allen! \:D

Glenn
_________________________
Actually I am a Rocket Scientist! \:D

Top
#198570 - 2010-05-10 11:56 AM Re: Easy help with printer kix i found [Re: Glenn Barnas]
metatron Offline
Fresh Scripter

Registered: 2008-05-06
Posts: 27
Loc: london
hi all. sorry for the bump on an old post. can anyone tell me how to convert the kix code i pasted above so it deletes printers instead of adding them?
Top
#198571 - 2010-05-10 02:14 PM Re: Easy help with printer kix i found [Re: metatron]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Above you are using the AddPrinterConnection function. There is also and DelPRinterConenction function that lets you delete the printer you specify.
What I did for our (still in BEAT testing) logon script is enumerate all network printers at the start of the script, list all needed printers during the script even if they are already mapped, compare both lists and delete everything that is in the "old" list and not in the "new" list.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#198760 - 2010-06-02 12:02 PM Re: Easy help with printer kix i found [Re: Mart]
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1342
Loc: NL
Do you have a peace of code Mart? After many years doing nothing with Kixtart I was asked today to change a printer script. The script uses the AddPrinterConnection and DelPRinterConenction functions. Users complain that there settings are missing when a new printer is added to a printer.ini file. It seams that all printers will be deleted and reconnected.
_________________________
Co


Top
#198761 - 2010-06-02 12:44 PM Re: Easy help with printer kix i found [Re: Co]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
Totally forgot about this post Sorry.

Standby........I'll post some code after lunch. Need to make it more generic first.
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#198763 - 2010-06-02 01:11 PM Re: Easy help with printer kix i found [Re: Mart]
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1342
Loc: NL
OK, I'll wait... ;\)
_________________________
Co


Top
#198764 - 2010-06-02 01:30 PM Re: Easy help with printer kix i found [Re: Co]
Mart Moderator Offline
KiX Supporter
*****

Registered: 2002-03-27
Posts: 4673
Loc: The Netherlands
The code below is DEMO code. It might be a bit dummy proof with variable names and stuff but I'm not the only one using this in our company and the others might be less skilled in kixtart so I made it easy to understand for everyone.

It requires some UDF's
ARRAYENUMKEY() - Creates an array of names of the subkeys contained in a registry key
PriMapState() - Checks for existent printerconnection
And the one below (not in UDF section).

 Code:
;Region Check printer logfile
;Set printer log file name
$printerlog = "C:\somefolder\Printers.ini"
;Check if the printer log file exists.
If Exist ($printerlog)
	;Delete old printer log file.
	Del $printerlog
	;Create printer log file.
	$rc = CreateFile($printerlog)
Else
	;Create printer log file.
	$rc = CreateFile($printerlog)
EndIf
;endregion

;Region Set variables
;Set name of the printerserver
$printerserver = "Server"
;Get all installed network printers.
;Set the key that should be enumerated.
$key = "HKCU\Printers\Connections"
;Enumerate network printers key.
$oldprinters = ArrayEnumKey($key)
;Do stuff for each value
For Each $value in $oldprinters
	;Split the value on the comma.
	$value = Split($value, ",")
	;Put the value back together so that is represents the correct path to the printer.
	$value1 = "\\"+ $value[2] + "\" + $value[3]
	;Add the created value to a variable to be writiine to the printer log file.
	$oldprinter = $oldprinter + "~" + $value1
Next
;Write all old printers to the printer log file.
$rc = WriteProfileString($printerlog, "Printers", "OldPrinters", $oldprinter)
$currentprinters = Split($oldprinter, "~")
;endregion

;Region Add printers
;Set printer name.
$printer = "\\" + $printerserver + "\Printer"
;Check group membership.
If InGroup("SomeName-Default") Or InGroup("SomeName-Extra")
	;Check if the printer is already installed.
	If Not PriMapState($printer)
		;Printer is not installed. Add printer.		
		$rc = AddPrinterConnection($printer)
	EndIf
	;Read all so far installed printers from the printer log file.
	$connected = ReadProfileString($printerlog, "Printers", "NewPrinters")
	;Add the printer name to a variable to be written to the printer log file.
	$connected = $connected + "~" + $printer
	;Write the new printers value to the printers log file.
	$rc = WriteProfileString($printerlog, "Printers","NewPrinters",$connected)
EndIf

;Region Set default printer
;Check group membership.
If InGroup("SomeName-Default")
	$printer = "\\" + $printerserver + "\Printer"
	;Check if the printer is already set as default.
	If PriMapState($printer) <> "2"
		;Set default printer.
		$rc = SetDefaultPrinter($printer)
	EndIf
EndIf

;Region Clean printers
;Read all old network printers from the printer log file.
$oldprinters = ReadProfileString($printerlog, "Printers", "OldPrinters")
;Check if there are any old printers in the list.
If $oldprinters <> ""
	;Split the old printers on the ~ character
	$oldprinters = Split($oldprinters, "~")
EndIf
;Check each element of the array.
For $i = 0 to UBound($oldprinters)
	;Remove extra ~ characters.
	$oldprinters[$i] = Join(Split($oldprinters[$i], "~"),"")
Next

;Read all new printers from the printer log file.
$newprinters = ReadProfileString($printerlog, "Printers", "NewPrinters")
;Check if there are any new printers in the list.
If $newprinters <> ""
	;Split the new printers on the ~ character
	$newprinters = Split($newprinters, "~")
EndIf
;Check each element of the array.
For $i = 0 to UBound($newprinters)
	;Remove extra ~ characters.
	$newprinters[$i] = Join(Split($newprinters[$i], "~"), "")
Next

;Compare the list of old and new printers.
$tobedeletedprinters = ArrayComp($newprinters, $oldprinters)
;Check if there are any printers that should be deleted.
If UBound($tobedeletedprinters) > -1
	For Each $tobedeletedprinter in $tobedeletedprinters
		;Delete each printer that should be deleted.
		$rc = DelPrinterConnection($tobedeletedprinter)
	Next
EndIf
;endregion


CreateFile() UDF:
 Code:
Function CreateFile($file)
	Dim $newFolder, $NewFile, $fso
	$fso = CreateObject("Scripting.FileSystemObject")
	$NewFile = $fso.CreateTextFile($file, True)
	$NewFile.Close
EndFunction
_________________________
Mart

- Chuck Norris once sold ebay to ebay on ebay.

Top
#198766 - 2010-06-02 01:37 PM Re: Easy help with printer kix i found [Re: Mart]
Co Offline
MM club member
***

Registered: 2000-11-20
Posts: 1342
Loc: NL
Thanks Mart! I will test it. I'll let you know if I it is usefull for me.
_________________________
Co


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
1 registered (Allen) and 363 anonymous users online.
Newest Members
Sir_Barrington, batdk82, StuTheCoder, M_Moore, BeeEm
17886 Registered Users

Generated in 0.062 seconds in which 0.024 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