Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Hidden file attribute in Win32

by bitman (Beadle)
on May 21, 2002 at 16:46 UTC ( #168187=perlquestion: print w/ replies, xml ) Need Help??
bitman has asked for the wisdom of the Perl Monks concerning the following question:

Is there any 'nice' way to check if a file is hidden in windows without THIS :-

my $hideflag=`attrib $src`; if ( $hideflag=~m/^ H/ ) {

stat & file test operators seem to not have anything to offer.

Title edit by tye (was "Hide well Frodo")

Replies are listed 'Best First'.
Re: Hidden file attribute in Win32 (was: Hide well Frodo)
by VSarkiss (Monsignor) on May 21, 2002 at 17:14 UTC

    You can use the Win32::File module, which is a little simpler than Win32API::File, like so:

    use Win32::File qw(GetAttributes HIDDEN); my $attr; if (GetAttributes($src, $attr)) { if ($attr & HIDDEN) { # whatever.... } } else { warn "Error getting attributes of $src"; }
    I don't do a lot of Win32 programming but just happened to know this one because I had to do something similar a while ago....

    Update
    Added code to check return value of GetAttributes. Shame on me for forgetting. Forty Hail Larrys.

Re: Hidden file attribute in Win32 (was Hide well Frodo)
by particle (Vicar) on May 21, 2002 at 17:03 UTC
    use Win32API::File;
    i think GetHandleInformation is the method, and FILE_ATTRIBUTE_HIDDEN is the flag you'll want. sorry i don't have a complete example for you.

    ~Particle *accelerates*

Re: Hidden file attribute in Win32
by marinersk (Curate) on Jul 09, 2016 at 04:45 UTC

    Just to expand a bit on the answers given:

    #!/usr/bin/perl use strict; use Win32::File; my $hidfnm = 'hideme.txt'; my $getatr = 0; my $getret = &Win32::File::GetAttributes($hidfnm, $getatr); if ($getret) { if ($getatr & HIDDEN) { print "File is hidden. Unhiding\n"; } else { print "File is not hidden. Hiding\n"; } # Toggle the HIDDEN flag my $setatr = ($getatr ^ HIDDEN); my $setret = &Win32::File::SetAttributes($hidfnm, $setatr); } __END__

    Results:

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2016-07-25 21:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What is your favorite alternate name for a (specific) keyboard key?


















    Results (229 votes). Check out past polls.