Norman W. Freeman · Oct 1, 2020

Is there a built-in method to escape special characters in a string, in a similar way to what is done by Portal with global data ?

If a global node contains special characters, (eg : a line returns), it will be displayed like this in Portal ("System > Globals > View Global Data" panel) :

^A(1) = "this is"_$c(13,10)_"a test"

I would like to export global data to a txt file using a similar format.

I already wrote the main code (that loops on all nodes and dump them to file), the problem is how to handle special characters.
For the moment I replace them manually one by one. It works, but it's far from perfect :

set text = ... //get global data
set text = $replace(text,"""","""""") //double the quotes
set text = $replace(text,$c(13),"""_$c(13)_""") 
set text = $replace(text,$c(10),"""_$c(10)_""")

Is there a built-in function in cache that handle special characters in a string by splitting it into smaller strings (with concatenation in between), something similar what Portal does ? 
Or even better : a built-in function that can export a global to a txt file directly, in a format similar to Portal.

0 5 86 4


Hi Norman,

Not sure if this helps, but write commands generally interpret $char whereas zwrite won't. I'm not sure what you're using to output to a file but perhaps you could try using zwrite.

I'm not sure what the portal uses, maybe somebody else has a cleaner solution.

Further elaborating on the suggestion of @Vic Sun 

set file="c:\whateverfilename.txt"   ;according to file system and access rights
if $data(^%SYS)    ; or whatever global
open file:("WNS")
use file zwrite @$ZR  ; send all output to file
close file

you can even verify it from terminal command line and the result looks like this:

^%SYS("tercap","vt320","bin","init")=$c(18,155)_"!p"_$c(155)_"62""p"_$c(27)_"F"_$c(27)_")0"_$c(155)_"?7h"                                                      ^%SYS("tercap","vt320","bin","poff")=$c(3,155)_"4i"

Wonderful. I works like a charm. Thank you. 


Just to pad out that example:

set gbl="^%SYS",x="",file="c:\temp\myfile.txt"

open file:("WNS"):0

else  w !,"Unable to open file" quit

for  set x=$o(@gbl@(x)) q:x=""  zw @$ZR  w !

close file

for multiple globals:

set file="c:\temp\text.txt"

open file:"("WNS"):0

for gbl="^ABC,^XYZ,^PQTY,^%Nigel"  set x="" for  set x=$o(@gbl@(x)) w !!:x=""  q:x=""  use file zw @$ZR

close file


Hi Norman,
Quote() method of %Global might be what you are lookig for:

 USER>write ##class(%Global).Quote($lb("abc", """", $c(13, 10)))