command line help needed!

B

BigDaddyCool

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?
 
P

Pegasus \(MVP\)

BigDaddyCool said:
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
 
B

BigDaddyCool

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
 
P

Pegasus \(MVP\)

See below.

BigDaddyCool said:
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
)
 

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