32

I need 100 or more folders named like such: ch.000, ch.001, ch.002, etc. In this case I need it to go up to ch.094 but I will need to create more folders later. That may be more or less folders, but definitely between 000 and 999. I don't know anything about programming, so please guide me through it.

Here is an example of what I need to do.

Screenshot of folders ch.000 to ch.012

Thank You!

TRiG
  • 1,310
  • 1
  • 15
  • 33
Safwan Abrar
  • 472
  • 1
  • 4
  • 8
  • 30
    Do not use Cygwin, it's terrible. – OrangeDog Oct 08 '19 at 22:06
  • 1
    Once or multiple times? Please [edit] that into your question, because the answers will differ greatly. – Jan Doggen Oct 09 '19 at 14:30
  • 5
    @OrangeDog - cygwin is just fine for many tasks (and for a long time was the only good option for some tasks) though slow for many common operations following unix idioms (that would be fast on Unix-a-like OSs) because of how expensive creating a new process is in Windows. And for this task there are decent built-in options in modern Windows as evidenced by the answers here. – David Spillett Oct 09 '19 at 16:41
  • 3
    @DavidSpillett cygwin is worse than other available solutions at pretty much everything. If you want a Unix-like environment in Windows then use either WSL or MSYS. – OrangeDog Oct 09 '19 at 16:51
  • 6
    @OrangeDog - while it certainly isn't the right tool for just this job, Cygwin still has advantages over both WSL and MSYS (in fact MSYS is in part a fork of Cygwin, but with different goals and therefore focus). This question is not the place to discuss the difference at length though, as the powershell (or, if you must, .bat) solutions are by far preferable to installing Cygwin/MSYS/WSL/similar for just this task. – David Spillett Oct 09 '19 at 17:23
  • The term `cygwin` as such is not an answer. You run bash in cygwin (or msys2 or wsl) to create the folders and need the according syntax. – Bernhard Döbler Oct 10 '19 at 15:13
  • It would be silly to install `cygwin` just for this one task, but it is indeed a great tool for linux-compatibility across many versions of windows (not just win10). – jrw32982 Oct 14 '19 at 18:30
  • Hyperbolic nonsense...Cygwin is miles from terrible. I use Windows as my main system but for command line it's gotta be *nix/bash. I have easy remote access to multiple linux instances and WSL running always yet I willingly use Cygwin for 90% of the things I want to do (a lot of things including scripting, system maintenance, routine file system operations, vim, etc. etc.). – B Layer Mar 16 '20 at 04:48

5 Answers5

50

Create a .bat file inside the folder in which to create these sub-folders, and copy inside the following text:

@echo off
setlocal enableDelayedExpansion
FOR /l %%N in (1,1,94) do (
    set "NUM=00%%N"
    set "DIRNAME=ch.!NUM:~-3!"
    md !DIRNAME!
)

Double-click the .bat file and it will create the required chapters.

In the future, if you wish to create for example numbers 95 to 110, just change the FOR line to:

FOR /l %%N in (95,1,110) do (
harrymc
  • 455,459
  • 31
  • 526
  • 924
36

Here's a PowerShell script:

for ($i=1; $i -lt 95; $i++) {
  $name = [string]::Format("ch.{0}", $i.ToString("000"));
  New-Item -Path "c:\source\temp" -Name $name -ItemType "directory"
}

Assuming you're on Windows, you can do Start > Run > "powershell.exe" > OK, then copy/paste this to the command line.

Note that you'll want to change c:\source\temp to the directory where you want the folders, and you can adjust the range to be created by adjusting the values in the for statement, where you see 1 and 95.

OrangeDog
  • 1,191
  • 12
  • 25
Adam Prescott
  • 1,328
  • 11
  • 12
  • 9
    If you're going for a one-liner, make it tight! :D `for ($i=1; $i -lt 95; $i++) {md {'c:\rootFolder\ch.{0:d3}' -f $i}}` – Keith Miller Oct 08 '19 at 22:36
  • 26
    @KeithMiller Don't forget the sequence operator! `1..94 | %{md {'c:\rootFolder\ch.{0:d3}' -f $_}}` – Bob Oct 09 '19 at 00:12
  • 28
    Oh, SuperUser suddenly becomes CodeGolf! :D – Ruslan Oct 09 '19 at 11:22
  • 21
    Don't forget to move the spaces! And you have the wrong brackets in there. **PowerShell**, 42 bytes... [Try it online!](https://tio.run/##K8gvTy0qzkjNyfn/31BPz9KkRrU6N0VDPdkqpig/v8QtPycltSgmOUOv2sAqxbhWXTdNJV6z9v9/AA "PowerShell – Try It Online") `1..94|%{md('c:\rootFolder\ch.{0:d3}'-f$_)}` – KGlasier Oct 09 '19 at 13:48
  • 11
    This would be a lot better as not a one-liner – OrangeDog Oct 09 '19 at 16:53
  • Thank you for the laugh. Love the one liners. Always a fun game – user319862 Oct 10 '19 at 03:16
  • One liner != Code golf. But hey, no harm done ;-) – Culme Oct 10 '19 at 09:13
  • For new installations the default shell is PS, so you can simply right-click on Start to get to it – OrangeDog Oct 10 '19 at 09:21
  • @OrangeDog: With code in multl-ine format, the semicolon trailing the `$Name = ...` is unnecessary. – Keith Miller Oct 12 '19 at 02:42
