Here is a pure perl method I made up after endless searching and having people tell me to use modules. This is such a simple concept, a required external module seems pretty lame. I have also made a recursive directory deleter so I/You can eliminate the need for File::Path qw(remove_tree) and File::Copy::Recursive qw(rcopy) altogether. If you use these together you can easily conduct a dir_move. And finally I included an fmove(); (you could use rename for certain instances obviously and fmove is identical to fcopy except for a final unlink) but if you were to combine fmove() into dircopy you could make a single sub/function to move a folder in one shot instead of recursive copy and then recursive delete.

#!/usr/bin/env perl use strict; use warnings; dircopy($ARGV[0],$ARGV[1]); sub dircopy { my @dirlist=($_[0]); my @dircopy=($_[1]); until (scalar(@dirlist)==0) { mkdir "$dircopy[0]"; opendir my($dh),$dirlist[0]; my @filelist=grep {!/^\.\.?$/} readdir $dh; for my $i (0..scalar(@filelist)-1) { if ( -f "$dirlist[0]/$filelist[$i]" ) { fcopy("$dirlist[0]/$filelist[$i]","$dircopy[0]/$filelist[$ +i]"); } if ( -d "$dirlist[0]/$filelist[$i]" ) { push @dirlist,"$dirlist[0]/$filelist[$i]"; push @dircopy,"$dircopy[0]/$filelist[$i]"; } } closedir $dh; shift @dirlist;shift @dircopy; } } sub fcopy { my ($i,$data,$cpo,$cpn); open($cpo,"<",$_[0]) or die $!; binmode($cpo); open($cpn,">",$_[1]) or die $!; binmode($cpn); while (($i=sysread $cpo,$data,4096)!=0){print $cpn $data}; close($cpn);close($cpo); }

And here is the recursive directory deleter...

#!/usr/bin/env perl use strict; use warnings; dir_del($ARGV[0]); sub dir_del { my @dirlist=("$_[0]"); my $r=0; my $s=scalar(@dirlist); while ( $r < $s ) { opendir my($dh),$dirlist[$r]; my @filelist=grep {!/^\.\.?$/} readdir $dh; for my $i ( 0..scalar(@filelist)-1 ) { if ( -f "$dirlist[$r]/$filelist[$i]" ) { unlink("$dirlist[$r]/$filelist[$i]"); } else { push @dirlist,"$dirlist[$r]/$filelist[$i]"; $s=scalar(@dirlist); } } closedir $dh;$r+=1; } my $a=scalar(@dirlist)-1; for my $i ( 0..scalar(@dirlist)-1 ) { rmdir($dirlist[$a]);$a-=1; } }

Just a side note: When using these make sure you run -d tests if you are not certain your target is a folder or a file. If it's just a file you can } else { unlink(file);}

sub fmove { my ($i,$data,$mvo,$mvn); open($mvo,"<",$_[0]) or die $!; binmode($mvo); open($mvn,">",$_[1]) or die $!; binmode($mvn); while (($i=sysread $mvo,$data,4096)!=0){print $mvn $data}; close($mvn);close($mvo);unlink($_[0]); }

In reply to Re: Copy folders by SM177Y
in thread Copy folders by avik1612

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.