Escape Char not working when using , and |

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
glenfs
Posts: 6
Joined: 03 Jul 2012 01:28

Escape Char not working when using , and |

#1 Post by glenfs » 03 Jul 2012 05:49

Hi people, I have the following code, But the conversion of , to | does not happen when i use the following code. It does not woek even when i use set str=%str:^,=^|%

Can anyone help as to what i am missing?

set file=GDS.txt
for /f "tokens=*" %%- in (%file%) do (
set str=%%-&&call :NEXT

)
echo.%str%>>gds2.txt
goto :eof

:NEXT
set str=%str:,=|%
echo.%str%>>gds2.txt

jeb
Expert
Posts: 1062
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Escape Char not working when using , and |

#2 Post by jeb » 03 Jul 2012 06:10

Hi gelnfs,

it works!

Code: Select all

set str=%str:,=|%

This replaces each comma with a pipe, the problem is that the pipe character is special character that will be used to create a pipe.

If a sample string is

Code: Select all

one,echo two

Then the replacement line looks like

Code: Select all

set str=one|echo two

This sets the variable "str" to "one", but as there is a pipe this will be in a new cmd.exe context, so it will not take any effect.
And alse the right side of the pipe will be executed and should display "two".

To avoid this you could quote the set statement or use delayed expansion

Code: Select all

set "str=%str:,=|%"
OR
set str=!str:,=^|!


jeb

glenfs
Posts: 6
Joined: 03 Jul 2012 01:28

Re: Escape Char not working when using , and |

#3 Post by glenfs » 03 Jul 2012 06:33

Hi Jeb, Thank you for the response, I used the quotes like:
set "str=%str:,=|%"
And also tried the second method u mentioned,
set str=!str:,=^|!

But it is still not working, Here is the part of the sample code i wrote:

:NEXT
set "str=%str:,=|%"
echo.%str%>>gds32.txt
goto :eof

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Escape Char not working when using , and |

#4 Post by Ed Dyreen » 03 Jul 2012 06:58

'
You just piped the echo part into the part after the pipe and, it throws you an error.
glenfs wrote:set "str=%str:,=|%"
echo.%str%>>gds32.txt

Code: Select all

Microsoft Windows XP [versie 5.1.2600]
(C) Copyright 1985-2001 Microsoft Corp.

>set "$=hello,there"

>set "$=%$:,=|%"

>set "$"
$=hello|there

>set "$=%$:|=^|%"

>set "$"
$=hello^|there

>>ou.TXT echo.%$%

>type ou.TXT
hello|there

>
As jeb explains very well, the pipe is a special character, it has to be escaped :wink:

jeb
Expert
Posts: 1062
Joined: 30 Aug 2007 08:05
Location: Germany, Bochum

Re: Escape Char not working when using , and |

#5 Post by jeb » 03 Jul 2012 07:23

Now your problem is the ECHO line

You could change it to delayed expansion, then it works

Code: Select all

set "str=%str:,=|%"
echo(!str!>>gds32.


And better avoid echo., as only ECHO( is content safe. :D

jeb

glenfs
Posts: 6
Joined: 03 Jul 2012 01:28

Re: Escape Char not working when using , and |

#6 Post by glenfs » 03 Jul 2012 07:32

Ed and Jeb, Thank you very much for the clarification and the solution you guys provided. It is working fine now. :)
Really Appreciate it.

Post Reply