in reply to How to remove COMPRESSED attribute with Win32::File?

And just in case you want a bit of explanation...

In order to clear a bit, you must first negate the bit mask, then bitwise AND it with the attribute flags. So the negation flips the bits, which makes it all ones, except for the bit we want to clear. This way when you AND it with the flags, the only bit affected is the one we want to clear.

That was wordy and probably confusing. I guess that's what I get for posting at 2:00am. How about an example?

#These values probably don't match the actual values COMPRESSED = 0001 0000 $attr = 0101 1100 After negation = 1110 1111 &0101 1100 ---------- 0100 1100

Replies are listed 'Best First'.
Re^2: How to remove COMPRESSED attribute with Win32::File?
by NovasTaylor (Initiate) on Mar 06, 2009 at 03:29 UTC
    Thank you for the replies. I tried the following without success:
    use Win32::File; my $path="C:\\temp\\Perl\\TestFolder\\TestFile.txt"; my $attr; Win32::File::GetAttributes($path, $attr); if ($attr & COMPRESSED) { print "File is compressed. Now uncompress it!\n"; $attr &= ~COMPRESSED; Win32::File::SetAttributes($path, $attr); } else { print "File is not compressed. Nothing to do here.\n"; }
    Any ideas on what I am doing wrong? I can set and unset the attribute in Windows Explorer but with the Perl code compression remains on. Cheers, Tim

      My first thought was:

      Maybe if you checked for errors, you'd know? The docs state how to detect if an error occured. And when one does, you can probably find out the error from $^E.

      But it seems SetFileAttributes, the system call for which Win32::File::SetAttributes is a wrapper, simply ignores the flag.

      The documentation directs programmers to "use the DeviceIoControl function with the FSCTL_SET_COMPRESSION operation."

      Any ideas on what I am doing wrong?

      I don't think you can change the 'COMPRESSED' status of a file using Win32::File. The MSDN documentation I'm looking at says that SetFileAttributes (which is what Win32::File is using) is unable to set the compression attribute - and I suspect that it is likewise unable to turn that attribute off. At least, I can't get the script that you posted to do the right thing, either - and it's not throwing any errors.

      Cheers,
      Rob