Can't get DOS "for" loop to return full filenames with spaces

D

dtronvig

I'm trying to set up a DOS batch file (in XP) to decode all the MP3 files in
a directory using a "for" loop. I just can't figure out how to get the "for"
command to return anything beyond the first space in a filename. To simplify
things, I tried this right in the Command window:

The directory contains files "a 1.mp3", "b 1.mp3" and "c 1.mp3". I enter:

for /f usebackq %f IN (`dir /b *.mp3`) do dir "%f"

The resulting echoed commands are

dir "a"
dir "b"
dir "c"

and of course the resulting directory listings are empty.

So `dir /b *.mp3` is producing the list of full filenames, but the "for"
command is just passing along the filenames up to the first space.

What am I missing?

Thanks,
Drew
 
T

Twayne

dtronvig said:
I'm trying to set up a DOS batch file (in XP) to decode all the MP3
files in a directory using a "for" loop. I just can't figure out how
to get the "for" command to return anything beyond the first space
in a filename. To simplify things, I tried this right in the Command
window:

The directory contains files "a 1.mp3", "b 1.mp3" and "c 1.mp3". I
enter:

for /f usebackq %f IN (`dir /b *.mp3`) do dir "%f"

The resulting echoed commands are

dir "a"
dir "b"
dir "c"

and of course the resulting directory listings are empty.

So `dir /b *.mp3` is producing the list of full filenames, but the
"for" command is just passing along the filenames up to the first
space.

What am I missing?

Thanks,
Drew

Group: alt.msdos.batch.nt
I get it on news.motzarella.org servers but tthere are many
others that carry it too.

Those guys know XP & DOS relations like the back of their hands. Only
switch from "DOS" to Command Prompt" to avoid being corrected and having
the diffs explained to you<g>.

HTH,

Twayne`
 
D

dtronvig

Thanks Twayne.

Will do.


Twayne said:
Group: alt.msdos.batch.nt
I get it on news.motzarella.org servers but tthere are many
others that carry it too.

Those guys know XP & DOS relations like the back of their hands. Only
switch from "DOS" to Command Prompt" to avoid being corrected and having
the diffs explained to you<g>.

HTH,

Twayne`
 
J

John John - MVP

In the current directory:

for %f in (*.txt) do echo "%f"


In a specified directory:

for %f in (C:\FolderName\*.txt) do echo "%f"

for %f in ("C:\Folder Name\*.txt") do echo "%f"


No quotes in echoed results:

for %f in ("C:\Folder Name\*.txt") do echo %f

John
 
D

dtronvig

Hey John,

Yeah, that works. I was seeing a lot of examples that were using a "dir /b"
command to generate a list of files, which the "for /f" command should then
pass on one by one. Instead, "for /f" just passes on the first portion of the
filename up to the first space. I wrote up a text file with long filenames,
put that between the parens and got the same result. Just dropping the /f and
using a simple wildcard filename inside the parens lets the full filename
pass though. Not sure why the more complicated approach doesn't work, but I
won't worry about it.

Thanks,
Drew
 
V

VanguardLH

dtronvig said:
I'm trying to set up a DOS batch file (in XP) to decode all the MP3 files in
a directory using a "for" loop. I just can't figure out how to get the "for"
command to return anything beyond the first space in a filename. To simplify
things, I tried this right in the Command window:

The directory contains files "a 1.mp3", "b 1.mp3" and "c 1.mp3". I enter:

for /f usebackq %f IN (`dir /b *.mp3`) do dir "%f"

The resulting echoed commands are

dir "a"
dir "b"
dir "c"

and of course the resulting directory listings are empty.

So `dir /b *.mp3` is producing the list of full filenames, but the "for"
command is just passing along the filenames up to the first space.

What am I missing?

Thanks,
Drew

You forgot that in a batch file that the replaceable parameter has to
use a doubled percentage sign.

From a command line:
for ... %a in (spec) do ... %a

