Nested loop in batch file help

June 13, 2009 at 20:13:07
Specs: Windows XP PRO, P4 2.8 $GB
I am testing relative performance and configuration of Hard Drives in different PC and through different network environments.

To do this I am wanting to create some known constants of my own, as well as some learning along the way.

As a part of this I am trying to make a nested loop in a Batch CMD file but without success.

Can you please advise me as to what I am missing?

I am trying to create a directory with multiple sub-directories, each of these sub-directories containing multiple small files. The sub-directories and file names can just uniquely numbered.

This will give me a large chunk of small files to handle and test with. Later I will do the same creating larger size files and will then be able to create a more realistic test.

So far I have three lines that work individually at least in part but I cannot get then to work together.

Here is what I have so far:

Create a directory and make it current.

FOR /L %%f in (1,1,1000) do md %%f

This line will create the sub-directories as intended

FOR /L %%g in (1,1,1000) do echo abc >

This line will create the 1000 files as intended but only in the root directory, it does not recurse through all of the sub-directories to continue making the files in each of them.

FOR /R %%h in (1,1,1000) do echo cba >

Using the /R instead of /L I am able to get the CMD file to recurse through but it only created two files in each directory. one is named and the second is

How do I wrap the recursing ability of the third line around the working function in line two?

Alan Smedley

See More: Nested loop in batch file help

Report •

June 13, 2009 at 22:58:42
I do not know what your performance expectations are. But I
had made a similar program using BASIC to test the creation
of CD-ROM from hard disk data files of relatively small files
with a directory structure 8 levels deep and file and directory
size of just exceeding one cluster size to maximize slack
space. In a partial trial I found that the time became greatly
dependent on the depth of the path. Copying is extremely
slow when the path is long. About 2 hours to create 1
directory at root plus sub-dirs 8 levels down plus files at the
deepest level.

Report •

June 13, 2009 at 23:17:08
A part of my performance testing is to see the differences in
network access of different virtual machines solutions running
Windows XP compared to a native Windows XP in the same

Currently we are testing VMware and VirtualBox.

Hopefully this will allow us to decide if reality we are needing
to change the network over from 10/100 to a 10/100/100. The
application that is running is an in house developed database
that mostly small data packets. Or if the performance and
access issues are just a lack of grunt in the terminal PC's

Alan Smedley

Report •

June 14, 2009 at 03:27:52
@echo off

FOR /L %%f in (1,1,1000) do (
md %%f
echo abc > %%f\abc

If at first you don't succeed, you're about average.


Report •

Related Solutions

June 14, 2009 at 05:04:39
Thanks for the heads up on how to get this working, its brilliant and worked a treat.

So what I now have is as follows:

md ABC
cd ABC
for /L %%f in (1,1,1000) do (
md %%f
cd %%f
for /L %%g in (1,1,999) do echo %%g>
cd ..

And the output is a directory called ABC that has 1000 subdirectories each with 999 files that contain the number of the file this giving me a variable file length to also play around with in my testing.


Alan Smedley

Report •

Ask Question