CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Recursively delete *.bak files

 



bulrush
Novice

Feb 6, 2012, 7:21 AM

Post #1 of 5 (1304 views)
Recursively delete *.bak files Can't Post

System: Sun Solaris and Perl 5.8.8

I need to delete all *.bak files recursively. Actually this is a test program to delete files that start with ".", which are some type of backup file for Adobe Photoshop or Illustrator. But I'm starting writing my own program to delete *.bak files. Why? Because unix 'find' does not delete any files that begin with ".".

Also note, I cannot use any Perl packages which use the unix 'find' command due to its limitations.

Here is my core program which checks to see if the file is a file or directory.

Code
sub procdir 
{my($curdir)=@_; # procdir
my($procname,$s,$t,@a,@b,$i,$fn,$fullfile);
my($tdir);

$procname="procdir";

$s="\n$procname: Going into dir _".$curdir."_, testing $myspec";
writeerr($s);

opendir(MYDIR, $curdir) or die "ERROR $procname: could not open dir $curdir";
# Get one file at a time.
while ( defined($file = readdir(MYDIR)) )
{
$d=$curdir;
#$d=~s/\///g; # Drop slashes.
$fullfile=$d.'/'.$file;
#$s="Testing entry $fullfile against $myspec"; # DEBUG
#writeerr($s); # DEBUG
$seenfncnt++;
if (-f $fullfile)
{
$s="$procname: Checking file $fullfile";
writeerr($s);
}

#if ($fullfile=~m/$myspec/)
if ($fullfile=~m/\*\.bak$/) # <==PROBLEM HERE
{
$s=" Deleting $fullfile";
writeerr($s);
#unlink($fullfile) or die "ERROR $procname: Could not delete file $fullfile";
$pcnt++;
}
elsif (-d $fullfile) # If it's a directory
{
# Ignore current and parent directories.
if ($file!~m/^(\.|\.\.)$/)
{
$seenfncnt--;
$tdir=$curdir.'/'.$file;
procdir($tdir); # Process directory
}
}
#else {
# $s="ERROR $procname: Unknown file type: $file";
# writeerr($s);
# }
} # while
closedir(MYDIR);

return; # procdir
}


My problem is with this line:
if ($fullfile=~m/\*\.bak$/) # <==PROBLEM HERE

When I get to a file named "testdir/dir1/test1.bak" the file should get deleted, but it does not past the IF statement. Why?

Program is called from shell like this:
perl sweep.pl testdir \*\.bak

$myspec is ARG[1], the filespec, or *.bak.

Thanks.
-----
* Redhat Linux RHEL 5.5.56
* Perl 5.8.8

(This post was edited by bulrush on Feb 6, 2012, 7:24 AM)


BillKSmith
Veteran

Feb 6, 2012, 12:11 PM

Post #2 of 5 (1296 views)
Re: [bulrush] Recursively delete *.bak files [In reply to] Can't Post

You are confusing regular expressions with shell wild cards.



You can either read the directory with glob (perldoc -f glob) or read it with readdir and parse the filename strings with regular expressions (perldoc perlre)

The expression argument for glob can contain unix shell wildcards.
Good Luck,
Bill


bulrush
Novice

Feb 7, 2012, 4:34 AM

Post #3 of 5 (1285 views)
Re: [BillKSmith] Recursively delete *.bak files [In reply to] Can't Post

I am using readdir(), doesn't that return a string? I then construct a string ($fullfile) which has the relative path of the filename. So why isn't my regex comparison of the string ($fullfile) filename working?
-----
* Redhat Linux RHEL 5.5.56
* Perl 5.8.8

(This post was edited by bulrush on Feb 7, 2012, 4:35 AM)


BillKSmith
Veteran

Feb 7, 2012, 6:38 AM

Post #4 of 5 (1279 views)
Re: [bulrush] Recursively delete *.bak files [In reply to] Can't Post

Your regular expression would match any string (file name) that ends in '*.bak'. The star is not a valid filename charachter, so your expression never matches anything.

You need a regular expression that matches any file name that ends in '.bak'. This task has nothing to do with the shell or wildcards. Remove '\*' from your regular expression.
Good Luck,
Bill


FishMonger
Veteran / Moderator

Feb 7, 2012, 7:05 AM

Post #5 of 5 (1276 views)
Re: [bulrush] Recursively delete *.bak files [In reply to] Can't Post

Why waste your time reinventing the wheel? Use either Perl's File::Find or File::Find::Rule module.

http://search.cpan.org/~flora/perl-5.14.2/lib/File/Find.pm
http://search.cpan.org/~rclamp/File-Find-Rule-0.33/lib/File/Find/Rule.pm

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives