Create multiple sequential folders

Hi everyone,

I'm looking for a solution to the following problem:

-I need to create multiple sequential folders into a specific one, by entering the starting and the ending number (eg. from 500 to 670, it will create 500, 501, 502 up to 670).

Is there any way to do this?

Many thanks for your help.

I had exactly this requirement a while back so here's a copy of my macro to do it. Its operation is pretty straightforward but make sure you first open a Finder window inside the folder where you want your new folders created.

Create sequential (numerical) empty folders.kmmacros (7.2 KB)

Click to see macro

Keyboard Maestro Export

The macro uses one of my library macros to pad out the new folder names, you'll need that too so here it is:

[LIB]_PadAtFront.kmmacros (8.3 KB)

Click to see macro

Keyboard Maestro Export

If I were to write this macro now I would use local variables but that's for another time...


Thank you so much tiffle,...I just downloaded KM yesterday, so this is something totally new to me and I'm trying to make your macros work...hope to be succesfull :crossed_fingers:t3: :sweat_smile:

1 Like

No problem - and welcome to KM and the KM Forum!

If you need more assistance, just ask. I wrote those macros a long time ago, so if I wrote them now they’d look a bit different and also be more user-friendly! Good luck.

1 Like

FWIW, an example using Haskell and showing how can we passing a JSON dictionary with options into a Haskell script.

Note: GHC is required and could be installed with Homebrew.

Sequential folders created at path.kmmacros (5.6 KB)

Expand disclosure triangle to show Haskell source
{-# LANGUAGE OverloadedStrings #-}

module Main where

-- import Data.Aeson.Types
import Control.Monad
import Data.Aeson
import qualified Data.ByteString.Lazy as LB
import Data.List (sortOn)
import Data.List.Split
import Data.Map.Strict (Map, fromList, (!))
import qualified Data.Text as T
import Data.Text.Encoding (decodeUtf8, encodeUtf8)
import System.Directory
import System.FilePath
import System.Random

interact' :: (String -> String) -> IO ()
interact' f = putStr . f =<< readFile =<< getContents

interact'' :: (Map String String -> IO ()) -> IO ()
interact'' f =
    >>= LB.readFile . T.unpack . decodeUtf8 . LB.toStrict
    >>= pure . (eitherDecode :: LB.ByteString -> Either String (Map String String))
    >>= either putStrLn f

main :: IO ()
main =
  interact'' $
    ( \x ->
        let startIndex = x ! "startIndex"
            endIndex = x ! "endIndex"
            maxWidth = length endIndex
            baseFolder = x ! "baseFolder"
            m = read startIndex :: Int
            n = read endIndex :: Int
         in mapM_ (createDirectoryIfMissing False) $
              map (baseFolder </>) $
                map (T.unpack . T.justifyRight maxWidth '0') $
                  map (T.pack . show) $
                    enumFromTo m n
1 Like

This is actually a job for (fanfare) Shell Man!

In bash or zsh you can use the command mkdir {500..670} to create the directories you need. In zsh you can pad the numbers with mkdir {001..999}. You can even go crazy with mkdir I\ am\ {a..z}{01..10}\ test\ folder to make folders "I am a01 test folder", "I am a02 test folder" through "I am z10 test folder".

For even more fun use mkdir -p Folder\ {A..Z}/Subfolder\ {01..10} to make folders "Folder A" though "Folder Z" and in each of those folders make "Subfolder 01" through "Subfolder 10".

There is a limit to this -- the shell expands the command into a list of commands, one for each folder to be created. The limit is to the number of characters, so the number of folders that can be created will depend on the length of the path string argument. On my machine I can do more than 8,000 with mkdir I\ am\ {0001..8000}\ test\ folder, so it's it's a usably large limit!

If you do want to make more in a single command (really?!) you can even do a for loop...

So all that's left is to knock the above into something more easily usable, maybe using KM dialogs to set the options. Hmmm...

1 Like

Presumably also known as Donatello :slightly_smiling_face:

1 Like

Superb....and incredibly easy solution...Thank you so much for helping me :clap:t3: :clap:t3:

An item on my "How does KM work?" list is HTML dialogs -- I'll see if I can knock something up that's a little more user friendly for setting ranges and pre-/post-fix!

Oh dear -- I didn't realise how much HTML etc I'd forgotten (or never really knew...). But, finally, here's a KM "make some folders" macro with an HTML front-end. It may not ever be useful, but it was a good learning experience.

You can set one or two ranges (either numbers, padded numbers, or letters) plus none, any or all of prefix, inter and postfix constants for your folders. The first and last folder names plus destination will be previewed at the bottom. So you could make folders "1" to "9", "01a test" thru "99z test", "test-01-project-A-folder" thru "test-99-project-Z-folder", etc.

The script will catch most of the problematic "special" shell characters, while letting you use spaces and similar. "Illegal" entries will have their field red-boxed to warn you. I haven't tried accented characters though! If you want to see the shell command that'll be used, remove the "--" at the beginning of the "display dialog" line in the AppleScript.

I would apologise for the horrible web page -- both the code and the look -- but I really can't be bothered!

Create Folders with HTML dialog.kmmacros (16.2 KB)