Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

A friend writes to me, asking, perl-wise: "Do you know if there is a move folder module?

I know there's a file::copy module that moves files, but i wondered if there was one that acts at the folder level and takes care of moving everything inside them folders instead of having to walk the folder trees."

...and I, a mere novice, have no answer for him. Advice appreciated.

Replies are listed 'Best First'.
Re: Moving directories
by btrott (Parson) on Jun 27, 2000 at 09:44 UTC
    If I understand what you're asking--and it's quite possible that I don't :)--File::Copy should work just fine for your needs:
    use File::Copy; move("dir1", "dir2");
    ... I'm not sure if this is OS-dependent or not. My File::Copy::move first tries to use rename to move the directory. Looking at the rename docs, I see:
    Other restrictions include whether it works on directories...
    You might just try it out and see if it works. Have you tried that?
      <KBD>> move("dir 1", "dir2");</KBD>

      Up to now I've written only wimpy little scripts (aside from that replacement for getopts :)
      and have had no occasion to use file::copy...until a couple of minutes ago.

      It worked as advertised given the suggestion quoted above (same drive). It failed
      ("Permission denied") if the destination was a not-yet-created directory on another drive.

      It didn't error out--but still didn't move anything--when the destination directory,
      on another drive, existed at the time the command was given.

      The friend gave few details in his e-mail but I'm guessing he wants to move entire trees
      from one drive to another.

        If you want to move whole trees then you could do something using File::Find. Something like this perhaps (untested)

        use File::Find; use File::Copy; my $src = '/some/directory/'; my $tgt = '/some/other/directory/'; find($src, \&move_it); sub move_it { if (-d $File::Find::name) { $File::Find::name =~ s/$src/$tgt; mkdir $File::Find::name, 0777 unless -d $File::Find::name; } else { my $new = $File::Find::name; $new =~ s/$src/$tgt; copy($File::Find::name, $new); } }
        --
        <http://www.dave.org.uk>

        European Perl Conference - Sept 22/24 2000
        <http://www.yapc.org/Europe/>
RE: Moving directories
by merlyn (Sage) on Jun 27, 2000 at 19:51 UTC
    Last I checked, the rename operation worked on both files and directories, unless you had to cross filesystems. Did you try that?

    -- Randal L. Schwartz, Perl hacker

what about a system call to mv?
by mcwee (Pilgrim) on Jun 27, 2000 at 17:44 UTC
    Is there any reason (apart from platform dependencies) not to do this using
    system "mv /dir/* /newdir/"
    or somesuch similar thing? I doubt this would address the "moving to diff dirs on diff drives" issue-- I'm just curious if this is a workable solution. (being a lazy monk, I haven't monkeyed much with File::copy My knee jerk reaction in such a situation is to make the call, and portability be damned.)

    The Autonomic Pilot; it's FunkyTown, babe.

        Not just in taint mode, but properly untainting (see Untaint.pm, and perlsec), but also calling system in a safe way. Read perlsec (I wish it was named perlsex, since that is what I always seem to type) over to see how to safely make system() calls.

        Cheers,
        KM