I need an automatic way of making a lot of numbered folders

30

9

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!

Safwan Abrar

Posted 2019-10-08T11:30:14.517

Reputation: 442

30Do not use Cygwin, it's terrible. – OrangeDog – 2019-10-08T22:06:46.947

1Once or multiple times? Please [edit] that into your question, because the answers will differ greatly. – Jan Doggen – 2019-10-09T14:30:25.727

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 – 2019-10-09T16:41:47.950

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 – 2019-10-09T16:51:51.633

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 – 2019-10-09T17:23:59.750

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 – 2019-10-10T15:13:11.037

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 supports Monica – 2019-10-14T18:30:49.273

Answers

48

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

Posted 2019-10-08T11:30:14.517

Reputation: 306 093

9Possibly create a from and a till variable, making this code easier when you look it after a few months (eg: Which 1 should I change again?) – Martijn – 2019-10-09T11:00:36.800

Wow, just when I thought Bash was terrible. – Agustín Lado – 2019-10-11T19:22:42.407

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.

Adam Prescott

Posted 2019-10-08T11:30:14.517

Reputation: 1 218

9If 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 – 2019-10-08T22:36:28.677

26@KeithMiller Don't forget the sequence operator! 1..94 | %{md {'c:\rootFolder\ch.{0:d3}' -f $_}} – Bob – 2019-10-09T00:12:35.513

28Oh, SuperUser suddenly becomes CodeGolf! :D – Ruslan – 2019-10-09T11:22:00.447

21

Don't forget to move the spaces! And you have the wrong brackets in there. PowerShell, 42 bytes... Try it online! 1..94|%{md('c:\rootFolder\ch.{0:d3}'-f$_)}

– KGlasier – 2019-10-09T13:48:56.563

11This would be a lot better as not a one-liner – OrangeDog – 2019-10-09T16:53:52.730

Thank you for the laugh. Love the one liners. Always a fun game – user319862 – 2019-10-10T03:16:42.320

One liner != Code golf. But hey, no harm done ;-) – Culme – 2019-10-10T09:13:13.923

For new installations the default shell is PS, so you can simply right-click on Start to get to it – OrangeDog – 2019-10-10T09:21:50.183

@OrangeDog: With code in multl-ine format, the semicolon trailing the $Name = ... is unnecessary. – Keith Miller – 2019-10-12T02:42:23.853

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

Posted 2019-10-08T11:30:14.517

Reputation: 257

4You can write loops over multiple lines just as well in PowerShell. No idea why people write completely weird non-idiomatic solutions. – Voo – 2019-10-09T14:47:55.440

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 – 2019-10-09T16:46:20.843

11It 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 – 2019-10-09T20:04:09.043

I wasn't aware of that one - thanks! – j4nd3r53n – 2019-10-10T08:38:53.060

@JoL Actually, {000..099} loses the leading zeros (in ksh) - is there a way around that? – j4nd3r53n – 2019-10-10T13:55:01.127

1@j4nd3r53n don't use ksh. both bash and zsh handle this correctly. – user60561 – 2019-10-10T15:40:04.847

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 – 2019-10-10T15:44:49.537

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 – 2019-10-11T08:47:05.887

@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 – 2019-10-11T10:44:53.523

1@Hashim Done. Though, I doubt it'll get any attention at this point. – JoL – 2019-10-11T15:03:40.400

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

Posted 2019-10-08T11:30:14.517

Reputation: 1 729

OP wasn't asking about renaming folders, they were asking about creating folders. – music2myear – 2019-10-13T03:39:40.050

@music2myear I added automatic creation of the folders with the correct names. – Felix Dombek – 2019-10-26T18:19:00.247

6

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

Posted 2019-10-08T11:30:14.517

Reputation: 332

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 – 2019-10-26T17:53:20.713

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 – 2019-10-26T18:45:44.997