14

I believe there now is a Linux subsustem in Windows (I've never used it - in fact, I don't use Windows at all), so you could use a bash script - type it on the command line in a bash shell (is that the term in windows? - and note that '$' is the bash-prompt):

$ for i in $(seq -w 1 100)
> do
> mkdir ch.$i
> done

Personally I think it looks better than the powershell version - not least because you can split commands that take a block, over several lines.

j4nd3r53n
  • 327
  • 2
  • 8
  • 4
    You can write loops over multiple lines just as well in PowerShell. No idea why people write completely weird non-idiomatic solutions. – Voo Oct 09 '19 at 14:47
  • Indeed, no need if you know PowerShell, but I found that interesting and helpful, since I use Linux all the time, and didn't know about the `seq` command. – Mark Stewart Oct 09 '19 at 16:46
  • 11
    It seems the OP wanted the numbers to start with 0, not 1. Though you could do `seq -w 0 099`, it would be simpler and easier to do `mkdir ch.{000..099}` than to use a loop. – JoL Oct 09 '19 at 20:04
  • @JoL Actually, {000..099} loses the leading zeros (in ksh) - is there a way around that? – j4nd3r53n Oct 10 '19 at 13:55
  • 1
    @j4nd3r53n don't use ksh. both bash and zsh handle this correctly. – flaviut Oct 10 '19 at 15:40
  • 1
    @j4nd3r53n Well, if ksh doesn't support it, you can still use `seq -w 000 099`. You can use the loop with that, or if you really want to avoid the loop, you can do something like `mkdir $(printf 'ch.%s\n' $(seq -w 000 099))` – JoL Oct 10 '19 at 15:44
  • The Windows Subsystem for Linux requires additional installation while PowerShell is built-in. Not that it invalidates your advice, but it's something to keep in mind if you wish to provide Linux-y advice to Windows users again in the future. – ooa Oct 11 '19 at 08:47
  • @JoL You should really post that as an answer, it's currently ten times more succinct than any other, and I didn't think that would be possible when I saw the the above `for` loop. – Hashim Aziz Oct 11 '19 at 10:44
  • 1
    @Hashim Done. Though, I doubt it'll get any attention at this point. – JoL Oct 11 '19 at 15:03
8

The free Total Commander can create multiple folders with one command since version 9.10 (2017).

Press F7 to open the Create Directory dialog, then enter

<1-99>ch.[C:3]

as shown below, and it will create the folders you need.

Total Commander New Folder dialog with counter

The complete syntax is as follows, with begin, step and width being optional:

<(counterstart)-(counterend)>sometext[C(begin)(+-)(step):(width)]sometext

Previous answer:

It also has a GUI for renaming things, useful if you already have the right number of directories but with wrong names (such as by copy-pasting lots of empty dirs).

Select the folders and open Files -> Multi-Rename Tool. The image shows settings that rename all folders to the scheme you want.

enter image description here

Felix Dombek
  • 2,003
  • 9
  • 28
  • 49
8

If you can run Linux commands on Windows, the most succinct way would probably be:

mkdir ch.{000..099}

If that doesn't work (because you use ksh or otherwise), then this should work:

mkdir $(printf 'ch.%s\n' $(seq -w 000 099))
JoL
  • 349
  • 1
  • 7
  • This is the best answer. The second version uses a feature of printf that I didn't know about, repeated format application on multiline argument, very nice! – Felix Dombek Oct 26 '19 at 17:53
  • 1
    @FelixDombek It doesn't need to be multiline. Because the command substitutions aren't quoted (`$()` instead of `"$()"`), the shell splits the output on `$IFS` characters (whitespace like spaces, tabs, and newlines by default) and provides it as multiple arguments to the command it's spliced in. It could be simple spaces or any other whitespace. `printf` doesn't see a multiline argument; it sees multiple arguments without whitespace. – JoL Oct 26 '19 at 18:45