sequential numbered folders

Hi all

I’m quite new to applescript (sorry!) but I’m struggling to create a script to do what I want.
I would like to make 1000 folders starting with 0001 through to 1000!

That’s it, am I a dunce or what?

Thanks in advance.

wilse :cry:

Nope, not a dunce at all. :slight_smile:

This should work as long as the target folder doesn’t contain a folder with the name of one of the new folders (OOO1-1000).

set targetFol to (choose folder with prompt "Choose the location for the new folders.")
set counter to 0

tell application "Finder"
	repeat 1000 times
		set counter to counter + 1
		try
			set new_ to make new folder at targetFol with properties {name:my add_leading_zeros(counter, 3)}
		on error e
			display dialog e
			return
		end try
	end repeat
end tell

on add_leading_zeros(this_number, max_leading_zeros) -- provided by Apple
	set the threshold_number to (10 ^ max_leading_zeros) as integer
	if this_number is less than the threshold_number then
		set the leading_zeros to ""
		set the digit_count to the length of ((this_number div 1) as string)
		set the character_count to (max_leading_zeros + 1) - digit_count
		repeat character_count times
			set the leading_zeros to (the leading_zeros & "0") as string
		end repeat
		return (leading_zeros & (this_number as text)) as string
	else
		return this_number as text
	end if
end add_leading_zeros

– Rob

Just as an aside, Rob, since the script is doing this 1000 times, there’s a much quicker way to work out the 4-digit string.

First, change the call to include the string length you want, rather than the number of 0’s to add

...
         set new_ to make new folder at targetFol with properties {name:my add_leading_zeros(counter, 4)} 
...

then:

on add_leading_zeros (value, length)
   return characters (- length) through -1 of ("00000" & value) as string
end add_leading_zeros

The idea here is to construct a new string consisting of “0000” and the input value, then truncate it. For example, if you’re passing in 123, the script basically:

  1. makes a new string “00000” & “123” → “00000123”

  2. catches the last n characters of the string (where n is passed into the routine). In this case n is 4, so it takes the last 4 characters of the string → “0123”

… or for simplicity:

set targetFol to (choose folder with prompt "Choose the location for the new folders.")

tell application "Finder"
  repeat with i from 10001 to 11000
    try
      make new folder at targetFol with properties {name:text 2 thru 5 of (i as string)}
    end try
  end repeat
end tell

:slight_smile:

1 Like

Yes, that’s much simpler! I was too lazy (sleepy) to think about it so I took the easy way out by poaching Apple’s handler and passing the result to the Finder. Thanks, Nigel. It’s always a treat to watch you squish a script down to a few lines of well crafted code. :wink:

Edited to thank Camelot too, because I thought both responses were from Nigel (until Wilse responded). :rolleyes:

– Rob

I had no idea how to do it!

And I’ve used it, and it works a treat, also I can believe how quick it is.

Cheers Rob, Nigel and Camelot your help is really appreciated.

Wilse

Slightly modified if you really wanted 3 digit folders

set targetFol to alias "Extra HD:Test Folders:"

tell application "Finder"
	repeat with i from 1001 to 1100
		try
			make new folder at targetFol with properties {name:text -3 thru -1 of (i as string)}
		end try
	end repeat
end tell

This will create 1000 sub-folders in a initial folder of your selection. Sub-folders will be zero padded as appropriate.

use AppleScript version "2.4" -- Yosemite (10.10) or later
use scripting additions

set targetFolder to POSIX path of (choose folder with prompt "Choose the location for the new folders.")
do shell script "zsh -c 'for f in {0001..1000}; do mkdir -p \"${0}${f}\";done' " & targetFolder
return

Probably quicker than any AppleScript repeat loop…:wink:

Yes, but by employing do shell script, which defeats the point a little bit. Regarding zsh, you don’t need the for ... loop, as you can do this:

mkdir -p "$0"/{0001..1000}/

But you also need to ensure that targetFolder is properly escaped in order to pass it to zsh as a single argument that can be referenced with $0. Folder paths that contain spaces, for instance, will be passed to zsh as multiple arguments, and $0 will therefore only contain the first portion of the full path. ■


If you use System Events instead of Finder to create the folders, it’ll be faster, it won’t complain if the folder already exists, and it won’t block Finder:

tell application id "com.apple.SystemEvents" to tell folder ¬
        (targetFol as text) to repeat with i from 1 to 1000
        make new folder with properties {name:¬
                text -1 thru -4 of ("000" & i)}
end repeat
1 Like