But in a batch file:
for ... %%a in (spect) do ... %%a

Otherwise, in the batch file, you would be trying to expand the
replaceable parameter as defined in the parent shell that was running
the batch file. You do not want the replaceable parameters evaluated
when the .bat file is opened and interpreted. You want them used when
the commands are *executed*.

Run: for /?

The first paragraph warns you about using the doubled percentage signs
used to delimit a replaceable parameter.

Notice that options are to be enclosed in double-quotes. You did not do
that for the usebackq option.

Another problem is parsing. The output of the fileset (your `dir`
output) uses spaces to delimit each item in the list. Yet you have
spaces in the item's strings in that list. So when the list gets parsed
based on spaces to pipe into the 'do' clause of the 'for' command, each
space-delimited item gets passed out. You might get around this parsing
problem by specifying a different delimiter than space and tab (which
are the defaults) by using the options parameter. As I recall, if the
delims option is used, it must be the last one inside the quoted options
string.

So try in the command line (I used the semicolon but you can pick
something else):

for /f "usebackq delims=;" %f in (`dir /b *.mp3`) do dir "%f"

and in a batch file use:

for /f "usebackq delims=;" %%f in (`dir /b *.txt`) do dir "%%f"

So what does this for-loop give you that you wouldn't get for the same
output just doing the following command?

dir "*.mp3"

You get a lot of duplicated output from performing each 'dir' command by
itself on each file.
 
D

dtronvig

Well, what I was aiming for turned out to be:

for %%a in (*.mp3) do call D:\Programs\Lame\Lame.exe --decode "%%a"

The "do dir" version was just an attempt to clarify what was being passed.

I now know more about "for /F" than I need to at the moment, but I'll be
tempted to use it for more complex operations in the future.

Thanks,
Drew
 
J

John John - MVP

You could have done it in two parts:

dir /b *.mp3 >C:\mp3list.txt

for /f "usebackq delims=" %A in (c:\mp3list.txt) do echo "%A"


You can also use FOR /R if you want to recurse through folders:

for /r C:\windows\ %a in (*.txt) do echo %a

John
 
V

VanguardLH

dtronvig said:
Well, what I was aiming for turned out to be:

for %%a in (*.mp3) do call D:\Programs\Lame\Lame.exe --decode "%%a"

The "do dir" version was just an attempt to clarify what was being passed.

I now know more about "for /F" than I need to at the moment, but I'll be
tempted to use it for more complex operations in the future.

Then it comes down to using the default parsing characters of space and
tab for the 'in' list created by the specified filespec (which, for you,
was the output of a dir command). There were spaces in the items added
to the list, and the default parsing uses the space character. That's
why one of my suggestions was to specify a different delimiter character
to insert between each item returned by the filespec.
 
R

Richard

dtronvig said:
I'm trying to set up a DOS batch file (in XP) to decode all the MP3 files
in
a directory using a "for" loop. I just can't figure out how to get the
"for"
command to return anything beyond the first space in a filename. To
simplify
things, I tried this right in the Command window:

The directory contains files "a 1.mp3", "b 1.mp3" and "c 1.mp3". I enter:

for /f usebackq %f IN (`dir /b *.mp3`) do dir "%f"

The resulting echoed commands are

dir "a"
dir "b"
dir "c"

and of course the resulting directory listings are empty.

So `dir /b *.mp3` is producing the list of full filenames, but the "for"
command is just passing along the filenames up to the first space.

What am I missing?

Thanks,
Drew

Hi Drew, I started a reply, but delayed sending, and it seems that "John
John" and "VanguardLH" are providing workable solutions. My first thought
was that since the * (wildcard) returns filenames with spaces, that you
would need to include quotes around: "*.mp3" in your code string.

Also, you are using the ` (grave accent) character rather than
' (apostrophe) character for single quote, if that makes a difference.

FWIW.
--Richard
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top