1

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=!
1
  • This type of management is fully described at this answer, although the topic is different. Commented Sep 20, 2017 at 14:46

1 Answer 1

2

you can use delayed expansion:

setlocal enableDelayedExpansion
set "target=some"
for %%i in ("something") do (
 set "var=%%i"
 echo !var:%target%!
)
Sign up to request clarification or add additional context in comments.

5 Comments

can it done without a loop? I am trying to make it simple as possible as I am not familiar with writing batch. I have updated my question to provide more information
@LeonArmstrong - you can.Just you mentioned that your code is withing a for loop.
echo !str:%targetPath%=! I try to use this line within the code , it is not running
@LeonArmstrong do you have the setlocal enableDelayedExpansion line before that?
Yes I have , I edited the question to provide more information , guess I have other syntax that affect the output.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.