Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Catching . .. in file listing

by deadpickle (Pilgrim)
on May 01, 2009 at 16:43 UTC ( [id://761335]=perlquestion: print w/replies, xml ) Need Help??

deadpickle has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to handle files in a directory and everytime I try to pass the . and .. that come with this I get this error:
C:\Documents and Settings\UAS\Desktop\GRRUVI\gps\. stat() on closed filehandle KID at GRRUVI-v1.611-gpsa.pl line 1950. readline() on closed filehandle KID at GRRUVI-v1.611-gpsa.pl line 1951 +.
which tells me that it is trying to open the . or .. file. Any ideas on how I can get around this?
opendir(GPS,"$new_dir") or die "Cannot open direct +ory"; my @GPS = readdir(GPS); closedir(GPS); foreach my $kid (@GPS){ #~ if ($kid ne "." or $kid ne ".."){ if (defined($kid)){ if ($kid !~ /.bak/ or $kid !~ /.txt/ o +r $kid !~ /^\.{1,2}$/){ #~ if ($kid !~ /.bak/ or $kid !~ /.txt +/){ open KID , '<', "$new_dir\\$kid"; my ($dev,$ino,$mode,$nlink,$uid,$g +id,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat(KID); my $data = <KID>; close KID; if (defined($data)){ my @data = split(" ", $data); my $diff = time-$mtime; if ($diff >= 3600){ unlink($kid); } elsif ($diff >= 20){ } } } #~ } }

Replies are listed 'Best First'.
Re: Catching . .. in file listing
by kennethk (Abbot) on May 01, 2009 at 16:55 UTC

    First, your commented if clause would work if you changed your "or" to an "and" - a valid choice must be both not "." and not "..". Also, in regular expressions, . is a wildcard character, so they will match far more than you intend - see perlretut.

    If you wanted to avoid some layers of conditional nesting, you could use next as a short circuit on your file tests. Something like:

    opendir(GPS,"$new_dir") or die "Cannot open directory"; my @GPS = readdir(GPS); closedir(GPS); foreach my $kid (@GPS){ next if $kid eq "."; next if $kid eq ".."; next if not defined $kid; next if $kid =~ /\.bak$/; next if $kid =~ /\.txt$/; open KID , '<', "$new_dir\\$kid" or die "Open failure on $kid: $!" +; my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ct +ime,$blksize,$blocks) = stat(KID); my $data = <KID>; close KID; if (defined($data)){ my @data = split(" ", $data); my $diff = time-$mtime; if ($diff >= 3600){ unlink($kid); } elsif ($diff >= 20){ } } }

    Update: I forgot to mention you should be checking whether open succeeds or not. As well, you may consider checking out the file tests available in -X.

Re: Catching . .. in file listing
by toolic (Bishop) on May 01, 2009 at 17:05 UTC
    If you only want files, then just grab files and ignore all directories:
    use strict; use warnings; opendir my $dh, $new_dir or die "Cannot open directory"; my @GPS = grep { -f "$new_dir/$_" } readdir($dh); closedir $dh;
      my @GPS = grep { -f "$new_dir/$_" } readdir($dh);

      It looks like the OP is on Windows so this is a bit OT but if you were on *nix you might want to consider adding and not -l "$new_dir/$_" into the grep depending on how you want to treat symbolic links.

      I hope this is of interest.

      Cheers,

      JohnGG

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://761335]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2024-04-23 15:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found