I'm unable to use @replace[] with a % character in string2

Jul 21, 2014
6
0
France
Hi,

I need to replace the blank characters into a string with the '%20' substring.
For example: "foo bar" --> "foo%20bar"

Code:
E:\>set var=foo bar

E:\>echo %var
foo bar

E:\>echo %@replace[ ,%20,%var]
foobar

What? Oh yes, '%' is a special character, I need to escape it:

Code:
E:\>echo %@replace[ ,^%20,%var]
foo

??? I don't understand anymore. Why has the second word vanished? What did TCC understand?

Yet ^% works well outside the @replace function:
Code:
E:\>echo ^%20
%20
So how should I do to obtain "foo%20bar"?
 
Apr 18, 2014
301
9
You need to use two percent signs, both escaped...
Code:
d:\>echo %@replace[ ,^%^%20,%var]
foo%20bar
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,491
90
Albuquerque, NM
prospero.unm.edu
Here's one way:
Code:
echo %@replace[ ,`%20`,%var]

Percent signs are always going to cause headaches for you. If you want to do anything further with the returned string, you may need to double the percent sign inside the strong quotes. Or play games with SETDOS /X.
 

samintz

Scott Mintz
May 20, 2008
1,511
18
Solon, OH, USA
This also works, but Rex will need to explain why the percents need to be quadrupled.
Code:
echo %@replace[ ,%%%%20,foo bar]
 

samintz

Scott Mintz
May 20, 2008
1,511
18
Solon, OH, USA
I sort of understand that it is creating foo%20bar and replacing %20bar with nothing because that env var is undefined. You can see this by:
Code:
set 20bar=hello
echo %@replace[ ,%%20,foo bar]
foohello
 
Jul 21, 2014
6
0
France
Thanks all for your answers, they work well.

Scott, your explanation is lucid and probably right. But the behavior of TCC differs depending on the function :
Code:
E:\>echo %@insert[2,^%20,abcd]
ab%20cd
With @insert[], one ^% is enough. But with @replace[], I have to use two ^%:
Code:
E:\>echo %@replace[ab,^%20,abcd]
ECHO est OFF

E:\>echo %@replace[ab,^%^%20,abcd]
%20cd

By the way, Charles, what is the meaning of back quotes (in `%20%`)? I found nothing about them in the help file.
I first thought that they were used to escape a whole string, but it's not so simple:
Code:
E:\>set 20bar=hello

E:\>echo %@replace[ ,`%20`,foo bar]
foo%20bar

E:\>set a=%@replace[ ,`%20`,foo bar]

E:\>echo %a
foohello
 

Charles Dye

Super Moderator
Staff member
May 20, 2008
4,491
90
Albuquerque, NM
prospero.unm.edu
By the way, Charles, what is the meaning of back quotes (in `%20%`)? I found nothing about them in the help file.
I first thought that they were used to escape a whole string, but it's not so simple:
Code:
E:\>set 20bar=hello

E:\>echo %@replace[ ,`%20`,foo bar]
foo%20bar

E:\>set a=%@replace[ ,`%20`,foo bar]

E:\>echo %a
foohello

Sorry, I missed this post. Yes, strong quotes prevent all variable expansion on whatever they enclose. So, for example:
Code:
set a=%@replace[ ,`%20`,foo bar]
will put replace any spaces in "foo bar" with the string "%20". You can verify that that is indeed what gets stored by typing:
Code:
set a

But stashing percent signs in an environment variable doesn't disarm them. They can still be used for variable expansion later on. So if you type:
Code:
echo %a
TCC wil expand replace %A with the value of environment variable A; then expand that string, replacing the %20BAR with the value of the 20BAR variable. (Or maybe with the 20th batch parameter or alias parameter, followed by "BAR".) You can use use this technique to delay variable expansion, saving expressions to be evaluated at a later time. You'll find more information under HELP QUOTING. (Rex calls these strong quotes "back quotes". I call that a "grave" misnomer, but never mind....)
 
May 20, 2008
11,520
102
Syracuse, NY, USA
The difference between these is puzzling.
Code:
v:\> echo %@insert[0,^%20,abcd]
%20abcd

v:\> echo %@replace[ab,^%20,abcd]
ECHO is OFF

v:\> echo %@replace[ab,^%^%20,abcd]
%20cd
@REPLACE's behavior is no surprise, but after the @INSERTion ("%20abcd") I'd expect ECHO to produce the value of the environment variable named "20abcd" (as Charles described).
 
