Solved

Perl script to delete older files

Posted on 2016-11-08
6
88 Views
Last Modified: 2016-11-12
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
Comment
Question by:shragi
  • 2
  • 2
  • 2
6 Comments
 
LVL 28

Expert Comment

by:FishMonger
ID: 41878957
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
 
LVL 26

Accepted Solution

by:
wilcoxon earned 500 total points
ID: 41879831
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
 

Author Comment

by:shragi
ID: 41882276
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 28

Expert Comment

by:FishMonger
ID: 41882293
Undo the "simple change" you made which you didn't show us.
0
 
LVL 26

Expert Comment

by:wilcoxon
ID: 41882306
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
 

Author Comment

by:shragi
ID: 41882319
Thank you Wilcoxon that fixed my error
grep m{^${fNameNoExt}_

Using additional bracket for variable fixed my error.

Thanks
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question