I think this is an easy question...

Hewlett-packard / Iq505
January 25, 2009 at 10:58:34
Specs: Windows Vista , Dual Core/4GB
I am working in a TEST directory and I have been trying to make a batch file that can read a text file and create folders named by each line of the text file. For example, if the text file was named Red.txt and had the following data in it...


then my batch file would have to make these folders


...now I decided to be bold and try to use the infamous FOR command... but alas... I fell into the dirt and ate an earthworm when I tried it...

This is the code that I thought would work...
::Batch to Make Directories
@echo off
@FOR /F "tokens=* delims=" %%G IN (*.txt) DO MD %%G
::end of Batch

Well.... like I said this batch file just giggled immensely when I double clicked it and did absolutely nothing on my computer. So... I defiantly decided to try again and I entered the following code instead...
::Batch to Make Directories
@echo off
@FOR /F %1 IN (*.txt) do MD (%1)
::end of Batch

Again, no deal...
Can anyone please tell me what I have done wrong? Thanks guys.

See More: I think this is an easy question...

Report •

January 25, 2009 at 13:46:31
What you have done wrong is to miss to refer to your file in the For clause

:: Batch to Make Directories
@echo off
pushd C:\TEST
for /F "tokens=* delims=" %%G in (Red.txt) do MD %%G
:: end of Batch

Avoid to prefix statements with @: @echo off suppresses screen echoing for the whole scope of batch.

Report •

January 25, 2009 at 14:18:40
Hi IVO, Doesn't the (*.txt) gather all of the text files in the current directory for the actions requested in the batch file? ...or are wildcards not allowed? :(

Report •

January 25, 2009 at 14:24:00
Sorry IVO, I have one more quick question as well... why is the pushd and the popd command needed for this one?

Report •

Related Solutions

January 25, 2009 at 14:31:03
For /F and plain For are different animals.

In plain For is right what you say, but the clause of For /F doesn't allow wildcards as it must contain

- the name of one file
- a command enclosed by single quotes (')
- a string enclosed by double quotes (")

Type For /? to know more.

Report •

January 25, 2009 at 14:38:15
Issue pushd C:\TEST to be sure your current directory is the one you select as the parent to create the subfolders any directory you are in when starting the batch.

Popd resumes your starting one.

Just a plus to make the script more professional.

Report •

January 25, 2009 at 14:42:56
I was afraid that you might say something like that. lol. Trust me I have spent a lot of time digging through the For /? in the command prompt window.

Unfortunately the explanations on this particular command are somewhat elusive to my less than nimble brain and I just can't seem to wrap my mind around what half of it means. Maybe its the black screen.... Thanks a caboodle for the pointers.

Report •

January 25, 2009 at 14:48:15
aha! So that is the secret of the push and pop commands. Thank you a million times. I will chop out the @ symbol in all lines after the first one and I will start pushing and popping directories!

Report •

January 25, 2009 at 16:15:42
I you want to process all files with a for /f loop using a wild card '2>nul type *.txt'.

As Ivo points out for /f can take commands, the are a couple of exceptions though. The command can't be a for loop or a :label call.

The prefixing @ suppresses echoing, it is of no use when echo is already off.

The for loop can be a little daunting for beginers i.e 'for %%a in (*beginers) do be daunting'. A for loop always works over a finite set, it can be files, words, directories, lines in a text file or lines output by a command. The for loop just does the same thing to every piece of the set 'I have 1000 toys and for each one I had to write my name on the back so it doesn't get stolen.' 'for %%a in (*toys) do write name'

Tokens and delimiters are just breaking up each unit(mostly lines) into smaller chunks. The delimiter states what goes between the chunks and the tokens specify which chunks you want, * specifies all tokens or the rest of the line and a null delimiter can also be specified, in effect they give the same result the whole line.

for /f "tokens=1 delims= " %%a ....
test line with delimiters


for /f "tokens=2,4 delims= " %%a ....
    token1        token2 
      |             |
test line with delimiters
    |    |    |


for /f "tokens=2-4 delims= " %%a ....
 token1   token2  token3
      |     |       |
test line with delimiters
    |    |    |


for /f "tokens=1,* delims= " %%a ....
token1  -token2- 
 |      ||  | ||
test line with delimiters
    |    |    |

%%b=line with delimiters

As you can see the variables auto increment if use tokens correctly. Obviously I can't explain it all here, there are a great many websites with information.

If you don't like reading the command line help because of the black screen, font, ect. Give this batch a try. It makes a html document out of all the help so it can be read with your web browser.

Report •

January 25, 2009 at 16:33:32
Wow! Thanks a zillian Judago! I am basking in the sunrise of actually beginning to understand how the FOR command works! I will nibble away at the help file that you have given me and I will try to use it on my other question which so far remains as a lonely weekend posting for the time being... Just one question though, If I am understanding the explanation correctly then why didn't the Make Directory command start with %%a instead of %%G
Do they not have to go in order of their occurence as successive "token chunks" in the statement?

Report •

January 25, 2009 at 16:39:29
...arrgh after I sent that post I changed the variable from %%G to %%a in my batch file and tried it out and lo and behold... It worked! So I am assuming that the letter that I use for the first variable does not matter. I know about the 26 variable limit for the batch file. :) Well hats off to both of you friends for your great help and I certainly have a mountain of reading ahead of me now. I hope I have enough mental power to memorize these lovely batch recipes. Thanks again! I will definitely be saving this post. Judago you should write up a tutorial. Your instructions were very clear... I couldn't grasp the little token image pointers at first but after a second take, I really think I understand it now... I will run off and test my batch powers. =)

Report •

January 25, 2009 at 20:53:15
instead of (Red.txt) can't we use ('type *.txt') ?

Instead the matter of fact there should be a blank line in the end of all text files, but I am sure there must be a way to cope up with that.

Report •

January 25, 2009 at 21:24:50
It started at %%G because that was the first variable specified. The first variable is always explicitly specified, this is the first occurrence of the variable in the for command. An example
for /f %%e in (textfile) do stuff

This tells the for command that you want the variable to be %%e. If their are multiple tokens involved %%f will be the next and so on until %%z is used.

Also be aware that for loop variables are case sensitive so %%e and %%E are two different variables that don't conflict each other.

This is especially important when nesting(making the "do" part of the command another for loop) for loops because when nested the variable remains active so using the same variable again might have undesirable results.

@echo off
for /l %%a in (0,1,9) do (
for /l %%b in (9,-1,0) do (
echo %%a %%b

Good point, I forgot about files that don't end in a linebreak. Might just be easier to just use:

for %%a in (*.txt) do for /f "usebackq" %%b in ("%%a") do commands

Report •

Ask Question