Replace "!" in a variable value when delayed expansion is on
Moderator: DosItHelp
-
- Posts: 3
- Joined: 23 Apr 2019 05:55
Replace "!" in a variable value when delayed expansion is on
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
Hi GCRaistlin,
yes, but it depends on the content of your string
It's simple for simple content (without quotes in the content)
For content with quotes inside, it's gets nasty
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
-
- Posts: 3
- Joined: 23 Apr 2019 05:55
Re: Replace "!" in a variable value when delayed expansion is on
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
I don't see, why the call should be neccessary:jeb wrote:Code: Select all
call set "_tmp=%%_tmp:^!=###%%"
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
Re: Replace "!" in a variable value when delayed expansion is on
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.
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.
-
- Posts: 3
- Joined: 23 Apr 2019 05:55
Re: Replace "!" in a variable value when delayed expansion is on
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:
--OUTPUT--
Dave Benham
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:!=...!
Code: Select all
...XYZ
=...
Re: Replace "!" in a variable value when delayed expansion is on
I also never heard that; my google-fu lead me to that explaination:
https://ss64.com/bash/syntax-pronounce.html#01
penpen