Apple Event: May 7th at 7 am PT

Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Automator duplicate names

I've looked everywhere... please help.


Duplicating a single file 60+ times, each is given 'copy 1, copy 2 etc.' at the END of the filename.


I want to add 60+ different words to the FRONT of the filename.


If 'copy 1' etc. was added at the front, I could make a single 60 entry workflow to 'find copy 1, replace with alpha... find copy 2, replace with beta' etc. But as it stands... how can I do this?

Posted on Jun 9, 2022 4:17 AM

Reply
Question marked as Best reply

Posted on Jun 9, 2022 11:24 AM

Your syntax makes no sense.


>CD ~Desktop/TEST


all UNIX commands are lower case. CD should throw and error on its own. Also ~Desktop/TEST isn't a valid path.


> do TESTFILE.pdf


do is a statement unto itself. It doesn't take any parameters, so its complaining about why 'TESTFILE.pdf' is passed in here.


> cp -a ../TESTFILE.pdf_${num}_TESTFILE.pdf


the cp command requires two parameters (in addition to any optional switches). It requires a source file to copy, and a destination to copy it to. As written, your script passes in a single parameter '../TESTFILE.pdf_${num}_TESTFILE.pdf'


It should work if you correct these errors:


cd ~/Desktop/TEST
for num in {01..60};
do
  cp -a TESTFILE.pdf _${num}_TESTFILE.pdf
done


spaces and capitalization matter.


Note that this will simply prefix a number on the file name. not an alpha string.


> Even better, I'd see how to add the text to all 60 odd files via a CSV, applying one row to the front of each filename?


Assuming your file contains a single prefix per line, and is saved as prefix.txt, you can rewrite the loop as:


while read prefix
do
  cp TESTFILE.pdf ${prefix}TESTFILE.pdf
done < prefix.txt


Note that the prefix strings must be single words. spaces and other non-shell safe characters will break this.

Similar questions

9 replies
Question marked as Best reply

Jun 9, 2022 11:24 AM in response to Smallbadger

Your syntax makes no sense.


>CD ~Desktop/TEST


all UNIX commands are lower case. CD should throw and error on its own. Also ~Desktop/TEST isn't a valid path.


> do TESTFILE.pdf


do is a statement unto itself. It doesn't take any parameters, so its complaining about why 'TESTFILE.pdf' is passed in here.


> cp -a ../TESTFILE.pdf_${num}_TESTFILE.pdf


the cp command requires two parameters (in addition to any optional switches). It requires a source file to copy, and a destination to copy it to. As written, your script passes in a single parameter '../TESTFILE.pdf_${num}_TESTFILE.pdf'


It should work if you correct these errors:


cd ~/Desktop/TEST
for num in {01..60};
do
  cp -a TESTFILE.pdf _${num}_TESTFILE.pdf
done


spaces and capitalization matter.


Note that this will simply prefix a number on the file name. not an alpha string.


> Even better, I'd see how to add the text to all 60 odd files via a CSV, applying one row to the front of each filename?


Assuming your file contains a single prefix per line, and is saved as prefix.txt, you can rewrite the loop as:


while read prefix
do
  cp TESTFILE.pdf ${prefix}TESTFILE.pdf
done < prefix.txt


Note that the prefix strings must be single words. spaces and other non-shell safe characters will break this.

Jun 9, 2022 6:00 AM in response to Smallbadger

The Finder is in control when you use its duplicate and it will append the generation numbers to the filename. It does not prefix filenames in this manner. If I duplicate kipsum.tex, the first duplication is kipsum copy.tex, the second kipsum copy 2.tex, and so forth… kipsum copy nn.tex.


If you start in the Zsh shell, you can interactively make numbered copies of your file. Let's assume that you have kipsum.tex on your Desktop and you want the numbered copies in an existing or created folder B. In the Terminal:


makedir -p ~/Desktop/B
cd ~/Desktop/B
for num in {01..60};
do
   # copy the filename one directory (Desktop) above this folder location
   cp -a ../kipsum.tex copy_${num}_kipsum.tex
done


This results in copy_01_kipsum.tex copy_02_kipsum.tex … copy_60_kipsum.tex in the current folder.


The Spotlight search for these TeX files would be:


kind:TeX name:copy_



Jun 9, 2022 6:10 AM in response to VikingOSX

Hey Viking! Thanks for the help!


So I ran a terminal command below, after making a folder called TEST and putting a file called TESTFILE.pdf inside it... I get a parse error near 'do' ?


CD ~Desktop/TEST
for num in {01..60};
do TESTFILE.pdf
   # copy the filename one directory (Desktop) above this folder location
   cp -a ../TESTFILE.pdf_${num}_TESTFILE.pdf
done

Automator duplicate names

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.