Delete substring using variable inside for /f

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
Blazer
Posts: 13
Joined: 20 Dec 2018 09:47

Delete substring using variable inside for /f

#1 Post by Blazer » 20 Dec 2018 10:26

I am trying to delete a substring from a variable using another variable inside a for /f loop.

The "Setlocal EnableDelayedExpansion" is required for other code.

The following example does not work.

Please help.

Thank you.

Code: Select all

@Echo off

for /f "delims=" %%F in ('dir /b "C:\Windows"') do (

Setlocal EnableDelayedExpansion

Set _cities="Aberdeen, London, Edinburgh"

Set _substr=Rome

Set _dummy=!_cities:!_substr!=!

Echo !_dummy!

If Not "!_dummy!"=="!_cities!" (Echo !_substr! was found.) Else (Echo !_substr! was not found.)

Endlocal

)
Last edited by Blazer on 21 Dec 2018 03:23, edited 1 time in total.

aGerman
Expert
Posts: 3639
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Delete substring using variable inside for /f

#2 Post by aGerman » 20 Dec 2018 15:41

Probably:

Code: Select all

Set _dummy=!_cities:%_substr%=!
Steffen

Blazer
Posts: 13
Joined: 20 Dec 2018 09:47

Re: Delete substring using variable inside for /f

#3 Post by Blazer » 21 Dec 2018 03:22

Thank you for your input Steffen, unfortunately that does not work :(

Here is the new code:

Code: Select all

@Echo off

for /f "delims=" %%F in ('dir /b "C:\Windows"') do (

Setlocal EnableDelayedExpansion

Set _cities="Aberdeen, London, Edinburgh"

Set _substr=Rome

Set _dummy=!_cities:%_substr%=!

Echo !_dummy!

If Not "!_dummy!"=="!_cities!" (Echo !_substr! was found.) Else (Echo !_substr! was not found.)

Endlocal

)

aGerman
Expert
Posts: 3639
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Delete substring using variable inside for /f

#4 Post by aGerman » 21 Dec 2018 07:31

Is it mandatory that you set the value of _substr inside of the parenthesized block? If not, just set the value before you run the loop. Otherwise it will get more tricky:

Code: Select all

for /f "delims=" %%S in ("!_substr!") do Set _dummy=!_cities:%%S=!
Steffen

Blazer
Posts: 13
Joined: 20 Dec 2018 09:47

Re: Delete substring using variable inside for /f

#5 Post by Blazer » 23 Dec 2018 05:28

That does work but I was hoping to find a way to fix the existing code without adding another for loop.

Maybe some extra exclamation or percent characters.

Thanks.

Squashman
Expert
Posts: 4106
Joined: 23 Dec 2011 13:59

Re: Delete substring using variable inside for /f

#6 Post by Squashman » 24 Dec 2018 00:11

Call Set "_dummy=%%_cities:!_substr!=%%"

Blazer
Posts: 13
Joined: 20 Dec 2018 09:47

Re: Delete substring using variable inside for /f

#7 Post by Blazer » 25 Dec 2018 06:20

@aGerman
@Squashman

Thank you for your help :D

Blazer
Posts: 13
Joined: 20 Dec 2018 09:47

Re: Delete substring using variable inside for /f

#8 Post by Blazer » 02 Jan 2019 05:22

This problem has been solved by both @aGerman and @Squashman :)

But I wonder if its possible to tweak the solution from @Squashman so that it works without the call statement?

Call Set "_dummy=%%_cities:!_substr!=%%"

Thank you

Post Reply