Solved Batch file: replace substring using variables

August 9, 2012 at 10:13:35
Specs: Windows 7
I have two variables:
1: %path% string
2: a variable that needs to be searched for and removed from 1.


@ECHO OFF
set pathString=C:\Program Files (x86)\company\product\apache-tomcat-7.0.12\webapps\gc\WEB-INF\lib;c:\test2;
set search1=C:\Program Files (x86)\company\product\apache-tomcat-7.0.12\webapps\gc\WEB-INF\lib;

echo %pathString% | find "%search1%"

IF (%ERRORLEVEL% == 0) (
rem the following line is what I'm having trouble with. All examples I have seen are using static text, but I need to replace the contents of variable 1 where variable 2 is found and remove it.
set str=%pathString:search1=%
)


rem the result should be c:\test2;

additionally, I can not use files or change the invoke switches for dos. reason: I am embedding this batch script into an installation software: InstallAnywhere


See More: Batch file: replace substring using variables

Report •


✔ Best Answer
August 9, 2012 at 21:21:50
it's the () that are killing you. These are highly toxic to batchscript.
Lethal, unless precautions are taken. Thankyou, MS, for putting all this garbage into filenames. Blessings.
This worked on your sample: (left all debugging crap in place.)
@ECHO OFF & setlocal enabledelayedexpansion
:first, wrap the variable in quotes to insulate against the parentheses.
set pathString="C:\Program Files (x86)\company\product\apache-tomcat-7.0.12\webapps\gc\WEB-INF\lib;c:\test2;"
set search1="C:\Program Files (x86)\company\product\apache-tomcat-7.0.12\webapps\gc\WEB-INF\lib;"


echo %pathstring%
echo %search1%


echo %pathString% | find %search1%


echo ERRLVL: %errorlevel%

IF %ERRORLEVEL%==0 (
rem the following line is what I'm having trouble with. All examples I have seen are using static text, but I need to replace the contents of variable 1 where variable 2 is found and remove it.
:now we gotta get rid of the quotes, hence this next set of code
for /f "tokens=*" %%b in (%search1%) do set f1=%%~b
echo f1 IS: !f1!
for /f "tokens=*" %%c in (%pathstring%) do set f2=%%~c
echo f2 is: !f2!
call :edit
echo target:!str!
)

goto :eof
:edit
echo in edit, f1=%f1%
set str=!f2:%f1%=!
:end

it worked on xp, but not tested on seven.



#1
August 9, 2012 at 10:25:28
My program files lives on the E: drive, and my OS is 32-bit. Your installer would fail for me, ∴ your product must suck. How could it not? It can't even install itself correctly.

How To Ask Questions The Smart Way


Report •

#2
August 9, 2012 at 11:24:12
anyone?

Report •

#3
August 9, 2012 at 11:25:22
the literal pathStrings are only used while I'm testing. They will be replaced with InstallAnywhere variables selected at run-time by the user.

Report •

Related Solutions

#4
August 9, 2012 at 21:21:50
✔ Best Answer
it's the () that are killing you. These are highly toxic to batchscript.
Lethal, unless precautions are taken. Thankyou, MS, for putting all this garbage into filenames. Blessings.
This worked on your sample: (left all debugging crap in place.)
@ECHO OFF & setlocal enabledelayedexpansion
:first, wrap the variable in quotes to insulate against the parentheses.
set pathString="C:\Program Files (x86)\company\product\apache-tomcat-7.0.12\webapps\gc\WEB-INF\lib;c:\test2;"
set search1="C:\Program Files (x86)\company\product\apache-tomcat-7.0.12\webapps\gc\WEB-INF\lib;"


echo %pathstring%
echo %search1%


echo %pathString% | find %search1%


echo ERRLVL: %errorlevel%

IF %ERRORLEVEL%==0 (
rem the following line is what I'm having trouble with. All examples I have seen are using static text, but I need to replace the contents of variable 1 where variable 2 is found and remove it.
:now we gotta get rid of the quotes, hence this next set of code
for /f "tokens=*" %%b in (%search1%) do set f1=%%~b
echo f1 IS: !f1!
for /f "tokens=*" %%c in (%pathstring%) do set f2=%%~c
echo f2 is: !f2!
call :edit
echo target:!str!
)

goto :eof
:edit
echo in edit, f1=%f1%
set str=!f2:%f1%=!
:end

it worked on xp, but not tested on seven.


Report •

Ask Question