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?
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
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
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!?! 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”.
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
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
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?
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 !)
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:"))) & " {}"
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:"))) & " {}"
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.
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…
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.