or Connect
AppleInsider › Forums › General › Genius Bar › copy folder structure without copying files
New Posts  All Forums:Forum Nav:

copy folder structure without copying files

post #1 of 26
Thread Starter 
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.

Any suggestions?
post #2 of 26
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.
post #3 of 26
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.
Apple Product Professional
Apple Certified Macintosh Technician
Reply
Apple Product Professional
Apple Certified Macintosh Technician
Reply
post #4 of 26
Quote:
Originally Posted by Karelia View Post

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.
post #5 of 26
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...
"I've learned there's more to life than being really, really, really, really, really, really, ridiculously good-looking. :-x" - Zoolander
~:My scraps:~
Reply
"I've learned there's more to life than being really, really, really, really, really, really, ridiculously good-looking. :-x" - Zoolander
~:My scraps:~
Reply
post #6 of 26
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.
post #7 of 26
Thread Starter 
Quote:
Originally Posted by nickmini View Post

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.
post #8 of 26
Thread Starter 
Marvin rules!!

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?

Thanks Marvin, you rule!!
post #9 of 26
Quote:
Originally Posted by FreddyFredFred View Post

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.
post #10 of 26
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)
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
"I've learned there's more to life than being really, really, really, really, really, really, ridiculously good-looking. :-x" - Zoolander
~:My scraps:~
Reply
"I've learned there's more to life than being really, really, really, really, really, really, ridiculously good-looking. :-x" - Zoolander
~:My scraps:~
Reply
post #11 of 26
Thread Starter 
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
post #12 of 26
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!
"I've learned there's more to life than being really, really, really, really, really, really, ridiculously good-looking. :-x" - Zoolander
~:My scraps:~
Reply
"I've learned there's more to life than being really, really, really, really, really, really, ridiculously good-looking. :-x" - Zoolander
~:My scraps:~
Reply
post #13 of 26

Thank you Wyatt Thrash. I know this thread is old - but years later you are saving me a ton of time. This script rocks!

post #14 of 26

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

post #15 of 26
Quote:
Originally Posted by maccie View Post

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.
Code:
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)
post #16 of 26

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)
post #17 of 26
Quote:
Originally Posted by JasonWD View Post

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:

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.
post #18 of 26

Thanks to JasonWD and Marvin

Michael
 

post #19 of 26

Hey,

 

rsync will do what you need.

 

Here's one I made earlier:

 

 

rsync -a [/path/from/] [/path/to/] --include */ --exclude *

 

Easy!

 

JK

post #20 of 26
Wow, I see lot's of fancy scripts for what is essentially a one liner.

$ mkdir /dir2
$ cd /dir1
$ find . -type d | cpio -pvdm ../dir2
When I looked up "Ninjas" in Thesaurus.com, it said "Ninja's can't be found" Well played Ninjas, well played.
Reply
When I looked up "Ninjas" in Thesaurus.com, it said "Ninja's can't be found" Well played Ninjas, well played.
Reply
post #21 of 26
Quote:
Originally Posted by Relic View Post

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.
post #22 of 26
Quote:
Originally Posted by Marvin View Post

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.

find . -type d -print0 | cpio -o -O > DIRLIST (To test before actually doing anything)
To extract it:
cpio -i < DIRLIST (then do it)

Well sure, but in this case I think a script would just add complexity to a simple solution. I do like your script though, love Python as well but I normally use Perl or CShell when dealing with files.


#!/usr/bin/perl
use strict;
use warnings;
use File::Find;
use File::Path;

my ($source, $target) = @ARGV;
my @paths;
find sub {push @paths, "$target/$File::Find::name" if -d and not
/^\.{1,2}$/;}, $source;
mkpath "$_" for @paths;


Oh here is one more way of doing it;

cd destination/dir
find /source/dir -type d -printf "%P\n" | xargs mkdir -p
Edited by Relic - 6/2/13 at 2:52pm
When I looked up "Ninjas" in Thesaurus.com, it said "Ninja's can't be found" Well played Ninjas, well played.
Reply
When I looked up "Ninjas" in Thesaurus.com, it said "Ninja's can't be found" Well played Ninjas, well played.
Reply
post #23 of 26

Easy? Not in my world. Changed the path names and copied/pasted to terminal. Just errors, errors, errors. May be i.e spaces or umlauts are still too much. Tried to mask them, but did help nothing. Maybe you first have to learn the complete language from ground up before using a single bit. 

 

Finally the AppleScript by Whyatt Thrash did the job. Thank You! Slow, but much faster them watching errors in terminal. 

post #24 of 26
Quote:
Originally Posted by YingYian View Post
 

Easy? Not in my world. Changed the path names and copied/pasted to terminal. Just errors, errors, errors. May be i.e spaces or umlauts are still too much. Tried to mask them, but did help nothing. Maybe you first have to learn the complete language from ground up before using a single bit. 

 

Finally the AppleScript by Whyatt Thrash did the job. Thank You! Slow, but much faster them watching errors in terminal. 

Would it be possible to see the errors you were getting in the terminal, copy the echoed text to this thread or do a screen shot. It's a very simple fix ounce I know where the terminal commands are stalling at.

When I looked up "Ninjas" in Thesaurus.com, it said "Ninja's can't be found" Well played Ninjas, well played.
Reply
When I looked up "Ninjas" in Thesaurus.com, it said "Ninja's can't be found" Well played Ninjas, well played.
Reply
post #25 of 26

Using a Python script or terminal commands would be much faster than a Applescript.

When I looked up "Ninjas" in Thesaurus.com, it said "Ninja's can't be found" Well played Ninjas, well played.
Reply
When I looked up "Ninjas" in Thesaurus.com, it said "Ninja's can't be found" Well played Ninjas, well played.
Reply
post #26 of 26

JK, thanks for this! Here's a little improvement for those who like to keep their folder icons and labels:

 

rsync -Ea [/path/from/] [/path/to/] --include="*/" --include="Icon*" --exclude="*"

New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Genius Bar
AppleInsider › Forums › General › Genius Bar › copy folder structure without copying files