Replace character in string with % (percent) symbol
Moderator: DosItHelp
Replace character in string with % (percent) symbol
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.
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.
sgilmour,
I think what you want is this:
From a text file you get: $varb1$/$varb2$/some text
Your environment variable varb1 is set to: value1
Your environment variable varb2 is set to: value2
You expect the result string: value1/value2/some text
Try 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.:
Will output:
Is this what you want?
I think what you want is this:
From a text file you get: $varb1$/$varb2$/some text
Your environment variable varb1 is set to: value1
Your environment variable varb2 is set to: value2
You expect the result string: value1/value2/some text
Try 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?
Follow-Up
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.
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.
Just a little more help.
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).
guinea3,
Two possibilities:
Pass the 'line' variable over the ENDLOCAL barrier like this.
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:ON
DosItHelp?
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:ON
Code: Select all
@echo off
set varb1=value1
set varb2=value2
set line=$varb1$/$varb2$/some text
call set line=!line:$=%%!
DosItHelp?
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.
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?
-
- Expert
- Posts: 391
- Joined: 19 Mar 2009 08:47
- Location: Iowa
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.