1.

Solve : Using environment variable string substitution?

Answer»

I know how to use string substitution in a WinXP command shell using the set command with the syntax
Code: [Select]set var2=%var:old=new%as in the example
Code: [Select]@echo off
set Text=This is the old string
set New=%Text:old=new%
echo String1 = %Text%
echo String2 = %New%I am trying to figure out how to use environment variables as the "old" and "new" in this example. SOMETHING like:
set New=%Text:%old%=%new%%
but the extra percent signs seem to break the code. Does anybody know how to excape the % or a work-around to accomplish this task?
I think I figured it out. You can CALL the set to use environment variables:
Code: [Select]call set string=%%Text:%old%=%new%%%Good Gurugary. [smiley=shocked.gif]

I have a question: Where you get the information?
Where you get your ideas
I have read the whole documentation of CALL and SET and I can't to find these things.

Here you have another possible solution

Code: [Select]@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

set Text=This is the old string
set old=old
set new=new
set TextNew=!Text:%old%=%new%!

echo %Text%
echo %TextNew%[smiley=beer.gif]
Hey, CARLOS.

I get most of my information on the web. I keep a LINK to http://www.microsoft.com/resources/documentation/windows/xp/all/proddocs/en-us/ntcmds.mspx which is a good reference to the stuff I haven't memorized yet.

My ideas often come from helping others on other forums like this one. This particular question was for a user that wanted a "rename" script where he could specify the text to be replaced in the rename.

I got the idea for the solution to this particular problem at http://www.microsoft.com/technet/prodtechnol/Windows2000serv/support/FAQW2KCP.mspx under "How do I test / manipulate strings"

So, where do you get YOUR information. I have seen many of your posts, and you have a very thorough knowledge of batch programming.

In case you are interested in a real example of the string substitution code, here is the batch file I CAME up with to HELP the user with the rename script:
Code: [Select]@echo off
setlocal

set /p Srch=Text in filename to find:
set /p Repl=Text to replace "%Srch%" with:
echo Searching for filenames containing "%Srch%" and replacing with "%Repl%" ...
for /f "tokens=*" %%a in ('dir /b *%Srch%*') do call :Replace "%%a"
goto :EOF

:Replace
set x=%1
call set x=%%x:%Srch%=%Repl%%%
echo ren %1 %x%Thanks for the answer and the links.
I`ll have a hard task until I reads it them. But it continues seeming very strange the mix of the senteces CALL SET... to replace text :-?

[smiley=beer.gif]




Discussion

No Comment Found