copy folder structure without copying files

Posted:
in Genius Bar edited July 2014
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?
«1

Comments

  • Reply 1 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.
  • Reply 2 of 26
    kareliakarelia Posts: 525member
    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.
  • Reply 3 of 26
    nickmininickmini Posts: 74member
    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.
    SpamSandwich
  • Reply 4 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...
  • Reply 5 of 26
    MarvinMarvin Posts: 15,326moderator
    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.
  • Reply 6 of 26
    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.
  • Reply 7 of 26
    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!!
  • Reply 8 of 26
    MarvinMarvin Posts: 15,326moderator
    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.
  • Reply 9 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



  • Reply 10 of 26
    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
  • Reply 11 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!
  • Reply 12 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!

  • Reply 13 of 26
    macciemaccie Posts: 2member


    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

  • Reply 14 of 26
    MarvinMarvin Posts: 15,326moderator
    maccie wrote: »
    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)
    
  • Reply 15 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)

  • Reply 16 of 26
    MarvinMarvin Posts: 15,326moderator
    jasonwd wrote: »
    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.
  • Reply 17 of 26
    macciemaccie Posts: 2member


    Thanks to JasonWD and Marvin


    Michael

     

  • Reply 18 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

  • Reply 19 of 26
    relicrelic Posts: 4,735member
    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
  • Reply 20 of 26
    MarvinMarvin Posts: 15,326moderator
    relic wrote: »
    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.
Sign In or Register to comment.