Replace "!" in a variable value when delayed expansion is on

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
GCRaistlin
Posts: 3
Joined: 23 Apr 2019 05:55

Replace "!" in a variable value when delayed expansion is on

#1 Post by GCRaistlin » 28 Aug 2019 05:37

Is there a way to replace every exclamation mark in a string when delayed expansion is on?

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

Re: Replace "!" in a variable value when delayed expansion is on

#2 Post by jeb » 28 Aug 2019 11:24

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

GCRaistlin
Posts: 3
Joined: 23 Apr 2019 05:55

Re: Replace "!" in a variable value when delayed expansion is on

#3 Post by GCRaistlin » 29 Aug 2019 01:39

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.

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Replace "!" in a variable value when delayed expansion is on

#4 Post by penpen » 29 Aug 2019 04:32

jeb wrote:

Code: Select all

call set "_tmp=%%_tmp:^!=###%%"
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

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

Re: Replace "!" in a variable value when delayed expansion is on

#5 Post by jeb » 29 Aug 2019 06:10

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.

GCRaistlin
Posts: 3
Joined: 23 Apr 2019 05:55

Re: Replace "!" in a variable value when delayed expansion is on

#6 Post by GCRaistlin » 29 Aug 2019 10:49

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?

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Replace "!" in a variable value when delayed expansion is on

#7 Post by dbenham » 29 Aug 2019 11:24

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

Code: Select all

...XYZ
=...
Dave Benham

penpen
Expert
Posts: 1991
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Replace "!" in a variable value when delayed expansion is on

#8 Post by penpen » 29 Aug 2019 11:48

GCRaistlin wrote:
29 Aug 2019 10:49
What is "bang"?
I also never heard that; my google-fu lead me to that explaination:
https://ss64.com/bash/syntax-pronounce.html#01


penpen

Post Reply