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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bosekamineni
Posts: 5
Joined: 04 Feb 2019 12:05

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

#1 Post by bosekamineni » 04 Feb 2019 12:16

@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

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

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

#2 Post by Squashman » 04 Feb 2019 12:42

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.

bosekamineni
Posts: 5
Joined: 04 Feb 2019 12:05

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

#3 Post by bosekamineni » 04 Feb 2019 15:51

@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.

bosekamineni
Posts: 5
Joined: 04 Feb 2019 12:05

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

#4 Post by bosekamineni » 05 Feb 2019 01:40

set "_string=%_string:~1,-1%"

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

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

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

#5 Post by jeb » 05 Feb 2019 02:24

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!

bosekamineni
Posts: 5
Joined: 04 Feb 2019 12:05

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

#6 Post by bosekamineni » 05 Feb 2019 11:12

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.

bosekamineni
Posts: 5
Joined: 04 Feb 2019 12:05

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

#7 Post by bosekamineni » 05 Feb 2019 11:22

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.

Post Reply