Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery
 
PerlMonks  

How do I copy an empty directory?

by Anonymous Monk
on Oct 02, 2012 at 20:20 UTC ( #996906=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

This question seems so simple, I am almost embarrassed to ask it but yet I can't figure it out so here goes. I have an empty directory name, x:\this\directory\name\is\empty. How can I move the empty directory named "empty" at the end of the path to c:\temp for example to make c:\temp\empty?

The operating system is Windows in case that matters and I actually thought about using a system call to either xcopy or robocopy and neither of them will do it either, even with all attempts at using their switches such as /e /s /t, and more. All I can seem to copy is all the data UNDER the directory if it were NOT empty but again, some directories in my path are empty and some are not so I need to copy them all, either way. Any ideas would certainly be welcome. Thank you.

Comment on How do I copy an empty directory?
Re: How do I copy an empty directory?
by hominid (Curate) on Oct 02, 2012 at 20:25 UTC
    Create a new directory with the desired name and then delete the old directory.
      As mentioned in the original post, some directories are not empty. How would this deal with those?
        The question, smartass, was "How can I move the empty directory named "empty" at the end of the path to c:\temp for example to make c:\temp\empty?"
Re: How do I copy an empty directory?
by Kenosis (Priest) on Oct 02, 2012 at 21:05 UTC

    Although hominid's solution makes a lot of sense, here's another option:

    use strict; use warnings; use File::Copy::Recursive qw/ dirmove /; dirmove( 'x:\this\directory\name\is\empty', 'c:\temp\empty' ) or die $ +!;
Re: How do I copy an empty directory?
by kcott (Abbot) on Oct 02, 2012 at 21:15 UTC

    Have you tried File::Copy?

    Your node title has "How do I copy ..."; your node content has "How can I move ...". File::Copy has functions for both operations.

    Update: It appears I was wrong about the copy() part; although, move() seems to work fine. See discussion below.

    -- Ken

      Correct me if I am wrong, but File::Copy copies files and not directories.

        Well, it seems you're correct with respect to File::Copy::copy(); however, File::Copy::move() seems to work fine.

        I tried copy() on an empty directory and just got an empty (normal) file. I then tried move() on an empty directory and that worked; move() on a populated directory also worked, i.e. moved the directory and its contents.

        If you're the OP, sorry for the bum steer. I'll update my post.

        -- Ken

Re: How do I copy an empty directory?
by pvaldes (Chaplain) on Oct 03, 2012 at 08:33 UTC
    #!/usr/bin/perl use File::Find; finddepth (\&empdir, ($ARGV[0] || '.')); sub empdir { return unless -d && rmdir($_); mkdir "/tmp/$_"; }

    Works for me in GNU. Any "empty" directory having only empty subdirectories will be deleted also (as the empty subdirs). Adapt the line 7 to your needs

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://996906]
Approved by hominid
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-12-28 05:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (178 votes), past polls