Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

File Attributes in Win32

by azatoth (Curate)
on May 01, 2001 at 18:34 UTC ( #76971=perlquestion: print w/replies, xml ) Need Help??

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

Hi Guys. Got a question for you.

I am tinkering around with Perl & Win32. I am using Activestate Perl 5.6 (Active build 630). And I am experiencing some strange behaviour. Please take a look at the code posted below :

#!perl -w use strict; print "Enter Directory Path : \n"; chomp(my $dirPath = <STDIN>); opendir (DIR, "$dirPath") || die "Fscked Filesystem: $!\n"; my @fileList = grep(!/^\.\.?$/, readdir (DIR)); closedir (DIR); foreach my $file (@fileList) { check_files($file,$dirPath); } ## SUBS ## sub check_files { my $file = shift; my $dirPath = shift; if (-A $file > 30) { print "$file has not been used for over 30 days. Delete? [y/n +]\n"; chomp(my $ans = <STDIN>); if ($ans =~ /^y/) { unlink ("$dirPath\\$file") || die "Could Not Remove $file +: $!\n"; print "Deleting $dirPath\\$file...\n"; } else { print "Skipping File...\n"; } } }
It is a little script that searches through a directory, checks the last modification of a file, and prompts the user for deletion if it meets certain criteria. As I said, I'm just tinkering. Now, the code at the moment checks for last usage of 30 days or under, here :

if  (-A $file < 30)

And this will work. But, I want to have this :

 if (-A $file > 30)

Where it will check for usage of 30 days or more. This is where Perl coughs and splutters and throws

Use of uninitialized value in numeric gt (>) at....

What's up with that?

Azatoth a.k.a Captain Whiplash

Make Your Die Messages Full of Wisdom!
Get YOUR PerlMonks Stagename here!
Want to speak like a Londoner?

Replies are listed 'Best First'.
Re: File Attributes in Win32
by Eureka_sg (Monk) on May 01, 2001 at 18:55 UTC

    I could be wrong but I think that when you use -A on the filename, you need to specify the path as well unless the file is located in the same directory as where you call the script.( similiar to your unlink ("$dirPath\\$file") statement)

    As a result, undef is being returned instead of the number of days since the file couldn't be found.

Re: File Attributes in Win32
by c-era (Curate) on May 01, 2001 at 18:45 UTC
    When it is false the '-A $file' returns null (which is considered a string). You can use this code to correct it:
    if (-A $file || 0 > 30)
    The '||' will use 0 to do the compair if -A $file is null (and no more warning).

    Update: I don't know why I missed this (thanks to the people below who brought it up). '-A $file' is returning false because you are getting a directory listing for a directory and then checking the files in the current directory. Use this if statement:

    if (-A "$dirPath\\$file")
    Then you don't even need the '|| 0'.

      You have a precedence error. Your code should be: if(  ( -A $file || 0 ) > 30  ) { || is a logical operator so it binds less tightly than comparison operators because you usually use it like: if(  -A $file > 30  ||  -M $file > 60  ) { and precedence is designed so that most common usages don't require parens (except for the bitwise operators which inheritted their broken precedence from C).

              - tye (but my friends call me "Tye")
on fsck
by petdance (Parson) on May 01, 2001 at 19:16 UTC
    A side note unrelated to the question...

    In your program, you have this line:

    opendir (DIR, "$dirPath") || die "Fscked Filesystem: $!\n";
    You may not be aware that fsck actually means something beyond the cute little "almost" profanity. It's a standard Unix command that checks the validity of a filesystem, hence "fsck", for "filesystem check".

    So your error message of "Fscked Filesystem" could be misleading because if that error comes up, 1) you didn't actually do any filesystem checking and 2) you couldn't anyway 'cause you're on a Windows box.

    Oh, and I'd drop the quotes around $dirPath. They serve no purpose.


    %_=split/;/,".;;n;u;e;ot;t;her;c; ".   #   Andy Lester
    'Perl ;@; a;a;j;m;er;y;t;p;n;d;s;o;'.  #
    "hack";print map delete$_{$_},split//,q<   >
      I am aware actually. I'm using it for the cute little profanity.

      Thanks for pointing this valuable information out.
Re: File Attributes in Win32
by jeroenes (Priest) on May 01, 2001 at 18:51 UTC
    Hmmm. I can't repeat it with:
    perl -we 'print -A, " age of $_: ", -A > 30 ?"old\n":"new\n" for (</t +mp/*.*>);' #output for /tmp (linux): 6.93273148148148 age of /tmp/20010424.txt: new 6.11077546296296 age of /tmp/20010425.txt: new 3.95417824074074 age of /tmp/20010427.txt: new 8.10185185185185e-05 age of /tmp/20010501.txt: new 8.00399305555556 age of /tmp/smb-print.log: new 0.0544560185185185 age of /tmp/ new 0.0526157407407407 age of /tmp/ new 0.0436921296296296 age of /tmp/ new
    You're sure it doesn't have something to do with the value of $file? Otherwise, I'd think of some activestate bug...

    "We are not alone"(FZ)
    Update: c-era maybe right as well. But on linux, you must be pretty fast for the -A to be really zero..

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://76971]
Approved by root
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others wandering the Monastery: (5)
As of 2020-07-07 09:42 GMT
Find Nodes?
    Voting Booth?

    No recent polls found