Script for copying a file to parent folder and all its sub-folder?

Hi,

Newbie here.

I have this need of copying a single file to the parent folder and all its subfolder (>500 of them in branches).

I know it is possible to use Script/Automator. But lacking any skills and finding programming not my cup of tea (I am a chemistry person), I am finding it impossible to get this thing working?

Please help!

Thanks,
Gopal

Hi,

if I get it right, you want to copy one file in every subfolder of a specified folder, try this:

set theFolder to choose folder with prompt "choose parent folder"
set theFile to choose file
set fName to name of (info for theFile)
set source to quoted form of POSIX path of theFile
tell application "Finder" to set subFolders to {theFolder} & folders of entire contents of theFolder
repeat with oneFolder in subFolders
	set destination to quoted form of ((POSIX path of (oneFolder as alias)) & fName)
	do shell script "cp " & source & space & destination
end repeat

Thank you!

This worked flawlessly!

Is it possible to do the reverse?
That is, delete a ‘selected’ file from a folder and all its subfolders?

Regards,
Gopal

This is much easier:

set theFolder to choose folder with prompt "choose parent folder"
set theFile to choose file
set fName to name of (info for theFile)
set theFiles to do shell script "find " & quoted form of POSIX path of theFolder & " -name " & fName & " -exec rm {} \\;"

Note: the files will be deleted immediately. So be careful :slight_smile:

Thank you once again!

This too was flawless.

After struggling for days together copying deleting, its a joy to see it all happen in seconds.

Wish I had the knowledge and skills to write such simple scirpts myself…

Regards,
Gopal

Should have built one long cp command to remove multiple shell calls :stuck_out_tongue:

Do you know, how?
I don’t and I was too lazy to google for it :wink:

Here’s my offering… a one-liner:


do shell script "find " & (quoted form of (POSIX path of (choose folder with prompt "Choose your destination folder:"))) & " -type d  | xargs -I  {} cp " & (quoted form of (POSIX path of (choose file with prompt "Choose the file to be copied:"))) & " {}"

Is that sexy or what!?! :smiley: This is easily extendable to be a droplet or to set default locations but as a down and dirty method it’s the fastest method I could think of. Looks like Stefan has the removal pretty much covered.

Jim Neumann
BLUEFROG

P.S. That’s a capital “i” in the xargs, not an “L”.

Him : hahah Love it!!!

Stefan:

set shellCommand to ""
repeat with oneFolder in subFolders
	set shellCommand to shellCommand & "cp " & source & space & quoted form of ((POSIX path of (oneFolder as alias)) & fName) & ";"
end repeat
do shell script shellCommand

Nice one-liner, Jim :slight_smile:

My first thought was also to use find, but the parameter -type d includes folders as well as folder packages like bundle applications,
and this is not expected :wink:

Is it possible to make the script request a file-name (i.e, type in a file-name or first 2-3 letters followed by a wild-card) instead of having to ‘select’ a file?

Thanks!

Regards,
Gopal

Nice catch, Stefan! I’ll file that away for the future.

I’ve only been shell scripting for a few weeks but I thought it was a pretty good attempt (without having to Google anything). This stuff is insanely powerful (and DANGEROUSLY cool :D)

Thanks for the heads up.

Jim

!! Responding to James’ post after this one… Sweet save! :D:D (and another thing to add to my shell scripting knowledge !)

This should work though Stefan

do shell script "find " & (quoted form of (POSIX path of (choose folder with prompt "Choose your destination folder:"))) & " -type d -not \\( -path \"*.pkg*\" -o -path \"*.app*\" -o -path \"*.mpkg*\" \\) | xargs -I  {} cp " & (quoted form of (POSIX path of (choose file with prompt "Choose the file to be copied:"))) & " {}"

:smiley:

or the spotlight version ;):

do shell script "mdfind -onlyin " & (quoted form of (POSIX path of (choose folder with prompt "Choose your destination folder:"))) & " kMDItemKind  = \"Folder\" | xargs -I {} cp " & (quoted form of (POSIX path of (choose file with prompt "Choose the file to be copied:"))) & " {}"

assuming of course it’s a indexed volume… you and Adam with yer bloody spotlight :smiley:

The mind reels with the possibilities … or I’m passing out from information overload :stuck_out_tongue:

Thanks for the extra lessons, gentlemen!

Oh wait, didn’t someone else start this thread with a question ?!? :smiley:

Jim

Hahah, you’re right! Applogies Gopal, we sometimes tend to go off on tangents :smiley:

Actually, I enjoyed the banter… indicates to me that this is a good place to be. I am a newbie to Apple scripts, but not new to forums in general. :slight_smile:

BTW, is it possible to provide a ‘file name’ (or first few letters of a filename and a wild card) instead of having to ‘click’ on a file? That would be immensely useful for my purposes…

Thanks,
Gopal

Sure is

set theFile to quoted form of "/Posix/Path/To/File.xxx"

do shell script "find " & (quoted form of (POSIX path of (choose folder with prompt "Choose your destination folder:"))) & " -type d -not \\( -path \"*.pkg*\" -o -path \"*.app*\" -o -path \"*.mpkg*\" \\) | xargs -I  {} cp " & theFile & " {}"

Now just change the variable to the proper posix path to the file you want.