Sunday, July 5, 2020

#1 2020-05-09 09:58:21 am

philipcaplan2
Member
Registered: 2015-03-11
Posts: 10

Method to duplicate one file a thousand times

Dear Forum

Could a kind person give me the simplest method (using Applescript or Automater or another way) to:
-- in a selected folder make 1000 copies of every file in that folder with the name prepended with a start number and an end number.

In other words, if the selected folder contains one file called 'sample.txt' then running the process with start=1 and end=1000 results in that folder containing 1001 files, all copies of the original file, called '1sample.txt', '2sample.txt', up to '1000sample.txt', as well as the original 'sample.txt'

I am running MacOS 10.13.3

Thank you.  Philip Caplan

Model: MacBookAIr
AppleScript: Script Editor v2.10
Browser: Safari 537.36
Operating System: macOS 10.13

Offline

 

#2 2020-05-09 10:30:47 am

KniazidisR
Member
Registered: 2019-03-03
Posts: 1259

Re: Method to duplicate one file a thousand times

Using Finder:

Applescript:


set aFolder to choose folder

tell application "Finder"
   repeat with aFile in (every file of aFolder) as alias list
       set baseName to name of aFile
       repeat with i from 1 to 1000
           set name of (duplicate aFile) to (i as text) & baseName
       end repeat
   end repeat
end tell

Last edited by KniazidisR (2020-05-09 10:35:10 am)


Model: MacBook Pro
OS X: Catalina 10.15.4
Web Browser: Safari 13.1
Ram: 4 GB

Offline

 

#3 2020-05-09 11:37:20 am

philipcaplan2
Member
Registered: 2015-03-11
Posts: 10

Re: Method to duplicate one file a thousand times

Many thanks, KniazidisR.

That works, but I had hoped there would be a dialog where I could enter a "start" number and an "end" number, as that would give me greater flexibility over the names and number of files created.

Would you (or somebody) be able to give me a revised script with that function added?

Many thanks.   Philip

Offline

 

#4 2020-05-09 11:50:21 am

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4534

Re: Method to duplicate one file a thousand times

Applescript:


set aFolder to choose folder
set firstNum to text returned of (display dialog "Enter a number" default answer "1") as integer
tell application "Finder"
   repeat with aFile in (every file of aFolder) as alias list
       set baseName to name of aFile
       repeat with i from firstNum to (firstNum + 999)
           set name of (duplicate aFile) to (i as text) & baseName
       end repeat
   end repeat
end tell

It's boring to have to guess what is really asked for.

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 9 mai 2020 19:50:15

Offline

 

#5 2020-05-09 12:46:13 pm

philipcaplan2
Member
Registered: 2015-03-11
Posts: 10

Re: Method to duplicate one file a thousand times

Hi Yvan

I ran your script but it didn't give me "a dialog where I could enter a "start" number and an "end" number" as I asked. It just went ahead and created 1000 copies!

sad    Philip

Offline

 

#6 2020-05-09 01:17:26 pm

Yvan Koenig
Member
Registered: 2006-09-14
Posts: 4534

Re: Method to duplicate one file a thousand times

Applescript:

set aFolder to choose folder
set twoNums to text returned of (display dialog "Enter first number, last number" default answer "1,2")
try
   set {t1, t2} to my decoupe(twoNums, ",")
on error
   error "“" & twoNums & "” aren‘t two numbers separated by a comma"
end try
try
   set {n1, n2} to {t1 as integer, t2 as integer}
on error
   error "“" & twoNums & "” aren‘t two numbers separated by a comma"
end try
if n1 > n2 then
   set bof to n1
   set n1 to n2
   set n2 to bof
end if

tell application "Finder"
   repeat with aFile in (every file of aFolder) as alias list
       set baseName to name of aFile
       repeat with i from n1 to n2
           set name of (duplicate aFile) to (i as text) & baseName
       end repeat
   end repeat
end tell

#=====

on decoupe(t, d)
   local oTIDs, l
   set {oTIDs, AppleScript's text item delimiters} to {AppleScript's text item delimiters, d}
   set l to text items of t
   set AppleScript's text item delimiters to oTIDs
   return l
end decoupe

#=====

Yvan KOENIG running High Sierra 10.13.6 in French (VALLAURIS, France) samedi 9 mai 2020 21:17:22

Offline

 

#7 2020-05-09 01:29:21 pm

philipcaplan2
Member
Registered: 2015-03-11
Posts: 10

Re: Method to duplicate one file a thousand times

Thanks, Yvan. That now works.
All the best and stay safe.
Philip

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)