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:

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