Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

File Properties/attributes in Win32

by blackadder (Hermit)
on Mar 14, 2006 at 11:27 UTC ( #536521=perlquestion: print w/ replies, xml ) Need Help??
blackadder has asked for the wisdom of the Perl Monks concerning the following question:

Dear All,

I ma trying to obtain the file/folder properties under win32 (the attributes such as hidden, readonly...etc). I have the following simple code
use Win32::File; my $path = $ARGV[0]; if (Win32::File::GetAttributes($path, $attrib)) { print $attrib, $/; } else { print "Error\n"; }
However, the output i get is just a number!

Can some please enlighten me on how can I get the attribes in human readble form?

Blackadder

Comment on File Properties/attributes in Win32
Download Code
Re: File Properties/attributes in Win32
by planetscape (Canon) on Mar 14, 2006 at 11:44 UTC
Re: File Properties/attributes in Win32
by rafl (Friar) on Mar 14, 2006 at 11:45 UTC

    As the documentation of Win32::File says GetAttributes returns the or-ed combination of the file attributes

    To chech if a given attribute is set or not you can use something like that:

    my $attrib; Win32::Fine::GetAttributes($path, $attrib); if ($attrib & HIDDEN) { # HIDDEN and other attributes are # exported by Win32::File by default print "$path is hidden\n"; }

    Cheers, Flo

      I see,...

      This answers my question....

      ....Thanks.
      Blackadder
      # HIDDEN and other attributes are exported by Win32::File by default

      Did you try that? If so, perhaps you could explain what I'm doing wrong here?

      #! perl -slw use strict; use Win32::File qw[ GetAttributes ]; while( my $file = glob $ARGV[0] ) { GetAttributes( $file, my $attrs ); printf "%1s%1s%1s%1s%1s%1s%1s%1s%1s %s\n", ( $attrs & ARCHIVE ) ? 'A' : ' ', ( $attrs & COMPRESSED) ? 'C' : ' ', ( $attrs & DIRECTORY ) ? 'D' : ' ', ( $attrs & HIDDEN ) ? 'H' : ' ', ( $attrs & NORMAL ) ? 'N' : ' ', ( $attrs & OFFLINE ) ? 'O' : ' ', ( $attrs & READONLY ) ? 'R' : ' ', ( $attrs & SYSTEM ) ? 'S' : ' ', ( $attrs & TEMPORARY ) ? 'T' : ' ', $file; } __END__ C:\test>junk4 *.pl Bareword "ARCHIVE" not allowed while "strict subs" in use at C:\test\j +unk4.pl line 7. Bareword "COMPRESSED" not allowed while "strict subs" in use at C:\tes +t\junk4.pl line 7. Bareword "DIRECTORY" not allowed while "strict subs" in use at C:\test +\junk4.pl line 7. Bareword "HIDDEN" not allowed while "strict subs" in use at C:\test\ju +nk4.pl line 7. Bareword "NORMAL" not allowed while "strict subs" in use at C:\test\ju +nk4.pl line 7. Bareword "OFFLINE" not allowed while "strict subs" in use at C:\test\j +unk4.pl line 7. Bareword "READONLY" not allowed while "strict subs" in use at C:\test\ +junk4.pl line 7. Bareword "SYSTEM" not allowed while "strict subs" in use at C:\test\ju +nk4.pl line 7. Bareword "TEMPORARY" not allowed while "strict subs" in use at C:\test +\junk4.pl line 7. Execution of C:\test\junk4.pl aborted due to compilation errors.

      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.

        I didn't actually try it as I don't own a Win32 box, but the source code tells me that.

        Exporter doesn't seem to import the symbols in @EXPORT if you specify a list of symbols yourself. You can either not import the GetAttributes function, import all attributes yourself or use the attributes with their full name.

        I also think you could bug the author of the module to create an export tag that allows you to import all attributes at once.

        Flo

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (8)
As of 2015-07-06 22:52 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (84 votes), past polls