Is there a way, either via the finder or terminal, or with some utility, to copy the folder structure of a given folder without copying all the files? On windows, I can use xcopy /T but I need a mac solution this time.
That is an excellent question! I've been trying to figure this out too - scanning a decade's worth of photos from negs, categorized by date in lots of nested folders. Apart from the hi-rez scans, client wants the same pics on DVD slideshows (low rez). Been trying to make an Automator action to create the same folder structure and then run a Photoshop action to save TV-rez versions of all the photos in their respective place in that structure.
If anyone figures this out it would be very helpful.
I don't believe so. Considering it's such a miniscule portion of the user population who would eveer need to do it, I'm not surprised. I guess Cmd-Shift-N will be your best friend.
I don't believe so. Considering it's such a miniscule portion of the user population who would eveer need to do it, I'm not surprised. I guess Cmd-Shift-N will be your best friend.
Well, so far you have on this forum thread two users who have need of this sort of tip. I would imagine it would be useful for any number of graphics/media professionals who often need different resolutions/colour spaces etc. of the same set of files but don't have the time to manually reproduce the folder structure and don't want to kill all their disk space by duplicating the whole folder full of high-rez files before downsampling.
The Genius Bar forum is supposed to be for helping each other. Not sure why you're wasting time telling us you don't think our problem is important. Help out if you can, don't patronize.
You could write a terminal script or combine applescript and unix commands to do this. Maybe someone could be kind enough to whip out a script, it shouldn't be too hard...
There are a few ways to do it. I tried to do it via a quick shell command but shell programming is quite ugly IMO. The best way I managed was to copy the directory entirely and the iterate through deleting the files. You'd do this using by piping the find command which returned all of the files into the remove command. It's not very nice as it can make a mess if you pick the wrong folder.
Then I went back to my beloved Python and it literally took me about 10 minutes to put this together. I can't say enough good things about this language and I'm so glad that Leopard will be able to use it system-wide as an alternative to Applescript.
If you just copy and paste this into a file and save as dircopy.py or whatever. Then basically open a terminal and type the following
python <drag in dircopy.py> <drag in dir1 you want to copy> <drag in dir 2 you want to copy to> hit return
Code:
import sys
import os
import string
if (len(sys.argv)<3):
>print "usage: dircopy dir1 dir2"
>sys.exit()
fread = os.popen('find %s -type d -print' %sys.argv[1])
folders = fread.read()
fread.close()
fnames = string.split(folders,"\
")
for f in fnames:
>strip = len(sys.argv[1])
>f = '%s%s' %(sys.argv[2][:-1],f[strip:])
>os.popen('mkdir -p "%s"' %f)
You need to replace the arrows at the front of each line that has a > mark with a tab. The forum removes the tabs and Python uses tabs for code blocks.
That is an excellent question! I've been trying to figure this out too - scanning a decade's worth of photos from negs, categorized by date in lots of nested folders. Apart from the hi-rez scans, client wants the same pics on DVD slideshows (low rez). Been trying to make an Automator action to create the same folder structure and then run a Photoshop action to save TV-rez versions of all the photos in their respective place in that structure.
If anyone figures this out it would be very helpful.
I sometimes wonder about doing things like this myself but can Photoshop do it? When you use the batch option, you can choose to put everything into one folder or overwrite the original. There have been times when I wanted to save the file to another folder, keeping the folder structure, but photoshop can't do this (unless you write an applescript/javascript to do it instead of using actions and the batch command but that's a royal pain).
If I'm missing something and this is possible, I'd love to know how to do it.
I just did a quick test and it works as advertised. I got all the folders without the files! Once this horribly long batch finishes up, I can connect the drive to the other computer and save myself hours of work by using this lovely script.
Sure glad I asked for help
Oh yeah, can this work in 10.3 or is it only 10.4?
Hmmm, kindof deprecated now that Marvin already posted a solution, but here's an applescript that does the same thing. Creates a duplicate of the folder structure of the frontmost finder window.
Here you go anyways!
Update:
The code is below but you can click here to open it in script editor and try it out. Just follow the directions in top part the script and you're good to go!
Code:
-- Open this script in script editor and save with the options:
-- * File type: Application
-- * Uncheck the option "Startup screen"
-- Now you can either drag and drop a set of folders on the application or just
-- run the application from the Finder and it will ask you to choose a folder.
on run
set source_folder to choose folder with prompt "Select folder to be duplicated:" as string
my do_main_script(source_folder)
end run
on open of source_folder_list
repeat with i from 1 to number of items in the source_folder_list
set this_folder_path to item i of the source_folder_list as string
if last character of this_folder_path is ":" then
my do_main_script(this_folder_path)
end if
end repeat
end open
on do_main_script(source_folder)
tell application "Finder" to set source_folder to folder (source_folder)
tell application "Finder" to set the target_folder to (parent of source_folder)
if source_folder is not "" and target_folder is not "" then
set new_folder_name to (name of source_folder as string) & " duplicate"
set source_folder to source_folder as string
set target_folder to target_folder as string
my create_new_folder(target_folder, new_folder_name)
my duplicate_folder_structure(source_folder, target_folder & new_folder_name & ":")
end if
end do_main_script
on duplicate_folder_structure(source_folder, target_folder)
tell application "Finder"
try
get name of folders of folder (source_folder)
set folder_list to result
repeat with i from 1 to number of items in the folder_list
set this_folder_name to item i of the folder_list as string
my create_new_folder(target_folder, this_folder_name)
thanks whyatt trash! I'm going to keep this one as it may come in handy as well. Eventually I'll be able to put all these little applescripts together to make one giant automated system and stay at home while the script is being productive for me at work
I updated the script to accept folders dropped on the application and if no folders are dropped a "open folder" dialog box. Check my post above for the updated script. Enjoy!
Hi there, is it possible to copy the Apple Double Files as well? Just now all your labels (color of files and folders) are lost!
Any ideas more than welcome!!
This will copy the Finder colors over. As before, paste it into a file, save as dircopy.py and run in the terminal using:
python {drag in dircopy.py} {drag in folder to copy from} {drag in folder to copy to} then hit return
All indented lines should have a tab to push them in but it'll probably work if they get converted to spaces too.
import sys
import os
import string
from xattr import xattr
if (len(sys.argv)<3):
print "usage: dircopy dir1 dir2"
sys.exit()
fread = os.popen('find "%s" -type d -print' %sys.argv[1])
folders = fread.read()
fread.close()
fnames = string.split(folders,"\n")
for f in fnames:
strip = len(sys.argv[1])
if(len(f)==0): continue
attrs = xattr(f)
key = u'com.apple.FinderInfo'
findervals = attrs.copy().get(key, chr(0)*32)
f = '%s%s' %(sys.argv[2],f[strip:])
os.popen('mkdir -p "%s"' %f)
attrs = xattr(f)
attrs.set(key, findervals)
I tried it and we have spaces in out folder names so amended the script with double quotes around the source path:
Thanks, I have corrected it above. Maybe it doesn't copy the backslashes e.g a path would be ~/Deskop/folder\ with\ space when dragged in, which should work but maybe its strips it out. Quotes are usually safer though.
Wow, I see lot's of fancy scripts for what is essentially a one liner.
Well yeah, I tend to use scripts as they are easier to modify for other tasks. If someone decides for example they only want to copy folders from a hierarchy that have been labelled yellow, it's a fairly trivial addition to a script but not as easy with a command-line. The command-line is fine for when you only need one line. When it gets to situations with more than one pipe, I find it takes longer to get it work properly. Plus it's easier to test the commands before running them to make sure that it won't do something disastrous.
Comments
If anyone figures this out it would be very helpful.
I don't believe so. Considering it's such a miniscule portion of the user population who would eveer need to do it, I'm not surprised. I guess Cmd-Shift-N will be your best friend.
Well, so far you have on this forum thread two users who have need of this sort of tip. I would imagine it would be useful for any number of graphics/media professionals who often need different resolutions/colour spaces etc. of the same set of files but don't have the time to manually reproduce the folder structure and don't want to kill all their disk space by duplicating the whole folder full of high-rez files before downsampling.
The Genius Bar forum is supposed to be for helping each other. Not sure why you're wasting time telling us you don't think our problem is important. Help out if you can, don't patronize.
Then I went back to my beloved Python and it literally took me about 10 minutes to put this together. I can't say enough good things about this language and I'm so glad that Leopard will be able to use it system-wide as an alternative to Applescript.
If you just copy and paste this into a file and save as dircopy.py or whatever. Then basically open a terminal and type the following
python <drag in dircopy.py> <drag in dir1 you want to copy> <drag in dir 2 you want to copy to> hit return
import sys
import os
import string
if (len(sys.argv)<3):
>print "usage: dircopy dir1 dir2"
>sys.exit()
fread = os.popen('find %s -type d -print' %sys.argv[1])
folders = fread.read()
fread.close()
fnames = string.split(folders,"\
")
for f in fnames:
>strip = len(sys.argv[1])
>f = '%s%s' %(sys.argv[2][:-1],f[strip:])
>os.popen('mkdir -p "%s"' %f)
You need to replace the arrows at the front of each line that has a > mark with a tab. The forum removes the tabs and Python uses tabs for code blocks.
That is an excellent question! I've been trying to figure this out too - scanning a decade's worth of photos from negs, categorized by date in lots of nested folders. Apart from the hi-rez scans, client wants the same pics on DVD slideshows (low rez). Been trying to make an Automator action to create the same folder structure and then run a Photoshop action to save TV-rez versions of all the photos in their respective place in that structure.
If anyone figures this out it would be very helpful.
I sometimes wonder about doing things like this myself but can Photoshop do it? When you use the batch option, you can choose to put everything into one folder or overwrite the original. There have been times when I wanted to save the file to another folder, keeping the folder structure, but photoshop can't do this (unless you write an applescript/javascript to do it instead of using actions and the batch command but that's a royal pain).
If I'm missing something and this is possible, I'd love to know how to do it.
I just did a quick test and it works as advertised. I got all the folders without the files!
Sure glad I asked for help
Oh yeah, can this work in 10.3 or is it only 10.4?
Thanks Marvin, you rule!!
Oh yeah, can this work in 10.3 or is it only 10.4?
Yeah it should work on 10.3. It has Python included by default too.
Here you go anyways!
Update:
The code is below but you can click here to open it in script editor and try it out. Just follow the directions in top part the script and you're good to go!
-- Open this script in script editor and save with the options:
-- * File type: Application
-- * Uncheck the option "Startup screen"
-- Now you can either drag and drop a set of folders on the application or just
-- run the application from the Finder and it will ask you to choose a folder.
on run
set source_folder to choose folder with prompt "Select folder to be duplicated:" as string
my do_main_script(source_folder)
end run
on open of source_folder_list
repeat with i from 1 to number of items in the source_folder_list
set this_folder_path to item i of the source_folder_list as string
if last character of this_folder_path is ":" then
my do_main_script(this_folder_path)
end if
end repeat
end open
on do_main_script(source_folder)
tell application "Finder" to set source_folder to folder (source_folder)
tell application "Finder" to set the target_folder to (parent of source_folder)
if source_folder is not "" and target_folder is not "" then
set new_folder_name to (name of source_folder as string) & " duplicate"
set source_folder to source_folder as string
set target_folder to target_folder as string
my create_new_folder(target_folder, new_folder_name)
my duplicate_folder_structure(source_folder, target_folder & new_folder_name & ":")
end if
end do_main_script
on duplicate_folder_structure(source_folder, target_folder)
tell application "Finder"
try
get name of folders of folder (source_folder)
set folder_list to result
repeat with i from 1 to number of items in the folder_list
set this_folder_name to item i of the folder_list as string
my create_new_folder(target_folder, this_folder_name)
my duplicate_folder_structure(source_folder & this_folder_name & ":", target_folder & this_folder_name & ":")
end repeat
end try
end tell
end duplicate_folder_structure
on create_new_folder(target_folder, new_folder_name)
tell application "Finder"
try
if not (exists item (target_folder & new_folder_name)) then
make new folder at folder target_folder with properties {name:new_folder_name}
end if
end try
end tell
end create_new_folder
Thank you Wyatt Thrash. I know this thread is old - but years later you are saving me a ton of time. This script rocks!
Hi there, is it possible to copy the Apple Double Files as well? Just now all your labels (color of files and folders) are lost!
Any ideas more than welcome!!
Michael
This will copy the Finder colors over. As before, paste it into a file, save as dircopy.py and run in the terminal using:
python {drag in dircopy.py} {drag in folder to copy from} {drag in folder to copy to} then hit return
All indented lines should have a tab to push them in but it'll probably work if they get converted to spaces too.
Thanks for that Marvin
I tried it and we have spaces in out folder names so amended the script with double quotes around the source path:
import sys
import os
import string
from xattr import xattr
if (len(sys.argv)<3):
print "usage: dircopy dir1 dir2"
sys.exit()
fread = os.popen('find "%s" -type d -print' %sys.argv[1])
folders = fread.read()
fread.close()
fnames = string.split(folders,"\n")
for f in fnames:
strip = len(sys.argv[1])
if(len(f)==0): continue
attrs = xattr(f)
key = u'com.apple.FinderInfo'
findervals = attrs.copy().get(key, chr(0)*32)
f = '%s%s' %(sys.argv[2],f[strip:])
os.popen('mkdir -p "%s"' %f)
attrs = xattr(f)
attrs.set(key, findervals)
Thanks, I have corrected it above. Maybe it doesn't copy the backslashes e.g a path would be ~/Deskop/folder\ with\ space when dragged in, which should work but maybe its strips it out. Quotes are usually safer though.
Thanks to JasonWD and Marvin
Michael
Hey,
rsync will do what you need.
Here's one I made earlier:
rsync -a [/path/from/] [/path/to/] --include */ --exclude *
Easy!
JK
$ mkdir /dir2
$ cd /dir1
$ find . -type d | cpio -pvdm ../dir2
Well yeah, I tend to use scripts as they are easier to modify for other tasks. If someone decides for example they only want to copy folders from a hierarchy that have been labelled yellow, it's a fairly trivial addition to a script but not as easy with a command-line. The command-line is fine for when you only need one line. When it gets to situations with more than one pipe, I find it takes longer to get it work properly. Plus it's easier to test the commands before running them to make sure that it won't do something disastrous.