PC Review


Reply
Thread Tools Rate Thread

command line help needed!

 
 
BigDaddyCool
Guest
Posts: n/a
 
      20th Apr 2008
Suppose I'm in a directory c:\temp. How do I write a for loop that will
create 26 directories, one for each letter of the alphabet? So like after the
for loop executes, the directory will have 26 folders like this:

c:\temp\a
c:\temp\b
c:\temp\c

....

c:\temp\z


Now, I know that to do this from the command-line, you tupe:

FOR %a IN (A B C) DO MKDIR %a

....but when I copy and paste the command into a file called CreateDirs.cmd
and double click that, it doesn't work. Why does it work at the command line
and not in the .cmd file?
 
Reply With Quote
 
 
 
 
Pegasus \(MVP\)
Guest
Posts: n/a
 
      20th Apr 2008

"BigDaddyCool" <(E-Mail Removed)> wrote in message
news:295194F5-ABD5-48F3-85AB-(E-Mail Removed)...
> Suppose I'm in a directory c:\temp. How do I write a for loop that will
> create 26 directories, one for each letter of the alphabet? So like after
> the
> for loop executes, the directory will have 26 folders like this:
>
> c:\temp\a
> c:\temp\b
> c:\temp\c
>
> ...
>
> c:\temp\z
>
>
> Now, I know that to do this from the command-line, you tupe:
>
> FOR %a IN (A B C) DO MKDIR %a
>
> ...but when I copy and paste the command into a file called CreateDirs.cmd
> and double click that, it doesn't work. Why does it work at the command
> line
> and not in the .cmd file?


.. . . because you must double your % characters when running
the command from within a batch file.
@echo off
for %%a in (A B C) do md %%a


 
Reply With Quote
 
BigDaddyCool
Guest
Posts: n/a
 
      20th Apr 2008
Thank you! It works!

Now, two more questions:

1. Writing out the entire alpohabet on one line is a bit of a pain because I
have to turn Word Wrap off. Is there any way I can extend the for loop onto
the next line without it interpreting the next as a separate command?

2. Suppose I want to do more with each created directory. Is there a way to
turn the for loop into a block of code that I can then work with each created
directory and then move on to the next one? I was thinking of something like
this:

FOR %%a IN (A B C ... Z) DO:
{
MKDIR %%a if it doesn't exists
COPY all files from c:\temp into c:\temp\%%a that start with that letter
go on to the next letter
}

or something like that


"Pegasus (MVP)" wrote:

>
> "BigDaddyCool" <(E-Mail Removed)> wrote in message
> news:295194F5-ABD5-48F3-85AB-(E-Mail Removed)...
> > Suppose I'm in a directory c:\temp. How do I write a for loop that will
> > create 26 directories, one for each letter of the alphabet? So like after
> > the
> > for loop executes, the directory will have 26 folders like this:
> >
> > c:\temp\a
> > c:\temp\b
> > c:\temp\c
> >
> > ...
> >
> > c:\temp\z
> >
> >
> > Now, I know that to do this from the command-line, you tupe:
> >
> > FOR %a IN (A B C) DO MKDIR %a
> >
> > ...but when I copy and paste the command into a file called CreateDirs.cmd
> > and double click that, it doesn't work. Why does it work at the command
> > line
> > and not in the .cmd file?

>
> .. . . because you must double your % characters when running
> the command from within a batch file.
> @echo off
> for %%a in (A B C) do md %%a
>
>
>

 
Reply With Quote
 
Pegasus \(MVP\)
Guest
Posts: n/a
 
      20th Apr 2008
See below.

"BigDaddyCool" <(E-Mail Removed)> wrote in message
news:97D69829-16D3-409A-B9BC-(E-Mail Removed)...
> Thank you! It works!
>
> Now, two more questions:
>
> 1. Writing out the entire alpohabet on one line is a bit of a pain because
> I
> have to turn Word Wrap off. Is there any way I can extend the for loop
> onto
> the next line without it interpreting the next as a separate command?


Yes, you can:
@echo off
echo A B C D ^
D E F

> 2. Suppose I want to do more with each created directory. Is there a way
> to
> turn the for loop into a block of code that I can then work with each
> created
> directory and then move on to the next one? I was thinking of something
> like
> this:
>
> FOR %%a IN (A B C ... Z) DO:
> {
> MKDIR %%a if it doesn't exists
> COPY all files from c:\temp into c:\temp\%%a that start with that letter
> go on to the next letter
> }
>
> or something like that


You were very close with your approach!
FOR %%a IN (A B C ... Z) DO (
if not exist %%a md %%a
copy /y c:\temp\*.* c:\temp\%%a
)

Best to indent lines inside a loop, for improved readability.

And since you're so interested in this stuff, here are two
approaches that will work too:

if not exist c:\temp\%%a (md c:\temp\%%a) else (echo %%a exists!)

or alternatively:
if exist c:\temp\%%a (
echo %%a exists!
) else (
md c:\temp\%%a
)


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Regexp needed to parse DOS command line David Mayerovitch Windows XP Help 0 23rd Nov 2006 06:29 AM
help needed with command line linker invocation a_marlow Microsoft VC .NET 1 11th Oct 2006 09:16 AM
RE: Command Line Switch needed =?Utf-8?B?Sm9lIE1vbm5pbg==?= Microsoft Outlook 0 10th Feb 2005 06:57 PM
IE6 Command Line Info Needed... Marvin Miller Windows XP Internet Explorer 1 14th Oct 2004 12:47 AM
Command Line Switch needed foxman Microsoft Outlook 2 28th Sep 2004 08:57 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 04:54 PM.