Parag Doke | 1 Dec 2010 16:44
Picon

How to escape special characters?

Hello All.
I'm stuck at one point while trying to write a script. The problem was seen as a part of a larger script ... but
I'll post the relevant part here.

=====temp.txt=====
a< b> c| d^ e% f&
=====temp.txt=====

=====temp.bat=====
set /p "var=" < "temp.txt"

echo "%var%"

set "new=%var:<=<#%"
set "new=%new:>=>#%"
set "new=%new:|=|#%"
set "new=%new:&=&#%"
set "new=%new:^%=^%%#%"
rem call set "new=%%new:^=^#%%"

echo "%new%"
=====temp.bat=====
I am able to see the escaped characters < > | and & ... and I see the hash sign added after each of them. But
somehow not able to figure out how to escape the % sign. And yet to attempt the ^ caret.

Can someone kindly share the trick ?
Thanks in advance,
Parag Doke

------------------------------------
(Continue reading)

foxidrive | 2 Dec 2010 00:44

Re: How to escape special characters?

On 2/12/2010 02:44, Parag Doke wrote:
> Hello All.
> I'm stuck at one point while trying to write a script. The problem was seen as a part of a larger script ... but
I'll post the relevant part here.
>
> =====temp.txt=====
> a<  b>  c| d^ e% f&
> =====temp.txt=====
>
> =====temp.bat=====
> set /p "var="<  "temp.txt"
>
> echo "%var%"
>
> set "new=%var:<=<#%"
> set "new=%new:>=>#%"
> set "new=%new:|=|#%"
> set "new=%new:&=&#%"
> set "new=%new:^%=^%%#%"
> rem call set "new=%%new:^=^#%%"
>
> echo "%new%"
> =====temp.bat=====
> I am able to see the escaped characters<  >  | and&  ... and I see the hash sign added after each of them. But
somehow not able to figure out how to escape the % sign. And yet to attempt the ^ caret.
>
> Can someone kindly share the trick ?

Caret is treated the same way as the rest.  Percent is an ugly character 
to manipulate in batch - can you describe the circumstance in which you 
(Continue reading)

Parag Doke | 2 Dec 2010 07:01
Picon

Re: How to escape special characters?

Hello Mic.
Thanks for the input regarding the caret.

Here is the scenario:
I'm using cleartool to list the comment of a file version (this comment
would be entered by whoever checked in the file version ... so it is likely
to have any set of characters ... we'll assume ASCII for now). So I capture
the comment to a text file using a command similar to:
cleartool desc -fmt "%%c" "<file version>" > SourceComment.txt
The cleartool syntax for the comment is to use %c, but since I do this
within a batch file, I need to use double % signs.

For each line in the comment, I need to substitute the name of the person
who created that file version with a blank string (since this would be on my
own branch). For this I need to iterate over each line, substitute the name
with a blank string and then append it to another output file (which will be
used at a later point).

Sample SourceComment.txt
{person_name} This is the user entered comment.
It can have multiple lines &
special characters such as ^ < > | % & too. (script_name)

Sample OutputFile.txt
This is the user entered comment.
It can have multiple lines &
special characters such as ^ < > | % & too.

The person_name will be on the 1st line. Also the script_name string will be
the last set of characters. What I am trying to state is that substitution
(Continue reading)

foxidrive | 2 Dec 2010 07:29

Re: How to escape special characters?

On 2/12/2010 17:01, Parag Doke wrote:
> Hello Mic.
> Thanks for the input regarding the caret.
>
> Here is the scenario:
> I'm using cleartool to list the comment of a file version (this comment
> would be entered by whoever checked in the file version ... so it is likely
> to have any set of characters ... we'll assume ASCII for now). So I capture
> the comment to a text file using a command similar to:
> cleartool desc -fmt "%%c" "<file version>">  SourceComment.txt
> The cleartool syntax for the comment is to use %c, but since I do this
> within a batch file, I need to use double % signs.
>
> For each line in the comment, I need to substitute the name of the person
> who created that file version with a blank string (since this would be on my
> own branch). For this I need to iterate over each line, substitute the name
> with a blank string and then append it to another output file (which will be
> used at a later point).
>
> Sample SourceComment.txt
> {person_name} This is the user entered comment.
> It can have multiple lines&
> special characters such as ^<  >  | %&  too. (script_name)
>
> Sample OutputFile.txt
> This is the user entered comment.
> It can have multiple lines&
> special characters such as ^<  >  | %&  too.
>
> The person_name will be on the 1st line. Also the script_name string will be
(Continue reading)

Parag Doke | 2 Dec 2010 08:08
Picon

Re: How to escape special characters?

Okay. I will go for it then. Thanks for leading me there.
Have a nice day ahead !

Regards,
Parag Doke

PS: I went for JScript to use the try catch ... but I'm a noob there too
:-). Will make it quit with non zero errorcodes in case of IO exceptions at
some later date. For now, this is what will work for me.

WScript.echo("Starting "+WScript.ScriptName);
var args=WScript.Arguments;
if (args.length < 3) {
    WScript.echo("Usage: "+WScript.ScriptName+" FilePath
FindString/FindRegex NewString [NewFilePath]");
    WScript.Quit(1)
}
var fso=WScript.CreateObject("Scripting.FileSystemObject");
var forReading=1;
var forWriting=2;
if (fso.GetFile(args.Item(0)).Size > 0) {
    var text=fso.OpenTextFile(args.Item(0),forReading).ReadAll();
    text=text.replace(args.Item(1),args.Item(2));
    if (args.Item(3).length > 0) {
        fso.OpenTextFile(args.Item(3),forWriting,true).Write(text);
    } else {
        WScript.echo(text);
    }
}
WScript.echo("Finished "+WScript.ScriptName);
(Continue reading)


Gmane