Created
June 24, 2009 10:57
-
-
Save ankurs/135169 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl | |
use strict; | |
use File::Glob ':glob'; # for whitespace problem with normal glob | |
use File::Copy; # for copying files | |
use strict; | |
my @dir; # will hold all original dirs | |
my @files; # will hold all original files | |
my @ndir; # will hold all new dirs | |
my @nfiles; # will hold all new files | |
sub gen_list | |
{ | |
my $dir = pop; # taking the first argument | |
push @dir,$dir; | |
foreach(@dir) # finding all the dirs and files ans storing them | |
{ | |
my @in =<$_/*>; | |
foreach (@in) | |
{ | |
if (-d $_) # for dir | |
{ | |
push @dir,$_; | |
} | |
else # if not dir then file | |
{ | |
push @files,$_; | |
} | |
} | |
} | |
@nfiles = @files; # copying data to nfiles | |
@ndir = @dir; # copying data to ndir | |
} | |
sub gen_nlist | |
{ | |
my $ddir = pop; # second argument | |
my $odir = pop; # first argument | |
foreach(@ndir) | |
{ | |
s/$odir/$ddir/; # changind old dir to new dir | |
} | |
foreach(@nfiles) | |
{ | |
s/$odir/$ddir/; # changing old dir to new dir | |
} | |
} | |
sub do_copy | |
{ | |
foreach(@ndir) | |
{ | |
mkdir($_); # creating the required folders | |
} | |
my $con=1; | |
while($con) | |
{ | |
my $ofile = shift @files; | |
my $nfile = shift @nfiles; | |
if ($ofile and $nfile) | |
{ | |
copy($ofile,$nfile); # copying the actual files | |
} | |
else | |
{ | |
$con=0; | |
} | |
} | |
} | |
# usage | |
my $originDir = "/home/ankur/code/perl"; # copy from | |
my $destDir="/home/ankur/code/perl/copy"; # copy to | |
&gen_list($originDir); | |
&gen_nlist($originDir,$destDir); | |
&do_copy(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment