Leon Armstrong
Leon Armstrong

Reputation: 1303

Sub string a variable with variable

I have a .bat file that needed to substring a variable with another variable. The the substring will be inside a loop. I have two variable inside this loop.

echo %%i  

echo %targetPath%

%%i is the full string. And I have to remove part of %%i if found a match string with %targetPath%

My coding which is not working.

echo  %%i%:%targetPath%=%

My couldn't get it to work because I am confused with the %%name and %name%. This is my expected result. In php , we have a substring function that able to pass the fullstring into substring function and a keyword to be match. And this is what I trying to achieve

fullstr=abcdefghijk
keyword=abcd

result=efghijk

Current working code

setlocal EnableDelayedExpansion 

set "targetPath=%~dp0in"

for /r "%targetPath%" %%i in (.) do (

set "str=%%i"
echo  !str:%targetPath%=!

)

Current output

echo  !str:C:\Users\vuser01\Desktop\Texture Packer\libgdxtools\in=!

Upvotes: 1

Views: 73

Answers (1)

npocmaka
npocmaka

Reputation: 57282

you can use delayed expansion:

setlocal enableDelayedExpansion
set "target=some"
for %%i in ("something") do (
 set "var=%%i"
 echo !var:%target%!
)

Upvotes: 2

Related Questions