tags:

views:

207

answers:

2

The following is a batch script that calls mp3splt (a program that splits mp3s http://mp3splt.sourceforge.net/mp3splt_page/home.php) into partitions of 5 mins long.

for /f %%a IN ('dir /b *.mp3') do call c:\PROGRA~1\mp3splt\mp3splt -t 5.0 -o output\@f+-+@n+-+@t  %%a

It breaks when the mp3 files contain a space. Can anyone propose a workaround?

+1  A: 

You can quote the variable containing the file name:

"%%a"
Peter Mortensen
That's not enough :-)
Joey
+2  A: 
for %%a IN (*.mp3) do (
    call c:\PROGRA~1\mp3splt\mp3splt -t 5.0 -o output\@f+-+@n+-+@t "%%a"
)

What others are saying here to quote the variable is doubtless correct. That's needed for it to work correctly. But it also is only half the solution.

Your problem here is that dir /b spits out lines of text, each one containing a single file name, like so:

foo bar.mp3
baz gak.mp3
track01.mp3
...

for /f on the other hand is made for reading files or something else (the dir output in this case) line by line ... but it also does tokenizing. That means it splits strings at configurable places into individual tokens.

By default, /F passes the first blank separated token from each line of each file. Blank lines are skipped. You can override the default parsing behavior by specifying the optional "options" parameter. This is a quoted string which contains one or more keywords to specify different parsing options. —help for

So you only get the part before the first space of the file name. You can work around that by using

for /f "delims=" in ...

to specify that tokenizing should be done with no delimiters, essentially giving you the complete line each time. But a better way is actually the one I outlined at the start to just use the regular for to iterate over the files. No surprises with spaces in file names there and it actually makes your intent a lot clearer.

I am regularly surprised why people tend to choose a convoluted and wrong solution over a simple, clear and correct one.

Joey
nice answer, ty!
Rubens Farias