Discussion:
Replacing a character with a percent symbol in a variable
(too old to reply)
Anton Shepelev
2024-05-21 16:58:13 UTC
Permalink
Hello, all.

How can one replace `#' with `%' in the value of a variable?
I have tried:

SET VAR=%VAR:#=%%%
SET VAR=%VAR:#=^%%

but to no avail.
--
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments
Herbert Kleebauer
2024-05-21 21:25:37 UTC
Permalink
Post by Anton Shepelev
How can one replace `#' with `%' in the value of a variable?
SET VAR=%VAR:#=%%%
SET VAR=%VAR:#=^%%
but to no avail.
I think it is a real bad idea to use a % in a variable,
but this should do it:


setlocal enabledelayedexpansion
set var=a#b##c
set p=%%

echo %var%
set var=%var:#=!p!%
echo %var%
Anton Shepelev
2024-05-22 12:29:41 UTC
Permalink
Post by Herbert Kleebauer
Post by Anton Shepelev
How can one replace `#' with `%' in the value of a variable?
SET VAR=%VAR:#=%%%
SET VAR=%VAR:#=^%%
but to no avail.
I think it is a real bad idea to use a % in a variable,
This is intentional: I want then to pass that variable (by
name) to subroutine, where its %'s will be expanded with the
values of local variables. A kind of dependency injection.
Post by Herbert Kleebauer
setlocal enabledelayedexpansion
[...]
Thanks! I there a way without delayed expansion and file
IO?
--
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments
Kenny McCormack
2024-05-22 12:59:53 UTC
Permalink
In article <***@gmail.moc>,
Anton Shepelev <***@gmail.moc> wrote:
...
Post by Herbert Kleebauer
setlocal enabledelayedexpansion
[...]
I there a way without delayed expansion and file IO?
I'm guessing there is an "s" missing after the "I", right?

But I don't see any "file IO". What's that about?
--
Genesis 2:7 And the LORD God formed man of the dust of the ground, and
breathed into his nostrils the breath of life; and man became a living soul.
Anton Shepelev
2024-05-22 14:20:14 UTC
Permalink
Post by Kenny McCormack
I there a way without delayed expansion and file IO?
I'm guessing there is an "s" missing after the "I", right?
Yeah, right.
Post by Kenny McCormack
But I don't see any "file IO". What's that about?
I had a wild conjecture that replacing a given character
(e.g. #) with a % in the value of a varialbe could be
accomplished with the help of file IO operations, by storing
intermediate data on the HDD. I don't want to pursue that
idea.
--
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments
JJ
2024-05-22 13:54:39 UTC
Permalink
I there a way without delayed expansion and file IO?
You could borrow MSHTA's JScript to do the troublesome work.
Caveat: slow and user may notice a flash of MSHTA window.
(long line warning; one long FOR command line, then the ECHO line)

@echo off
setlocal
set "src=a#b%%c!d#e%%f!g"
echo "src=%src%"
set dest=
for /f "delims=" %%A in ('mshta.exe "javascript:new ActiveXObject("scripting.filesystemobject").getstandardstream(1).write("%src%".replace(/#/g,"%%")),close()"') do set "dest=%%A"
echo "dest=%dest%"
Anton Shepelev
2024-05-30 10:36:34 UTC
Permalink
Post by JJ
You could borrow MSHTA's JScript to do the troublesome
work.
Indeed, thanks for the hint. If I should need an external
utility program to facilitate batch programming, I will
rather write one in C.
Post by JJ
(long line warning; one long FOR command line, then the
ECHO line)
Yeah -- so many broken Newsreaders nowadays, but not mine,
not mine.
--
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments
Herbert Kleebauer
2024-05-22 15:06:43 UTC
Permalink
Post by Anton Shepelev
Thanks! I there a way without delayed expansion and file
IO?
I don't see any file IO and if VAR doesn't have any
spaces or other delimiters und there are only single
#, than this should work (but what is wrong with
delayed expansion?):

set var=a#b#c

set var2=%var:#= %&set var=&
for %%i in (%var2%) do call set var=%%var%%%%i%%%%
set var=%var:~0,-1%

echo %var%
Anton Shepelev
2024-05-30 10:33:47 UTC
Permalink
Post by Herbert Kleebauer
I don't see any file IO
I mentioned IO as a possible way to use the HDD as an
external memory in the script -- a crazy idea that I
abandoned.
Post by Herbert Kleebauer
and if VAR doesn't have any spaces or other delimiters und
there are only single #, than this should work (but what
I consider this additional limitation as a challenge.
Post by Herbert Kleebauer
set var=a#b#c
set var2=%var:#= %&set var=&
for %%i in (%var2%) do call set var=%%var%%%%i%%%%
set var=%var:~0,-1%
echo %var%
Thank you. You use CALL SET to effect a double (a therefore
delayed) expansion. The `&' in `set var=&' seems
unnecessary. Is it there to avoid potential trailing
whitespace?
--
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments
Herbert Kleebauer
2024-05-30 15:00:17 UTC
Permalink
Post by Anton Shepelev
Post by Herbert Kleebauer
and if VAR doesn't have any spaces or other delimiters und
there are only single #, than this should work (but what
I consider this additional limitation as a challenge.
Batch programming is always a challenge.
Post by Anton Shepelev
Post by Herbert Kleebauer
set var=a#b#c
set var2=%var:#= %&set var=&
for %%i in (%var2%) do call set var=%%var%%%%i%%%%
set var=%var:~0,-1%
echo %var%
The `&' in `set var=&' seems
unnecessary. Is it there to avoid potential trailing
whitespace?
Yes.

But why do you want to do it without delayed expansion?
You don't have to use it for the complete batch code,
just for the replacement of the #:

set var=a#b##c

setlocal enabledelayedexpansion&set p=%%&set var=%var:#=!p!%
endlocal&set var=%var%

echo %var%

Zaidy036
2024-05-22 14:19:20 UTC
Permalink
Post by Anton Shepelev
Hello, all.
How can one replace `#' with `%' in the value of a variable?
SET VAR=%VAR:#=%%%
SET VAR=%VAR:#=^%%
but to no avail.
why not generate a new variable name using text commands something like:

Find location of # = n
Set xx=%VAR:~,n%
Set yy=%VAR:~n,%
Set VAR=%xx%%%yy%
Anton Shepelev
2024-05-30 10:57:19 UTC
Permalink
Post by Zaidy036
why not generate a new variable name using text commands
Find location of # = n
Set xx=%VAR:~,n%
Set yy=%VAR:~n,%
Set VAR=%xx%%%yy%
I see: you propose to get the substrings around the # and
then concatenate them back with over %. Thanks.
--
() ascii ribbon campaign -- against html e-mail
/\ www.asciiribbon.org -- against proprietary attachments
Loading...