Replace character in string with % (percent) symbol

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
sgilmour
Posts: 5
Joined: 31 Oct 2006 16:16

Replace character in string with % (percent) symbol

#1 Post by sgilmour » 31 Oct 2006 16:19

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.

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#2 Post by DosItHelp » 31 Oct 2006 20:50

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

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? :wink:

sgilmour
Posts: 5
Joined: 31 Oct 2006 16:16

#3 Post by sgilmour » 31 Oct 2006 22:23

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.

sgilmour
Posts: 5
Joined: 31 Oct 2006 16:16

Follow-Up

#4 Post by sgilmour » 01 Nov 2006 15:29

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.

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#5 Post by DosItHelp » 04 Nov 2006 17:54

Awesome :wink:

guinea3
Posts: 2
Joined: 02 Dec 2008 14:38

Just a little more help.

#6 Post by guinea3 » 02 Dec 2008 15:56

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

DosItHelp
Expert
Posts: 239
Joined: 18 Feb 2006 19:54

#7 Post by DosItHelp » 05 Dec 2008 01:43

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

Code: Select all

@echo off
set varb1=value1
set varb2=value2
set line=$varb1$/$varb2$/some text
call set line=!line:$=%%!

DosItHelp? :wink:

guinea3
Posts: 2
Joined: 02 Dec 2008 14:38

#8 Post by guinea3 » 23 Mar 2009 12:07

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. :lol: :lol: :D :D :!: :!:
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:ON

Code: Select all

@echo off
set varb1=value1
set varb2=value2
set line=$varb1$/$varb2$/some text
call set line=!line:$=%%!

DosItHelp? :wink:

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#9 Post by avery_larry » 24 Mar 2009 08:47

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.

Post Reply