May 20, 2008
11,520
102
Syracuse, NY, USA
You're not inserting "%20", you're inserting "^%20".
There's still a difference. Why doesn't @REPLACE[bc,^%20,abcd] replace "bc" with "^%20"? If it did, I'd ecpect to see the "%20" below.
Code:
v:\> echo %@replace[bc,^%20,abcd]
ECHO is OFF
With @REPLACE[] two escapes are needed.
Code:
v:\> echo %@replace[bc,^^^%20,abcd]
a%20d
 

rconn

Administrator
Staff member
May 14, 2008
12,404
152
Why would you think they would be identical? They serve different purposes.

@REPLACE does an internal "unescape" of its arguments. It has to in order to substitute special characters; @INSERT doesn't have that issue. (Note -- this is 20-year-old behavior!)
 
May 20, 2008
11,520
102
Syracuse, NY, USA
Why would you think they would be identical? They serve different purposes.

@REPLACE does an internal "unescape" of its arguments. It has to in order to substitute special characters; @INSERT doesn't have that issue. (Note -- this is 20-year-old behavior!)
What special characters? Are there characters which @INSERT can't insert because @INSERT doesn't do a similar internal "unescape"?

Yes, they have different names and they do different things. But there's something to be said for consistent behavior. On the surface (even now, to me) there doesn't seem to be a need for them to behave differently. How's the new (or old) user supposed to figure these things out?
 

rconn

Administrator
Staff member
May 14, 2008
12,404
152
There is absolutely no chance that I'm going to break a few zillion existing batch files and aliases to solve an imaginary consistency problem for you ...

You can put anything in @INSERT, but you have to unescape the line afterwards. There's no way I could do the same thing in @REPLACE, unless I disallowed all special characters in the arguments.
 
Similar threads
Thread starter Title Forum Replies Date
N Unable to bind multiple keys to the same key mapping directive Support 9
A Unable to pass batch file arguments unaltered Support 18
Eric Peters New Computer, unable to register Support 3
A Unable to register TC23 Support 1
D Unable to uninstall TCC 21 Support 1
G How to? Unable to install TCMD 21 under Win2012R2 Support 5
K Unable to register TakeCommand Support 4
W Unable to register TCMD v21 Support 16
dcantor Unable to access help Support 9
WavSlave Fixed Unable to sort files by description in TCC v19 Support 2
nickles WAD del unable to delete files ending in "." Support 18
R Unable to start any BTM file from Explorer Support 5
Jay Sage Unable to Specify INI File Support 19
thedave WAD Unable to type various characters using Windows 8's onscreen keyboard Support 7
Dan Glynhampton Fixed @version "force" flag not working, and unable to set separator Support 3
D ftps gives "Unable to build data connection" Support 5
M Why am I unable to delete files? Support 8
Y Unable to register v13. Support 1
Y Unable to close v13 Support 26
N Unable to end tcc.exe process Support 1
Y Unable to install 10.00.50 Support 3
Joe Caverly Documentation Replace internal command with LIBRARY command Support 0
I how to make TCC default/replace cmd.exe? Support 9
x13 TPIPE /REPLACE - Remove line (including EOL) Support 3
T @replace -- what is wrong here Support 2
C Replace 1st argument on command line Support 4
R How to? Replace in Rename Support 9
C How to? @replace[a comma,_,filename] Support 5
MickeyF Can't get my TPIPE /replace to work Support 4
C @replace and @xreplace problems Support 5
C How to? Replace &'s in filenames / FixNames.dll Support 2
MikeBaas Strange prob with %@replace.. Support 4
vefatica Back-refs in TPIPE's /replace's replacement string? Support 2
Frank @replace works case sensitive Support 5
J @replace weirdness Support 1
vefatica TPIPE /replace? Support 3
Stefano Piccardi replace tcc process space? Support 8
vefatica @SUBST ... how replace character with a caret? Support 9
mdwyer @replace - global use in a text file Support 3
B Regex and Replace Support 6
B REPLACE is good news Support 0
J Problem with %var:find=replace% syntax Support 5
vefatica WAD TCC: inconsistent character handling Support 11
vefatica @INT with comma as decimal character Support 2
vefatica What's this character? Support 12
rps Regex problem: \xnn not recognized as a hex character Support 0
old coot Regex problem: \xnn not recognized as a hex character Support 12
L How to? Character transfer among @BALLOC buffers, command tails, and possibly redirected standard streams Support 11
D How to? Filenames with "&" character Support 3
samintz WAD Copy/append adds a character Support 3

Similar threads