Discussion forum for all Windows batch related topics.
Moderator: DosItHelp
-
Blazer
- Posts: 14
- Joined: 20 Dec 2018 09:47
#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.
-
Blazer
- Posts: 14
- Joined: 20 Dec 2018 09:47
#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: 4678
- Joined: 22 Jan 2010 18:01
- Location: Germany
#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: 14
- Joined: 20 Dec 2018 09:47
#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.
-
Blazer
- Posts: 14
- Joined: 20 Dec 2018 09:47
#7
Post
by Blazer » 25 Dec 2018 06:20
@aGerman
@Squashman
Thank you for your help
-
Blazer
- Posts: 14
- Joined: 20 Dec 2018 09:47
#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