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
Escape Char not working when using , and |
Moderator: DosItHelp
Re: Escape Char not working when using , and |
Hi gelnfs,
it works!
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
Then the replacement line looks like
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
jeb
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
Re: Escape Char not working when using , and |
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
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
Re: Escape Char not working when using , and |
'
You just piped the echo part into the part after the pipe and, it throws you an error.
As jeb explains very well, the pipe is a special character, it has to be escaped 
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
>

Re: Escape Char not working when using , and |
Now your problem is the ECHO line
You could change it to delayed expansion, then it works
And better avoid echo., as only ECHO( is content safe.
jeb
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.

jeb
Re: Escape Char not working when using , and |
Ed and Jeb, Thank you very much for the clarification and the solution you guys provided. It is working fine now.
Really Appreciate it.

Really Appreciate it.