Windows – How to create a list of named folders in Windows automatically

batch filescriptwindowswindows 7

So, I am studying an online course which has hundreds of sub lectures. I need to download the source code for each lecture in a corresponding sub-folder. With over 120 lectures, creating a sub-folder one by one is a painstaking process.

Here's my current folder structure:

enter image description here

Is there a way I can create all these sub-folders at once, along with the proper name, through a batch file or something similar.

Best Answer

How can I can create all these sub-folders at once, using my naming scheme?

If I were to create the sub-folders in a specific directory, such as C:\Dropbox\Development, would I need to cd to that directory first? Assuming I'm using the cmd shell?

To create the sub-folders (sub-directories) in a specific directory (that is not the current directory), you can do one of the following:

  • cd C:\Dropbox\Development first or
  • Change the md Lec-%%i command to md C:\Dropbox\Development\Lec-%%i.

Note:

  • mkdir is a synonym for md and can be used in its place.

Below I show both alternatives, first from a cmd shell (command line), and second using a batch file.

As a bonus (although not asked for in the original question) there is a bash shell alternative as well.


From a cmd shell:

cd C:\Dropbox\Development
for /l %i in (9,1,120) do md Lec-%i

or

for /l %i in (9,1,120) do md C:\Dropbox\Development\Lec-%i

From a batch file:

@echo off
cd C:\Dropbox\Development
for /l %%i in (9,1,120) do md Lec-%%i

Or

@echo off
for /l %%i in (9,1,120) do md C:\Dropbox\Development\Lec-%%i

Notes:

  • 9 is the start number. Change if necessary.
  • 1 is the step. Do not change this.
  • 120 the end number. Change if necessary to the number of the last directory you require.
  • To create files in another directory, you can either

    • cd C:\Dropbox\Development\Lec-%%i first or
    • change the md command to md C:\Dropbox\Development\Lec-%%i.

Is there a way to do a similar thing for Mac OSX from the Mac terminal?

From a bash shell:

for i in {9..120}; do mkdir Lec-$i; done; 

Or (for a more portable version)

for i in `seq 9 120`; do mkdir Lec-$i; done;

Further Reading

Related Question