better way to duplicate and rename file to a new folder multiple times

Ive searched the message boards for awhile now and I cant seem to solve this.

I would like to duplicate a file to a new folder multiple times based on a user defined amount as well as rename said file to add a sequential number suffix.

Right my script duplicates the file to the folder, but I cant duplicate to another folder multiple times. After I duplicate the file to the folder I then have to duplicate that file within the folder multiple times and then I end up with *.txt, *.txt copy, *txt copy 1, etc.

I then have to rename all of the files to *_01.txt, *_02.txt, *_03.txt, etc.

how can I condense this idea into a few lines of script so that my file gets moved and given its number suffix starting at 01 for the number of times the user defines.

thanks for any insight,

ryan

warning my brain gets fuzy come this time of morning so this is not the preffered way to do this… not that I know what would be the right way per say, but this does work and should at least give you a start in the (right?) direction.

set fileToDup to choose file
set posPath to POSIX path of fileToDup
set dupCount to 5 -- hard code this or ask the user
repeat with i from 1 to dupCount
	set s to "cp " & (quoted form of posPath) & " " & quoted form of ((text 1 through -5 of posPath) & "_" & i & (text -4 through -1 of posPath))
	do shell script s
end repeat

What your missing here is the leading 0 though it could be coded in to add that. The other thing is this assumes a 3 character file extension, but that too could be coded to accomodate the length of it determined at run time.

Have fun mate, I’m off to bed.

Hi Ryan,

Here’s an example:

property index_length : 2
-- "name.extension"
set f to choose file
set dest_folder to choose folder
-- get file information
set f_info to (info for f)
-- get file's name
set f_name to name of f_info
-- get the extension
set f_ext to name extension of f_info
-- get length of extension
set ext_length to length of f_ext
-- get name without extension
set base_name to text 1 thru -(ext_length + 2) of f_name
repeat
	-- get user input for number of duplicates
	display dialog "Enter an integer:" default answer "0"
	-- check user input for integer
	set user_input to text returned of result
	try
		set n to user_input as integer
		exit repeat
	on error
		display dialog "You must enter an integer."
	end try
end repeat
if n is 0 or n < 0 or n > 100 then return
-- repeat n times with index
repeat with i from 1 to n
	-- create index with leading 0s
	set this_index to text -index_length thru -1 of ("0" & i) -- for indices from 01 through 99
	-- make new name
	set this_name to base_name & "_" & this_index & "." & f_ext
	-- duplicate the file
	tell application "Finder"
		set this_duplicate to duplicate f to dest_folder
		set name of this_duplicate to this_name
	end tell
end repeat
tell application "Finder"
	activate
	open dest_folder
end tell

I may have missed some error checking. That’s your job.

Edited: oopsy, this line:

set this_index to text -index_length thru -1 of (“0” & i)

Instead of “0”, I should have created new text of leading 0s according to the index length. If the index length is 2 (i.e. 01 through 99), then it is OK. If the index length is 3 (i.e. 001 thru 999), then “0” should be “00”. ETC. But you can correct that. Just add a new check for the index length and create the leading 0s check.

Edited: oops, here’s another one. This line:

if n is 0 or n < 0 or n > 100 then return

How can we make 100 variable according to the index length. Maybe 10^index_length - 1. Then you have to add more error checking. Well, you can do work it. If you have problems ask.

gl,

It was bugging me so I fixed it up a little.


property index_length : 2
-- "name.extension"
set f to choose file
set dest_folder to choose folder
-- get file information
set f_info to (info for f)
-- get file's name
set f_name to name of f_info
-- get the extension
set f_ext to name extension of f_info
-- get length of extension
set ext_length to length of f_ext
-- get name without extension
set base_name to text 1 thru -(ext_length + 2) of f_name
repeat
	-- get user input for number of duplicates
	display dialog "Enter an integer:" default answer "0"
	-- check user input for integer
	set user_input to text returned of result
	try
		set n to user_input as integer
		exit repeat
	on error
		display dialog "You must enter an integer."
	end try
end repeat
set index_max to 10 ^ index_length - 1
if n is 0 or n < 0 or n > index_max then return -- fixed this
-- repeat n times with index
set Z_list to {}
repeat with i from 1 to index_length - 1
	set end of Z_list to "0"
