Help with substring removal

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
bigpickle
Posts: 2
Joined: 01 Jun 2018 09:28

Help with substring removal

#1 Post by bigpickle » 01 Jun 2018 09:43

Hi. I'm trying to remove a substring " - DASH" from a string but for some reason it's not working. What am I missing here? Thanks for the help!

It's supposed to turn "test - DASH.ogg" into "z-test.mkv".

Code: Select all

for %%A in (*DASH*.ogg) do (
	set outfile=z-%%~nA.mkv
	echo %outfile%
	set outfile=%outfile: - DASH=%
	echo %outfile%
)
Output is:
D:\>(
set outfile=z-test - DASH.mkv
echo
set outfile=- DASH=
echo
)
ECHO is on.

sst
Posts: 93
Joined: 12 Apr 2018 23:45

Re: Help with substring removal

#2 Post by sst » 01 Jun 2018 10:46

Percent variable expansion will take place before execution of the code and it occurs only once within a block of code. So when your code is executing, %outfile% variable is already expanded to nothing (or to whatever it has been defined before that block of code).
Use delayed variable expansion to resolve that.

Code: Select all

setlocal EnableDelayedExpansion
for %%A in (*DASH*.ogg) do (
	set outfile=z-%%~nA.mkv
	echo !outfile!
	set outfile=!outfile: - DASH=!
	echo !outfile!
)

bigpickle
Posts: 2
Joined: 01 Jun 2018 09:28

Re: Help with substring removal

#3 Post by bigpickle » 01 Jun 2018 11:06

That worked, thank you for the quick help sst!

Post Reply