How can I create a list of named folders in Windows automatically?

35,960

Solution 1

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

Solution 2

There is another easy way, for limited number of folders. May be Useful here Or for someone else.

In Windows we can make numbered folder names by creating a folder "lec(1)" and copy pasting it how many time we want, if we paste 10 time there will be 11 folders with names "lect(1)" to "lec(10) - Copy"

Only trick here is that the first folder must include parentheses (n), where n is the number from where numbering starts.

Windows includes "- copy" at the and of pasted folder name "lec(1) - Copy" :(

If you don't like it, just select all and rename first lec(1) -> lec-(1) or anything.

All folder's names will be adjusted and "- copy" will be removed ;)

  • Ctrl+C - Copy
  • Ctrl+V - Paste
  • F2 - rename
  • Enter - to finish renaming.
  • ESC- to cancel renaming.
  • Ctrl+A or Ctrl+UP to select folders.

enter image description here

Solution 3

This won't be better than a script for your particular scenario, but it's kind of nice to know this when your folder names are unrelated: you can make multiple directories from the command line by separating them by a space:

C:\temp\animals>dir
 Volume in drive C is Windows
 Volume Serial Number is 82CB-BB0F

 Directory of C:\temp\animals

11/16/2015  03:55 PM    <DIR>          .
11/16/2015  03:55 PM    <DIR>          ..
               0 File(s)              0 bytes
               2 Dir(s)  1,636,846,243,840 bytes free

C:\temp\animals>mkdir cats dogs penguins

C:\temp\animals>dir
 Volume in drive C is Windows
 Volume Serial Number is 82CB-BB0F

 Directory of C:\temp\animals

11/16/2015  03:55 PM    <DIR>          .
11/16/2015  03:55 PM    <DIR>          ..
11/16/2015  03:55 PM    <DIR>          cats
11/16/2015  03:55 PM    <DIR>          dogs
11/16/2015  03:55 PM    <DIR>          penguins
               0 File(s)              0 bytes
               5 Dir(s)  1,636,846,178,304 bytes free

Solution 4

You might find that if you create numbered folders named like this, they won't display in the proper (expected) sort order:

C:\Dropbox\Development> dir /b
Lec-10
Lec-100
Lec-101
Lec-102
Lec-103
Lec-104
Lec-105
Lec-106
Lec-107
Lec-108
Lec-109
Lec-11
Lec-110
Lec-111
Lec-112
Lec-113
Lec-114
Lec-115
Lec-116
Lec-117
Lec-118
Lec-119
Lec-12
Lec-120
Lec-13
Lec-14
Lec-15
Lec-16
Lec-17
Lec-18
Lec-19
Lec-20
Lec-21
...
Lec-89
Lec-9
Lec-90
Lec-91
...

The problem is, the number portion of the foldernames is variable width, which affects how the folders are sorted.

If it's important to you that the folders are sorted properly, use the batch file below to create folders with the numbered portion of the foldernames padded with "0's" (zero's) so that all numbers are the same length, like this:

C:\Dropbox\Development> dir /b
Lec-009
Lec-010
Lec-011
Lec-012
Lec-013
Lec-014
Lec-015
Lec-016
Lec-017
Lec-018
Lec-019
Lec-020
Lec-021
...

Here is the batch file:

@echo off

for /L %%f in (9,1,120) do call :work 000%%f
goto :EOF


:work

set "dx=%~1"
set "dx=%dx:~-3%"

md "C:\Dropbox\Development\Lec-%dx%" >nul 2>&1

set "dx="
goto :EOF
Share:
35,960

Related videos on Youtube

Manish Giri
Author by

Manish Giri

I love fiddling with code. I like to solve problems, build interesting things, and I'm constantly amazed by the power you get with the knowledge of coding. Currently working as an SDE-2 at Dell Technologies. Graduated with a Masters in Computer Science in December 2020 from the University of Cincinnati. Okay, seeing as I'm generally not very good with "About Me's", gonna stop here. Feel free to hit me up through any of the links on the right. Oh, and there's something I very strongly believe in - “Everybody in this country should learn to program a computer, because it teaches you how to think” - Steve Jobs

Updated on September 18, 2022

Comments

  • Manish Giri
    Manish Giri over 1 year

    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.

    • Admin
      Admin over 8 years
      @WesSayeed I'm not sure what you mean. Wouldn't that just delete all the folders? I'm looking to create folders from 9 all the way to 122(or how many ever the number of lectures that are there).
    • Admin
      Admin over 8 years
      Since you specifically mention batch and script files I won't post this as an answer, but real super users know that it's much easier to just get the right tool for the job instead of writing your own ;). Just use a renaming tool and you can probably figure this out in less than a minute, including the download. I've personally used Rename Master and Bulk Rename Utility (contains ads) and both can handle this and many other annoying file and folder operations. (I am not affiliated with either tool.)
    • Admin
      Admin over 8 years
      Use a program like getright for downloading. It has a free version. Each file should be downloaded to its proper home.
  • Manish Giri
    Manish Giri over 8 years
    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? And many thanks!
  • LPChip
    LPChip over 8 years
    @Manish yes, you'd need to cd to the folder first.
  • DavidPostill
    DavidPostill over 8 years
    No. Just change the md Lec-%%i to md C:\Dropbox\Development\Lec-%%i
  • DavidPostill
    DavidPostill over 8 years
    It is nice of you to point this out, but it doesn't answer the question...
  • TTT
    TTT over 8 years
    @DavidPostill. I agree, which is why I worded it the way I did. But it's MUCH better than creating folders 1 by 1. Perhaps I should delete the answer and make it a comment?
  • LPChip
    LPChip over 8 years
    +1 for teaching me something I didn't know. :)
  • Kevin Fegan
    Kevin Fegan over 8 years
    The animation (".gif") is rather a nice touch, but repeating it over and over and over and over... endlessly, is very annoying.
  • phyrfox
    phyrfox over 8 years
    @KevinFegan I disagree. Looped GIFs makes sure the user (you) get to actually see the animation without reloading the page and scrolling down quickly to try and see it. Most browsers don't offer GIF control buttons.
  • mothmonsterman
    mothmonsterman over 8 years
    awesome answer and just stop watching after you get the point!
  • afrazier
    afrazier over 8 years
    How they sort is tool-dependent. Explorer (and most other file managers) will sort them intuitively, with Lec-10 coming after Lec-9.
  • user541686
    user541686 over 8 years
    Instead of md why not use mkdir on Windows? That way it's consistent with *nix and one less difference for people to remember.
  • DavidPostill
    DavidPostill over 8 years
    @Mehrdad I will add a note to that effect.
  • Gil Epshtain
    Gil Epshtain about 6 years
    This will not work if you don't want parenthesis in your folder name. For example I can create a list of folders: x(0), x(1), x(2), ... x(N). But i can NOT create a list: x0, x1, x2, ... xN
  • Gil Epshtain
    Gil Epshtain almost 6 years
    There is a bug in your script. You need to use % sign only once (not twice). Therefore the command need to be for /l %i in (9,1,120) do md Lec-%i
  • DavidPostill
    DavidPostill almost 6 years
    @GilEpshtain Well spotted. Corrected answer.
  • phuclv
    phuclv almost 6 years
    Explorer uses natural sort so 2 will be put before 10 as one would expected (if you look at the OP's screenshot 9 is also sorted before 10). Only dir suffers from that problem and needs 0 paddings, but the OP didn't know much about the command line so I don't think it'll be a problem
  • Kevin Fegan
    Kevin Fegan almost 6 years
    @phuclv - Yes, as you say, "OP didn't know much about the command line...". That's why I wrote this answer. To let OP know what to expect and to provide a solution (answer). I could imagine OP using DavidPostill's great answer, then to become confused to see dir listing folders out of order. Windows As you and afrazier mentioned, Windows Explorer will sort/list the folders correctly, but the OP was looking for a batch solution which means they will be (most likely) working from the command prompt.
  • jiggunjer
    jiggunjer over 4 years
    This incorrectly assumes the user has the complete folder tree already. There are better solutions that don't require extra and/or unknown software.