For Loop.. delims

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
ldoodle
Posts: 18
Joined: 26 Sep 2015 04:01

For Loop.. delims

#1 Post by ldoodle » 26 Sep 2015 04:10

Hey,

Can you specify complete phrases, including spaces as a delimiter?

This is what I'm trying to achieve. 2 options. Option 1 works, option 2 doesn't because of the spaces in the delimiter phrase.

Code: Select all

@echo off
setlocal enabledelayedexpansion

for /f "delims=" %%x in ('mkvinfo.exe "c:\temp\mkv\folder 5\Jellyfish-3-Mbps - Copy (2).mkv" ^| findstr /i "Title"') do (
   set var1=%%x
   set var1=!var1:^| + Title: =!
   echo !var1!
)

for /f "delims=^| + Title: " %%x in ('mkvinfo.exe "c:\temp\mkv\folder 5\Jellyfish-3-Mbps - Copy (2).mkv" ^| findstr /i "Title"') do (
   echo %%x
)



Basically the output from mkvinfo is this:

Code: Select all

| + Title: Jellyfish-3-Mbps: Copy (2)


I need to get the bit after the first colon (so lose '| + Title: '), so in this case Jellyfish-3-Mbps: Copy (2). I can't use colon as a delimiter because it breaks on the colon actually in the file. Plus, some files may contain multiple colons, so I can't use %%x and %%y.

What's the best way?

Thanks!

ldoodle
Posts: 18
Joined: 26 Sep 2015 04:01

Re: For Loop.. delims

#2 Post by ldoodle » 26 Sep 2015 04:33

Ideally you could just delim at the first occurrence, so the first colon after title, then anything before is token 1 and anything after is token 2.

Can you do that?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: For Loop.. delims

#3 Post by foxidrive » 26 Sep 2015 04:59

It helps if you explain what you want to do rather than giving code which isn't functional.

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: For Loop.. delims

#4 Post by ShadowThief » 26 Sep 2015 06:00

Use the tokens option with the * value to split the string on the first colon and then include later colons in the second token.

Code: Select all

for /F "tokens=1,* delims=:" %%x in ('mkvinfo.exe "c:\temp\mkv\folder 5\Jellyfish-3-Mbps - Copy (2).mkv" ^| findstr /i "Title"') do set title=%%y

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: For Loop.. delims

#5 Post by foxidrive » 26 Sep 2015 06:14

Basically the output from mkvinfo is this:

Code: Select all

| + Title: Jellyfish-3-Mbps: Copy (2)


Plus, some files may contain multiple colons


What do you see when there are multiple colons?

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: For Loop.. delims

#6 Post by ShadowThief » 26 Sep 2015 13:40

Also, it's worth noting that file names in Windows cannot include colons, so unless you're pulling these from a Linux machine, you really have nothing to worry about there.

ldoodle
Posts: 18
Joined: 26 Sep 2015 04:01

Re: For Loop.. delims

#7 Post by ldoodle » 26 Sep 2015 16:15

ShadowThief wrote:Use the tokens option with the * value to split the string on the first colon and then include later colons in the second token.

Code: Select all

for /F "tokens=1,* delims=:" %%x in ('mkvinfo.exe "c:\temp\mkv\folder 5\Jellyfish-3-Mbps - Copy (2).mkv" ^| findstr /i "Title"') do set title=%%y


Thanks, that helped a lot :)

ldoodle
Posts: 18
Joined: 26 Sep 2015 04:01

Re: For Loop.. delims

#8 Post by ldoodle » 26 Sep 2015 16:16

ShadowThief wrote:Also, it's worth noting that file names in Windows cannot include colons, so unless you're pulling these from a Linux machine, you really have nothing to worry about there.


Yeah, sorry. I didn't mean in the filename. Title is a meta data property of MKV files.

ldoodle
Posts: 18
Joined: 26 Sep 2015 04:01

Re: For Loop.. delims

#9 Post by ldoodle » 26 Sep 2015 16:17

foxidrive wrote:
Basically the output from mkvinfo is this:

Code: Select all

| + Title: Jellyfish-3-Mbps: Copy (2)


Plus, some files may contain multiple colons


What do you see when there are multiple colons?


In this case it would be just 'Jellyfish-3-Mbps', so between the 2 colons.

Post Reply