31

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!

8
  • 30
    Do not use Cygwin, it's terrible.
    – OrangeDog
    Oct 8 '19 at 22:06
  • 1
    Once or multiple times? Please edit that into your question, because the answers will differ greatly.
    – Jan Doggen
    Oct 9 '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. Oct 9 '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 9 '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. Oct 9 '19 at 17:23
49

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 (
2
  • 9
    Possibly 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
    Oct 9 '19 at 11:00
  • Wow, just when I thought Bash was terrible. Oct 11 '19 at 19:22
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.

9
  • 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}} Oct 8 '19 at 22:36
  • 26
    @KeithMiller Don't forget the sequence operator! 1..94 | %{md {'c:\rootFolder\ch.{0:d3}' -f $_}}
    – Bob
    Oct 9 '19 at 0:12
  • 28
    Oh, SuperUser suddenly becomes CodeGolf! :D
    – Ruslan
    Oct 9 '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! 1..94|%{md('c:\rootFolder\ch.{0:d3}'-f$_)}
    – KGlasier
    Oct 9 '19 at 13:48
  • 11
    This would be a lot better as not a one-liner
    – OrangeDog
    Oct 9 '19 at 16:53
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.

9
  • 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 9 '19 at 14:47
  • 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 9 '19 at 20:04
  • 1
    @j4nd3r53n don't use ksh. both bash and zsh handle this correctly.
    – user60561
    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
  • 1
    @Hashim Done. Though, I doubt it'll get any attention at this point.
    – JoL
    Oct 11 '19 at 15:03
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))
2
  • 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! 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
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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.