13

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.

3
  • @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). Nov 16, 2015 at 21:44
  • 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.)
    – Lilienthal
    Nov 17, 2015 at 18:03
  • Use a program like getright for downloading. It has a free version. Each file should be downloaded to its proper home.
    – JDługosz
    Nov 18, 2015 at 4:18

6 Answers 6

31

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

7
  • 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! Nov 16, 2015 at 21:47
  • @Manish yes, you'd need to cd to the folder first.
    – LPChip
    Nov 16, 2015 at 21:47
  • No. Just change the md Lec-%%i to md C:\Dropbox\Development\Lec-%%i
    – DavidPostill
    Nov 16, 2015 at 21:48
  • Instead of md why not use mkdir on Windows? That way it's consistent with *nix and one less difference for people to remember.
    – user541686
    Nov 18, 2015 at 11:33
  • @Mehrdad I will add a note to that effect.
    – DavidPostill
    Nov 18, 2015 at 11:34
22

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

5
  • 2
    +1 for teaching me something I didn't know. :)
    – LPChip
    Nov 17, 2015 at 8:49
  • The animation (".gif") is rather a nice touch, but repeating it over and over and over and over... endlessly, is very annoying. Nov 17, 2015 at 10:58
  • 4
    @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.
    – phyrfox
    Nov 17, 2015 at 16:27
  • awesome answer and just stop watching after you get the point! Nov 17, 2015 at 17:02
  • 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 Mar 20, 2018 at 9:31
2

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
2
  • 3
    It is nice of you to point this out, but it doesn't answer the question...
    – DavidPostill
    Nov 16, 2015 at 22:01
  • 1
    @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?
    – TTT
    Nov 16, 2015 at 22:50
1

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
3
  • 1
    How they sort is tool-dependent. Explorer (and most other file managers) will sort them intuitively, with Lec-10 coming after Lec-9.
    – afrazier
    Nov 17, 2015 at 18:22
  • 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
    – phuclv
    Jun 3, 2018 at 9:48
  • @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. Jun 13, 2018 at 15:55
0

You could run this batch file in the same folder as the files. Each file will be moved into a folder of the same name.

for /f "delims=" %%a in ('dir /b') do (
if not "%%~fa"=="%~f0" (
md "%%~na" 2>nul
if exist "%%a" move "%%~na.*" "%%~na"
)
)

I use it with a warning below to avoid accidental use.

@echo off
echo.
set sure=
set /p sure=you want to put each file in this folder into their own folders? 
if /i "%sure%"=="" exit
if /i "%sure:~0,1%"=="n" exit
if /i not "%sure:~0,1%"=="y" (exit) else (echo.&echo are you sure? Close window to cancel or..)
echo.&pause 
for /f "delims=" %%a in ('dir /b') do (
if not "%%~fa"=="%~f0" (
md "%%~na" 2>nul
if exist "%%a" move "%%~na.*" "%%~na"
)
)
-1

First of all i didn't read your whole problem...but as far as i can figure it out...you want to rename folders on the basis of list...well there is two method

Method-1 -If you do not have folder list...there is workaround...you need to go to macOS with this Folder tree...and just copy whole folder tree...What macOS will do is they copy content as well as list of your all folders in terms of list as well...now paste in TextEdit or any software similar to Notepad.

-Now I am assuming you having that folder list is with you...you just need one third party software to do the job (Now all the Steps are for Windows) https://www.rename-expert.com -In terms of use this software you had to create new folder, new folder-2, new folder-4 as much as you wanted.

-Now in Rename Expert there is a Option to Rename Folders from the Basis of the List.

-Boom you are done.

Method-2 -If you want to create mirror copy of your whole folder tree without files in it.

-Beyond Compare is the software you need it. https://www.scootersoftware.com/support.php?zz=kb_copyfolderstructure

1
  • This incorrectly assumes the user has the complete folder tree already. There are better solutions that don't require extra and/or unknown software.
    – jiggunjer
    Aug 27, 2019 at 15:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .