search & replace strings in environment variables

F

foxidrive

Hello Jerold,

have you really tried?

SET TestVar="Asterisks*should*be*replaced"
SET TestVar=%TestVar:*=_%

The "*" in the second expression is handled as a wildcard and this results
in an error condition. I've tested it with XP/2k/2k3 and it does NOT work.

Regards,
Frank

Go and read Jerold's post again, Frank. He suggested a completely
different approach.
 
P

Phil Robyn

foxidrive said:
Go and read Jerold's post again, Frank. He suggested a completely
different approach.

Was it something like this?

C:\cmd>demo\replaceasterisks
TestVar=Asterisks*should*be*replaced
TestVar=Asterisks_should_be_replaced

C:\cmd>wyllist demo\ReplaceAsterisks.cmd
==========begin file C:\cmd\demo\ReplaceAsterisks.cmd ==========
001. @echo off
002. setlocal
003. SET "TestVar=Asterisks*should*be*replaced"
004. set TestVar
005. set "NewVar="
006. :loop
007. for /f "tokens=1* delims=*" %%a in (
008. "%TestVar%"
009. ) do (set NewVar=%NewVar%_%%a&set TestVar=%%b)
010. if defined TestVar goto :loop
011. set TestVar=%NewVar:~1%
012. set TestVar
==========end file C:\cmd\demo\ReplaceAsterisks.cmd ==========
 
F

Frank

Hello Phil,

best approach I've seen so far. Is there a trick to also replace equal signs
or double quotes (or even better: ANY character) using this method?


Best regards,

Frank
 
P

Phil Robyn

Frank said:
Hello Phil,

best approach I've seen so far. Is there a trick to also replace equal signs
or double quotes (or even better: ANY character) using this method?


Best regards,

Frank

Hi, Frank,

If this is a learning experience and you just want to toy with this
topic, you can modify the example I posted to replace equal signs and
see if it works.

On the other hand, if you want something really robust and don't want
to have to worry about 'will it handle character such-and-such?', then
if I were you I would try to put something together using GSAR, which
you can obtain for free from

http://gnuwin32.sourceforge.net/packages/gsar.htm

One basic approach might be to write the environment variables in
question to a file, invoke GSAR to do the change(s), and then read
the changed environment variables from the file.

But, as someone else recently said in this (or some other) newsgroup,
there's definitely more than one way to skin a cat....
 
F

Frank

Hi Phil,

for now I choose VBS to get around the shell limitations because VBS is
built in Windows and sufficient for the task to be done (especially with the
RegExp component). I'm looking forward to the new command shell Monad ;-)


Kind regards,

Frank
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top