I want to archive all txt files using File :: Find, delete the source files and delete the empty directories.
I'm having difficulty renaming files with $$ tar-> rename (); because I would like to disconnect them from their full path names and use only the parent directory / *. txt, but everything I try renames only one file.
I donβt know where is the appropriate place to execute the "unlink" function.
Thank.
use strict;
use warnings;
use Archive::Tar;
use File::Find;
use File::Basename;
my $dir = "E:/";
my @files = ();
find(\&archive, $dir);
sub archive {
/\.txt$/ or return;
my $fd = $File::Find::dir;
my $fn = $File::Find::name;
my $folder = basename($fd);
my $file = $_;
push @files, $fn;
my $tar = Archive::Tar->new();
$tar->add_files(@files);
$tar->rename( $fn, $folder."\\".$file );
$tar->write($fd.'.tar');
unlink $fn;
finddepth(sub{rmdir},'.');
}
source
share