end repeat
set lead_z to Z_list as string
repeat with i from 1 to n
	-- create index with leading 0s
	set this_index to text -index_length thru -1 of (lead_z & i) -- for indices from 01 through 99
	-- make new name
	set this_name to base_name & "_" & this_index & "." & f_ext
	-- duplicate the file
	tell application "Finder"
		set this_duplicate to duplicate f to dest_folder
		set name of this_duplicate to this_name
	end tell
end repeat
tell application "Finder"
	activate
	open dest_folder
end tell

Party time!

gl,

Hi,

I was in a bit of a rush last night and fixed it up.


property index_length : 2
-- "name.extension"
set f to choose file
set dest_folder to choose folder
-- get file information
set f_info to (info for f)
-- get file's name
set f_name to name of f_info
-- get the extension
set f_ext to name extension of f_info
-- get length of extension
set ext_length to length of f_ext
-- get name without extension
set base_name to text 1 thru -(ext_length + 2) of f_name
-- get the max integer
set index_max to (10 ^ index_length - 1) as integer
-- repeat until user enters a valid integer or Cancels
repeat
	-- get user input for number of duplicates
	display dialog ("Enter an integer from 1 to " & index_max) & ":" default answer "0"
	-- check user input for integer
	set user_input to text returned of result
	try
		set n to user_input as integer
		if n < 1 or n > index_max then error
		exit repeat
	on error
		display dialog "You must enter an integer from 1 to " & index_max & "!"
	end try
end repeat
-- repeat n times with index to create leading zeros
set z_list to {}
repeat with i from 1 to index_length - 1
	set end of z_list to "0"
end repeat
set lead_z to z_list as string
repeat with i from 1 to n
	-- create index with leading 0s
	set this_index to text -index_length thru -1 of (lead_z & i) -- for indices from 01 through n-1
	-- make new name
	set this_name to base_name & "_" & this_index & "." & f_ext
	-- duplicate the file
	tell application "Finder"
		set this_duplicate to duplicate f to dest_folder
		set name of this_duplicate to this_name
	end tell
end repeat
tell application "Finder"
	activate
	open dest_folder
end tell

Write back if you have any problems. You might change the repeat loop that creates leading zeros if you want. It’s kind of an overkill for this situation.

gl,

Hi guys. While I haven’t been following this topic, I just looked at it now - and recalled having produced something similar a while back. Dusted off and thrown in just for comparison, FWIW… :slight_smile:

property |first run| : true

to |set target| for f
	set {d, text item delimiters} to {text item delimiters, ":"}
	set {my |container folder|, my |target folder|, text item delimiters} to {f's text 1 thru text item -2 as alias, f's text item -1, d}
end |set target|

if |first run| then tell (path to desktop) to set {|first run|, |source file|, |container folder|, |target folder|, |duplicate count|} to ¬
	{false, it, it, "Duplicates Folder", "1"}

set |source file| to choose file with prompt "Choose a file to duplicate:" default location |source file| without invisibles

|set target| for ((choose file name default location |container folder| with prompt ¬
	"Choose name and location of the folder for the duplicate files:" default name |target folder|) as Unicode text)

repeat
	tell text returned of (display dialog "Enter the number of duplicate files required:" default answer |duplicate count|) to try
		tell it as integer to if it < 1 or it > 9999999 then error
		set |duplicate count| to it
		exit repeat
	end try
	beep
end repeat

set c to count |duplicate count|
set {z, c} to {{(10 ^ c as integer as text)'s text 2 thru c, ""}'s item (1 div c + 1), -c}
set |sound on| to not output muted of (get volume settings)
if |sound on| then set volume with output muted
tell application "Finder"
	set {name:b, name extension:e} to |source file|
	if (count e) > 0 then set e to "." & e
	set b to b's text 1 thru -((count e) + 1) & "_"
	tell (make new folder at |container folder| with properties {name:|target folder|}) to repeat with i from 1 to |duplicate count|
		set (duplicate |source file| to it)'s name to b & (z & i)'s text c thru end & e
	end repeat
end tell
if |sound on| then set volume without output muted

thanks this looks very promising. Ive been out of town so once I get settled down I will test these out.

Ill keep you posted

thanks,

ryan