Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 140
  • Last Modified:

Perl script to delete older files

Hi - I am storing all the files that come in a folder (example folderA), all the files have same name so I store them using time stamp after compressing them.
So for example folderA has the below files.
testfile2_20161102124335.zip
testfile2_20161102225357.zip
testfile2_20161102230409.zip
testfile2_20161102231907.zip
testfile2_20161102232440.zip
testfile2_20161102232811.zip
testfile2_20161102234703.zip
testfile2_20161102235149.zip
testfile2_20161102235825.zip
testfile2_20161103000507.zip
testfile2_20161103000659.zip
testfile2_20161106233202.zip

so if you observe i have the same filename but with different timestamp, i am storing these files as archive files and i want to automatically delete the old files if the count goes above 10. I mean if I have 10 files in the folder and if i get 11th file then i want to delete the oldest file before inserting the new one into the folder, so that there will always be 10 archived files at any time.

I did this using shell script as below but not able to do it in perl, so help me how to do this in perl.

for file in `ls $folderA`; do
    name=${file%.*}
noGzFile=`ls $folderA | grep $name | wc -l | awk '{print $1}'`
    #echo $noGzFile
    if [[ $noGzFile -gt 10 ]]; then
        noGzDel=$((noGzFile - 10))
        for f in `ls $folderC | grep $name | sort | head -$noGzDel`; do
            rm $folderC/$f
        done
    fi
done

Open in new window

0
shragi
Asked:
shragi
  • 2
  • 2
  • 2
1 Solution
 
FishMongerCommented:
What have you tried?
What warnings and errors do you get?
What part of the task do you not know how to do?

Please post your code.
0
 
wilcoxonCommented:
This should do what you want:
use strict;
use warnings;
my $dir = shift or die "Usage: $0 directory\n";
opendir DIR, $dir or die "could not open dir $dir: $!";
my @files = sort grep m{^testfile2_\d+\.zip$}, readdir DIR;
closedir DIR;
if (@files > 9) {
    splice @files, -9; # remove 10 newest files from list
    foreach my $fil (@files) {
        unlink "$dir/$fil" or die "could not delete $dir/$fil: $!";
    }
}
# compress the new file
# move it to the directory

Open in new window

0
 
shragiAuthor Commented:
Hi wilcoxon - the script works i made one simple change to the script and got the below error, can you fix it please

Global symbol "$fnameNoExt _" requires explicit package name at deleteOldFiles.pl line
6.
Execution of deleteOldFiles.pl aborted due to compilation errors.
Press any key to continue . . .

Open in new window



use strict;
use warnings;
my $dir = shift or die "Usage: $0 directory\n";
opendir DIR, $dir or die "could not open dir $dir: $!";
my $fnameNoExt = "testfile2";
my @files = sort grep m{^$fnameNoExt _\d+\.zip$}, readdir DIR;
closedir DIR;
if (@files > 9) {
    splice @files, -9; # remove 10 newest files from list
    foreach my $fil (@files) {
        unlink "$dir/$fil" or die "could not delete $dir/$fil: $!";
    }
}
# compress the new file

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
FishMongerCommented:
Undo the "simple change" you made which you didn't show us.
0
 
wilcoxonCommented:
I don't see what is causing that error in the code you posted but I do see another error.  This may fix your issue (if the code you posted was not exact).
use strict;
use warnings;
my $dir = shift or die "Usage: $0 directory\n";
opendir DIR, $dir or die "could not open dir $dir: $!";
my $fnameNoExt = 'testfile2';
my @files = sort grep m{^${fnameNoExt}_\d+\.zip$}, readdir DIR;
closedir DIR;
if (@files > 9) {
    splice @files, -9; # remove 10 newest files from list
    foreach my $fil (@files) {
        unlink "$dir/$fil" or die "could not delete $dir/$fil: $!";
    }
}
# compress the new file

Open in new window

0
 
shragiAuthor Commented:
Thank you Wilcoxon that fixed my error
grep m{^${fNameNoExt}_

Using additional bracket for variable fixed my error.

Thanks
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now