Josh R
(Fresh Scripter)
2003-07-14 02:32 PM
Replacing text

I am exporting mapped printers by using the following code:

$Index = 0
:Loop2
$KeyName = EnumKey("HKEY_CURRENT_USER\Printers\Connections\", $Index)
If @ERROR = 0
?"$KeyName"
$Index = $Index + 1
GoTo Loop2
EndIf

When I export it there are commas in the text which makes it space out in the file. How can I replace these commas with a "\"?


Howard Bullock
(KiX Supporter)
2003-07-14 02:38 PM
Re: Replacing text

Try:
$line = join(split($line,","),"\")

More info (for fun): http://www.kixtart.org/board/ultimatebb.php?ubb=get_topic;f=14;t=000882


Sealeopard
(KiX Master)
2003-07-14 02:47 PM
Re: Replacing text

And take a look at the UDF Library, you will find some helpful UDF in there with regards to enumerating registry keys/values and printer mapping/inventory.

Radimus
(KiX Supporter)
2003-07-14 02:49 PM
Re: Replacing text

$KeyName = EnumKey("HKEY_CURRENT_USER\Printers\Connections\", $Index)
$KeyName = Replace($KeyName,',','\')

code:
Function Replace($Str,$From,$To)
$Replace = Join(Split($Str,$From),$To)
ENDFUNCTION



[ 14. July 2003, 14:51: Message edited by: Radimus ]


Josh R
(Fresh Scripter)
2003-07-14 02:59 PM
Re: Replacing text

I am not sure if I explained myself that well. When the data shows up in the csv document (I am using excel to view it) it is showing up in the third column because of the double commas. Does that make more sense?

LonkeroAdministrator
(KiX Master Guru)
2003-07-14 03:03 PM
Re: Replacing text

you can put the value in quotes so it will be kept in the field even though it contains commas.

Howard Bullock
(KiX Supporter)
2003-07-14 03:04 PM
Re: Replacing text

You did not say you were writing a comma delimited text file.

You will either need to write a TAB delimited text or wrap the text that contains commas in quotes.

[ 14. July 2003, 15:05: Message edited by: Howard Bullock ]


Josh R
(Fresh Scripter)
2003-07-14 03:06 PM
Re: Replacing text

I think I already do have the text in quotes?

Sealeopard
(KiX Master)
2003-07-14 03:07 PM
Re: Replacing text

You have still been provided the solution to your problem, all the code fragments posted above will replace commas in a string with the back-slash character.

Please explain in detail why this does not solve your originally posted problem
quote:
How can I replace these commas with a "\"?


Josh R
(Fresh Scripter)
2003-07-14 03:09 PM
Re: Replacing text

When the registry key is read the CSV file is reading the commas and moving the data to a new column. That is my issue.

Sealeopard
(KiX Master)
2003-07-14 03:10 PM
Re: Replacing text

If you try to include a comma as part of a text string inside a comma-delimited file, then thet complete text string must be enclosed in quotes
code:
text,text,"text with , comma",text



Josh R
(Fresh Scripter)
2003-07-14 03:11 PM
Re: Replacing text

When the registry key is read the CSV file is reading the commas and moving the data to a new column. That is my issue.

Sealeopard
(KiX Master)
2003-07-14 03:16 PM
Re: Replacing text

Again, the code fragments above will replace the commas with back-slashes, as requested by you. Thus, having unwanted commas in your CSV-file is no longer an issue.

LonkeroAdministrator
(KiX Master Guru)
2003-07-14 03:17 PM
Re: Replacing text

yep.
like many of us quessed.
you need to write also the quotes around the data if it contains commas.

just like told.


Josh R
(Fresh Scripter)
2003-07-14 03:22 PM
Re: Replacing text

Ok there are 5 - 6 totally differnt solutions. Which ONE actually works? I have tried all of them and nothing works right.

LonkeroAdministrator
(KiX Master Guru)
2003-07-14 03:29 PM
Re: Replacing text

there is 2 solutions provided to you.
the first ones are what you asked, to replace comma's with back slashes.

then, we decided to go further and told you that you could just insert the quotes in the file around the data so the comma's don't matter anymore.

now, if you have tried these and 4 other ones...
dude, I must ask you to show your code.


Sealeopard
(KiX Master)
2003-07-14 03:31 PM
Re: Replacing text

"Code on a silver platter"TM
code:
$Index = 0
:Loop2
$KeyName = EnumKey("HKEY_CURRENT_USER\Printers\Connections\", $Index)
$KeyName = join(split($KeyName ,","),"\")
If @ERROR = 0
? "$KeyName"
$Index = $Index + 1
GoTo Loop2
EndIf

Oh, and please refrain form using GOTOs in your code, see for example the ArayEnumKEY() UDF.

[ 14. July 2003, 15:32: Message edited by: sealeopard ]


Sealeopard
(KiX Master)
2003-07-14 04:38 PM
Re: Replacing text

See also Deleting commas..... .