Page 1 of 1
Replace "!" in a variable value when delayed expansion is on
Posted: 28 Aug 2019 05:37
by GCRaistlin
Is there a way to replace every exclamation mark in a string when delayed expansion is on?
Re: Replace "!" in a variable value when delayed expansion is on
Posted: 28 Aug 2019 11:24
by jeb
Hi GCRaistlin,
yes, but it depends on the content of your string
It's simple for simple content (without quotes in the content)
Code: Select all
@echo off
setlocal DisableDelayedExpansion
set "string=Test with caret ^ bang ! and some other <>&| %% stuff"
setlocal EnableDelayedExpansion
set "_tmp=%string:!=###%"
set _tmp
For content with quotes inside, it's gets nasty
Code: Select all
@echo off
setlocal DisableDelayedExpansion
set "string=caret ^ bang ! and some other <>&| %%"
setlocal EnableDelayedExpansion
set "string=!string! and also quoted stuff "!string!""
set string
set "_tmp=!string!"
set "_tmp=!_tmp:"=""!"
call set "_tmp=%%_tmp:^!=###%%"
set "_tmp=!_tmp:""="!"
set _tmp
Re: Replace "!" in a variable value when delayed expansion is on
Posted: 29 Aug 2019 01:39
by GCRaistlin
Sorry I wasn't precise, I was seeking for the way to replace "!" using !-expansion. call set works, but its perfomance is just terrible. Anyway, thanks for the trick with doubling the quotes.
Re: Replace "!" in a variable value when delayed expansion is on
Posted: 29 Aug 2019 04:32
by penpen
I don't see, why the call should be neccessary:
After you doubled the doublequotes, then you should be in the state which is equivalent to using no doublequotes.
At least it does work with your example on win10 (so i wonder if i am missing something):
Code: Select all
@echo off
setlocal DisableDelayedExpansion
set "string=caret ^ bang ! and some other <>&| %%"
setlocal EnableDelayedExpansion
set "string=!string! and also quoted stuff "!string!""
set string
set "_tmp=!string!"
set "_tmp=!_tmp:"=""!"
set "_tmp=%_tmp:!=###%"
set "_tmp=!_tmp:""="!"
set _tmp
penpen
Re: Replace "!" in a variable value when delayed expansion is on
Posted: 29 Aug 2019 06:10
by jeb
Hi Penpen,
you are right. The Call is superfluous, I cpoied it from macro code.
@GCRaistlin
No it's not possible to use delayed expansion to replace a single bang, only when the bang is precede with other charactets.
Re: Replace "!" in a variable value when delayed expansion is on
Posted: 29 Aug 2019 10:49
by GCRaistlin
jeb wrote: ↑29 Aug 2019 06:10
No it's not possible to use delayed expansion to replace a single bang, only when the bang is precede with other charactets.
What is "bang"? And I don't understand the last part - how preceding with other characters can change the situation?
Re: Replace "!" in a variable value when delayed expansion is on
Posted: 29 Aug 2019 11:24
by dbenham
Bang is a term used in the computer industry to mean exclamation point. I believe it originated in the Unix world.
The statement about characters before hand has to do with the
mechanism (rules) used to parse/expand variables.
Here is a demonstration:
Code: Select all
@echo off
setlocal disableDelayedExpansion
set "VAR=ABC!XYZ"
setlocal enableDelayedExpansion
REM This works, replacing all characters from beginning through the !
echo !VAR:*!=...!
REM This fails because it attempts to expand a variable named VAR:, which is empty (undefined), and then it strips the trailing !
echo !VAR:!=...!
--OUTPUT--
Dave Benham
Re: Replace "!" in a variable value when delayed expansion is on
Posted: 29 Aug 2019 11:48
by penpen
I also never heard that; my google-fu lead me to that explaination:
https://ss64.com/bash/syntax-pronounce.html#01
penpen