Automating file copying on a Mac
February 21, 2008 10:35 AM   Subscribe

How do I copy a file to multiple folders, and rename it based on those folders? I know the answer is "unix", but I need hand-holding.

So I have a template file. Let's call it template.fla. I need to copy it to about 40 folders, all within the same folder. I also need to prepend it with the folder name, so if it goes in a folder called 1234 its name should be 1234_template.fla. I'd rather not do this 40 times by hand (or 80, since there are two template files).

I'm on a Mac, and I know there's a some way to script this, but besides the most basic commands, I don't know my way around the shell (or Applescript, for that matter).
posted by O9scar to Computers & Internet (9 answers total)
Response by poster: An update: I don't need to prepend, just replace the name with the folder name. So template.fla inside folder 1234 becomes 1234.fla.
posted by O9scar at 10:41 AM on February 21, 2008

Are the subdir's all the dir's that are in the containing dir?
posted by d4nj450n at 10:44 AM on February 21, 2008

Put this in a file called, say, /home/o9scar/
use File::Copy;
my $file = shift;
while (<>) {
  next unless -d;
  copy ($file, "$_/$_.fla") or warn $!;
Open a console window (or whatever Mac people call getting to a shell.) Change directory to the folder containing the 40 files, with something like:
cd /home/o9scar/myfolder
Then enter,
perl /home/o9scar/ /tmp/template.fla
(specifying wherever the template.fla file is.)

You say you want to do this with 2 files -- I presume the other one has a different naming scheme than having the same name as the directory, yes?
posted by Zed_Lopez at 11:02 AM on February 21, 2008

I'm a n00b and there are doubtlessly better ways to do it, but:

Assuming you're in the directory in which all the subfolders are contained, type:

find . -type d -exec cp /path/to/somefile.flv {} \;

and then:

find . -type d -exec mv {}/somefile.flv {}/{}.flv \;

posted by limon at 11:07 AM on February 21, 2008

Seconding d4nj450n 's question. If they are, then (assuming bash shell)

template="template.file" ; for i in `find . -maxdepth 1 -mindepth 1 -type d` ; do cp -vi $template $i/${i}_$template ; done

should do the trick.

If they're not all in the same directory, I'd advise making a file "dirlist" with each directory (either relative or absolute) and then:

template="template.file" ; for i in `cat dirlist` ; do dirname=`echo $i | perl -e '$_=<>; chomp ; if (/.*\/(.*?)$/){ $_=$1; } print "$_\n" ; '` ; cp -vi $template $i/${dirname}_$template ; done

should work. Repeat the command changing


while keeping the for loop the same. There's probably a nicer way of getting the last subdir from full path, but I tested that that works for my bash.

If you're afraid, put a "echo" in front of the "cp" command, and you'll see what the script would do. Heck, cut and copy the output into the shell if you want.
posted by nobeagle at 11:14 AM on February 21, 2008

limon's is a better approach than my Perl script. And you can do it in one:
find . -type d -exec cp /path/to/template.fla {}/{}.fla \;
Fails if there are any subdirectories under the directories at the 1234 level, though.
posted by Zed_Lopez at 11:18 AM on February 21, 2008 [1 favorite]

Zed_Lopez - it won't fail if one does "find -mindepth 1 -maxdepth 1 " ... then the only problem is if the directories aren't all subdirectoreis. Bah. missed the update, one would have to change the destination of the 2nd for loop (the one where all the destination directories aren't in the current directory) to $i/$dirname.fla .
posted by nobeagle at 11:35 AM on February 21, 2008

Nice, Zed_Lopez. I'm a dolt for doing it in two commands.
posted by limon at 12:22 PM on February 21, 2008

Isn't this what folder actions or automator are designed to do? The scripting part is not my forte, but ever since buying a macbook last year I've been able to find a feature within osx to do pretty much anything I can think of.

Actually, looks like folder actions are designed to ease repetitive tasks. I guess if you only wanted to perform this action once, you wouldn't need it.
posted by Chris4d at 3:47 PM on February 21, 2008

« Older How to count the number of times a string appears...   |   Muttering : where do you cross the line? Newer »
This thread is closed to new comments.