Help me automate a file copy and renaming task on OS X.
March 2, 2020 9:07 AM   Subscribe

Help me copy and rename a file 220 times.

I'm in the middle of a project to generate product photos for a new product launch. As part of this launch, about 220 products were launched that have the exact same configuration. Instead of taking 220 photos, we've taken one. The hitch is that this one photo now needs to be copied and renamed 220 times according to our catalog number and product name scheme.

I have a spreadsheet with the catalog numbers and product names, and I know computers were made to do this kind of work. I'm reasonably certain Automator can do this, but I have no idea how to make it do it.

Can you explain it to me?
posted by rocketman to Computers & Internet (11 answers total) 4 users marked this as a favorite
 
Save your list as a txt file and something like this should work on the command shell:

cat namelist.txt | xargs cp onephoto.jpg
posted by paper chromatographologist at 9:16 AM on March 2, 2020


Can you not just have each product call the same photo in the CMS?
posted by humboldt32 at 9:42 AM on March 2, 2020


A Better Finder Rename might be the tool for you.
posted by Kitchen Witch at 9:43 AM on March 2, 2020


Response by poster: Can you not just have each product call the same photo in the CMS?

Oh god do I wish that were the case. Unfortunately, we're dealing with a hosted catalog situation, where the data gets ported out to an Amazon-like re-seller. So we need a unique instance of the image for every catalog number, named "CatNumber_ProductName.jpg"

I can find a bored receptionist to do the work for me, but frankly I'd prefer to run a command to save the time of explaining it and then doing QC on the manual job.
posted by rocketman at 9:48 AM on March 2, 2020 [1 favorite]


Best answer: Here's a version of paper chromatographologist's answer that should work in the terminal, given a plain text file with a single line per new file name:

cat new_names.txt | xargs -I {} cp Original_Photo.jpg {}
posted by theodolite at 10:14 AM on March 2, 2020 [3 favorites]


Best answer: For creating new_names.txt, I'd create it as a column in Excel first (with something like CONCAT(CatNumber, "_", ProductName, ".jpg")) and then copy-paste the result to a text editor. You could also paste that column to a new blank spreadsheet, then save it with the "Macintosh Formatted Text (.txt)" format.
posted by theodolite at 10:22 AM on March 2, 2020 [3 favorites]


I would use a spreadsheet and just build the whole command as a string formula.
So column A will be the product numbers and column B will be something like
="cp Original_Photo.jpg " & A1 & "_ProductName.jpg"

Drag the formula down and then if the new filenames look right, copy and paste that whole column to the command line.
posted by Lanark at 10:39 AM on March 2, 2020


Response by poster: theodolite, your answer is *mostly* working, except all the files get a " ?" at the end, with the exception of the last name in the file.

I'm assuming there's a hidden character in the file that's getting expressed as the "?".
posted by rocketman at 10:56 AM on March 2, 2020


Best answer: Oof, yeah, if you saved from Excel I think it used the wrong newline sequence. This should fix it:

cat new_names.txt | tr -d '\r' > new_names_fixed.txt

Copy-pasting the original Excel column into a text editor and just saving that should work too.
posted by theodolite at 11:25 AM on March 2, 2020 [1 favorite]


Response by poster: Thanks so much! Everything works!

Wish I had a better grasp of *what* exactly I was doing, but this should save me a ton of time in the future.
posted by rocketman at 11:30 AM on March 2, 2020


I'm just a baby command line warrior, but I think this stuff is neat, so here's a stab at explaining how that worked.

cat new_names.txt: this says "take new_names.txt and just print out the whole file." This isn't that useful by itself, but it's perfect if you want to take the contents of a file and do something else to it. That's what the | (pipe) character does: it says "take the output from the command before the pipe, and use that as input for the command after the pipe."

xargs is a neat little utility that says "take some kind of input, and use it in the arguments (i.e., the stuff you type after the name of the command) for whatever comes next." The "-I {}" part says to go through the input line by line and insert that text where it says {} in the following command. ({} is just a placeholder: you could use % or x or anything else.)

cp copies the file named in the first argument to the filename in the second argument. The first argument (the filename of the original photo) never changes, but we want the second argument to change for each line of the text file. So

cat new_names.txt | xargs -I {} cp Original_Photo.jpg {}

is equivalent to typing


cp Original_Photo.jpg C456_pooltoy.jpg
cp Original_Photo.jpg C441_gardengnome.jpg
cp Original_Photo.jpg C423_cowboyhat.jpg

...

for each line of the file.

The Excel complication has to do with the fact that there are a bunch of different ways to represent a line break in text files, usually a combination of \r (carriage return) and \n (line feed). UNIX systems and their descendants (including OS X) use \n, old pre-OSX Macs use \r, and Windows says ¿porque no los dos? and uses \r\n.

Because xargs is from Unixtown circa 1977 it's expecting lines to be separated by just a \n, which is why you were getting those question marks - those were the shell trying to depict the extra \r characters that Excel helpfully added. (I thought saving as "Macintosh Formatted Text" might have worked, but it seems like that actually uses the pre-OSX \r newlines, which Macs haven't used for about twenty years. grr!)

So that second line uses the tr (translate) command to delete (-d) all the \r characters that were goofing you up and creates a new file from the output.

You can type (e.g.) man tr to pull up a manual that explains all of the functions and options for a given command. However, these tend to be written in a pretty technical style, so if you want to learn to do more of this stuff I'd recommend working through a primer like this one or an interactive course like this.
posted by theodolite at 2:13 PM on March 2, 2020 [2 favorites]


« Older Help me track down this episode of an obscure 1982...   |   Help me get over my secret shame Newer »
This thread is closed to new comments.