Page 1 of 1
Replace character in string with % (percent) symbol
Posted: 31 Oct 2006 16:19
by sgilmour
I'm trying to replace some character in a environment variable.
The Variable comes from an entry in a text file the has $ symbol, e.g.
$varb1$/$varb2$/some text
I want to replace the $ with a % sign as this will then be used to identify
some preconfigured environment variables of the same name, e.g. $varb1$ will become %varb1%.
However, string replacement does not escape the %% sign at the end of the command line; e.g.
set varb1=$varb1$ ;; this is actually read from a text file.
set varb1=%varb1:$=%%
I've tried everything I can but as soon as the command line sees one %
that's it.
I thought of changing the source text file $ symbols to % but don't know how to do this either. I'd like to get this working without changing the source document, but if I have to I will.
All help greatly appreciated.
Posted: 31 Oct 2006 20:50
by DosItHelp
sgilmour,
I think what you want is this:
From a text file you get:
$varb1$/$varb2$/some textYour environment variable varb1 is set to:
value1Your environment variable varb2 is set to:
value2You expect the result string:
value1/value2/some textTry this...
Enable DELAYEDEXPANSION and resolve your variables using the exclamation mark rather than the percent, so that the command interpreter doesn't read the percent as 'end of variable'.
I.e.:
Code: Select all
SETLOCAL ENABLEDELAYEDEXPANSION
set varb1=value1
set varb2=value2
set line=$varb1$/$varb2$/some text
call set line=!line:$=%%!
echo.%line%
Will output:
value1/value2/some text
Is this what you want?
Posted: 31 Oct 2006 22:23
by sgilmour
I think that may just do the trick - thank you.
I was trying the Delayed Variable option - but could see how it worked.
Thanks so much for this - and the quick reply.
Follow-Up
Posted: 01 Nov 2006 15:29
by sgilmour
Thought I'd post an update on this solution.
I tried the solution and it works perfectly. In fact it has solved a major problem I was having with my script and opens up other opportunities from my program that I didn't think were possible.
So a big thank you for this fix.
Posted: 04 Nov 2006 17:54
by DosItHelp
Awesome
Just a little more help.
Posted: 02 Dec 2008 15:56
by guinea3
Great routine. But how can the Batch be set up so that all variables remain in the environment AFTER THE BATCH HAS COMPLETED (and before the Command Prompt Window is closed).
Posted: 05 Dec 2008 01:43
by DosItHelp
guinea3,
Two possibilities:
Pass the 'line' variable over the ENDLOCAL barrier like this.
Code: Select all
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set varb1=value1
set varb2=value2
set line=$varb1$/$varb2$/some text
call set line=!line:$=%%!
ENDLOCAL & set "line=%line%"
Or:Make sure your Command Prompt Window has Delayedexpansion turned on and avoid the SELTLOCAL call within the batch, i.e. open a new Command Prompt Window via Start-Run:
CMD /V:ONCode: Select all
@echo off
set varb1=value1
set varb2=value2
set line=$varb1$/$varb2$/some text
call set line=!line:$=%%!
DosItHelp?
Posted: 23 Mar 2009 12:07
by guinea3
In response to your 1st method...
This is a very clever trick. It worked great. In this particular case, I had several variables; so to complete the task after your portion of script completed, I added a FOR-LOOP to put all variables back into the environment. That was done immediately after your code.
Since understanding the code, and using it that once, I have applied it in many other Batch Files. I can use that section of code anywhere in the Batch, and often multiple times in the same Batch. I find that most of my code-writing, NOW, cannot get along without it.
Thanx a bunch.
DosItHelp wrote:guinea3,
Two possibilities:
Pass the 'line' variable over the ENDLOCAL barrier like this.
Code: Select all
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION
set varb1=value1
set varb2=value2
set line=$varb1$/$varb2$/some text
call set line=!line:$=%%!
ENDLOCAL & set "line=%line%"
Or:Make sure your Command Prompt Window has Delayedexpansion turned on and avoid the SELTLOCAL call within the batch, i.e. open a new Command Prompt Window via Start-Run:
CMD /V:ONCode: Select all
@echo off
set varb1=value1
set varb2=value2
set line=$varb1$/$varb2$/some text
call set line=!line:$=%%!
DosItHelp?
Posted: 24 Mar 2009 08:47
by avery_larry
guinea3 wrote:In response to your 1st method...
This is a very clever trick. It worked great. In this particular case, I had several variables; so to complete the task after your portion of script completed, I added a FOR-LOOP to put all variables back into the environment.
Could you post an example of the FOR-LOOP you're referring to? Sounds interesting/useful.