@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
I need to escape pipe character in a string but still remove leading and trailing quotes
Moderator: DosItHelp
-
- 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
Use this quoting syntax for all of your SET commands.
This technique protects special characters when working with a string.
Code: Select all
set "_string=%_string:~1,-1%"
-
- 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
@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.
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.
-
- 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
set "_string=%_string:~1,-1%"
did not escape the special character |
any other ideas would be helpful
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
Hi bosekamineni,
you should use delayed expansion, because it's always safe to expand with delayed expansion.
The only drawback is that you be careful with literal exclamation marks, when delayed expansion is enabled.
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!
Code: Select all
@echo off
setlocal EnableDelayedExpansion
set "_string=Test String With bang ^!"
echo the string is !_string!
-
- 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
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.
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.
-
- 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
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.
: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.