Page 1 of 1

I need to escape pipe character in a string but still remove leading and trailing quotes

Posted: 04 Feb 2019 12:16
by bosekamineni
@echo on
set _string="Test String With command"
echo the string is %_string%
set _string=%_string:~1,-1%
echo the string is %_string%


set _string="Test String With |command"
set _string=%_string:~1,-1%
echo the string is %_string%

if I do not use escape then after executing the statement set _string=%_string:~1,-1% -
output is
'command' is not recognized as an internal or external command,
operable program or batch file.

if I use ^|

set _string="Test String With |command"
set _string=%_string:^|"=%
echo the string is %_string%

set _string="Test String With |command"
echo the string is "Test String With |command"
the string is "Test String With |command"

It escapes the | but it does not remove leading and trailing quotes

Re: I need to escape pipe character in a string but still remove leading and trailing quotes

Posted: 04 Feb 2019 12:42
by Squashman
Use this quoting syntax for all of your SET commands.

Code: Select all

set "_string=%_string:~1,-1%"
This technique protects special characters when working with a string.

Re: I need to escape pipe character in a string but still remove leading and trailing quotes

Posted: 04 Feb 2019 15:51
by bosekamineni
@echo off
set _string="Test String With command"
echo the string is %_string%
set "_string=%_string:~1,-1%"
echo the string is %_string%

when there is no special character it works
the string is "Test String With command"
the string is Test String With command


set _string="Test String With |command"
echo the string is %_string%
set "_string=%_string:~1,-1%"

the string is "Test String With |command"
'command' is not recognized as an internal or external command,
operable program or batch file.

Re: I need to escape pipe character in a string but still remove leading and trailing quotes

Posted: 05 Feb 2019 01:40
by bosekamineni
set "_string=%_string:~1,-1%"

did not escape the special character |
any other ideas would be helpful

Re: I need to escape pipe character in a string but still remove leading and trailing quotes

Posted: 05 Feb 2019 02:24
by jeb
Hi bosekamineni,

you should use delayed expansion, because it's always safe to expand with delayed expansion.

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "_string=Test String With | &&<> command"
echo the string is !_string!
The only drawback is that you be careful with literal exclamation marks, when delayed expansion is enabled.

Code: Select all

@echo off
setlocal EnableDelayedExpansion
set "_string=Test String With bang ^!"
echo the string is !_string!

Re: I need to escape pipe character in a string but still remove leading and trailing quotes

Posted: 05 Feb 2019 11:12
by bosekamineni
Jeb

Thank you- I tested your code and I tried to include in the script where I was having issues

existing code -
:addv
@rem set the params to the entire set because quotations causes windows to expand the value and split it up
@rem setlocal EnableDelayedExpansion
set "_params=%*"
@rem strip the wrapper quotes
set _params=%_params:~1,-1%

@rem set _params=!_params!
set /a preMax+=1

@rem wrap the entire set in quotes to deal with bad env variables like foo=<
@rem which can only be set this way when the value isn't already wrapped in quotes
set "pre[%preMax%]=%_params%"

----------------

example of output

call :addv "CliqrTier_WS2012R2_1_USER_DEFINED_TAGS=FE_T1_DHTS_CLINICAL_TEST"

set "_params="CliqrTier_WS2012R2_1_USER_DEFINED_TAGS=FE_T1_DHTS_CLINICAL_TEST""

set _params=CliqrTier_WS2012R2_1_USER_DEFINED_TAGS=FE_T1_DHTS_CLINICAL_TEST

set /a preMax+=1

set "pre[86]=CliqrTier_WS2012R2_1_USER_DEFINED_TAGS=FE_T1_DHTS_CLINICAL_TEST"

goto finish

ENDLOCAL

example where there is issue

call :addv "cliqrWindowsPassword=U2h|ocom5=fd.jgz"

set "_params="cliqrWindowsPassword=U2h | ocom5=fd.jgz""
'ocom5' is not recognized as an internal or external command,
operable program or batch file.

I have question before making changes

set "_params=%*"

I am not sure how to delay the parsing for the parameter -
scripts fails

call :addv "cliqrWindowsPassword=U2h|ocom5=fd.jgz"

set "_params="cliqrWindowsPassword=U2h | ocom5=fd.jgz""
'ocom5' is not recognized as an internal or external command,
operable program or batch file.

Re: I need to escape pipe character in a string but still remove leading and trailing quotes

Posted: 05 Feb 2019 11:22
by bosekamineni
I tried

:addv
@rem set the params to the entire set because quotations causes windows to expand the value and split it up
setlocal EnableDelayedExpansion
set "_params=%*"

It does not delay the parsing and it fails
call :addv "cliqrWindowsPassword=U2h|ocom5=fd.jgz"

setlocal EnableDelayedExpansion

set "_params="cliqrWindowsPassword=U2h | ocom5=fd.jgz""
'ocom5' is not recognized as an internal or external command,
operable program or batch file.