Go Back   TechArena Community > Technical Support > Computer Help > Windows XP > Windows XP Support
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read SiteMap

Tags: , , , , ,

Sponsored Links



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

Windows XP Support


Reply
 
Thread Tools Search this Thread
  #1  
Old 28-05-2009
dtronvig
 
Posts: n/a
Can't get DOS "for" loop to return full filenames with spaces

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
Reply With Quote
  #2  
Old 28-05-2009
Twayne
 
Posts: n/a
Re: Can't get DOS "for" loop to return full filenames with spaces

dtronvig wrote:
> 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`



Reply With Quote
  #3  
Old 29-05-2009
dtronvig
 
Posts: n/a
Re: Can't get DOS "for" loop to return full filenames with spaces


Thanks Twayne.

Will do.


"Twayne" wrote:

> dtronvig wrote:
> > 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`
>
>
>
>

Reply With Quote
  #4  
Old 29-05-2009
John John - MVP
 
Posts: n/a
Re: Can't get DOS "for" loop to return full filenames with spaces

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

dtronvig wrote:
> 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

Reply With Quote
  #5  
Old 29-05-2009
dtronvig
 
Posts: n/a
Re: Can't get DOS "for" loop to return full filenames with spaces

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
Reply With Quote
  #6  
Old 29-05-2009
VanguardLH
 
Posts: n/a
Re: Can't get DOS "for" loop to return full filenames with spaces

dtronvig wrote:

> 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.
Reply With Quote
  #7  
Old 29-05-2009
dtronvig
 
Posts: n/a
Re: Can't get DOS "for" loop to return full filenames with spaces

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

"VanguardLH" wrote:

> dtronvig wrote:
>
> > 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.
>

Reply With Quote
  #8  
Old 29-05-2009
John John - MVP
 
Posts: n/a
Re: Can't get DOS "for" loop to return full filenames with spaces

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


dtronvig wrote:
> 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

Reply With Quote
  #9  
Old 02-06-2009
Richard
 
Posts: n/a
Re: Can't get DOS "for" loop to return full filenames with spaces

"dtronvig" <dtronvig@discussions.microsoft.com> wrote in message
news:9235055D-FC99-475D-B25F-C283FC237B72@microsoft.com...
> 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



Reply With Quote
Reply

  TechArena Community > Technical Support > Computer Help > Windows XP > Windows XP Support


Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Can't get DOS "for" loop to return full filenames with spaces"
Thread Thread Starter Forum Replies Last Post
"Access denied" error in a subfolder that the user has "full-control skulmat Active Directory 1 19-10-2009 10:48 AM
Type.GetTypeFromProgID("WMSServer.Server") return null michaelitrn MediaCenter 2 25-02-2009 02:31 AM
Install Copy Error!! Filenames on Install disk missing ".ex_" Westbrook Windows XP Support 3 02-02-2008 08:58 AM
SMTP command "xexch50" with "504 Need to authenticate first ". The full command sent was "XEXCH50 1020 2 ". ecscomp Small Business Server 3 08-12-2006 01:24 PM
Uninstall "Microsoft Update" and return to "Windows Update" jabarnut Windows Update 14 13-07-2005 09:28 AM


All times are GMT +5.5. The time now is 07:57 AM.