Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Win32::File::SetAttribute does not work

Reply
Thread Tools

Win32::File::SetAttribute does not work

 
 
Shea Martin
Guest
Posts: n/a
 
      10-12-2005
Using build 809, on winxp pro.

Win32::File::GetAttributes("somefile.txt", \$attrib);
Win32::File::SetAttributes("somefile.txt", $attrib|READONLY)
|| die "failed to set attribute";

But it does not work. If I manually set the file to readonly, then run
this code on it, it actually removes the readonly attribute, when I
would expect it to do nothing.

Is this a known bug, or am I doing something wrong.
chmod(0444, "somefile.txt) does work.

~S
 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      10-12-2005
Shea Martin wrote:
> Using build 809, on winxp pro.
>
> Win32::File::GetAttributes("somefile.txt", \$attrib);

^^

The second argument to GetAttributes shouldn't be a reference. The
function directly modifies its argument.

> Win32::File::SetAttributes("somefile.txt", $attrib|READONLY)
> || die "failed to set attribute";
>
> But it does not work. If I manually set the file to readonly, then run
> this code on it, it actually removes the readonly attribute, when I
> would expect it to do nothing.
>
> Is this a known bug, or am I doing something wrong.
> chmod(0444, "somefile.txt) does work.


The following script works correctly for me, on both:
This is perl, v5.8.4 built for MSWin32-x86-multi-thread
Binary build 810 provided by ActiveState Corp.
http://www.ActiveState.com
and
This is perl, v5.8.6 built for cygwin

#!/usr/bin/perl
use strict;
use warnings;
use Win32::File;

my $file = 'somefile.txt';
open my $fh, '>', $file or die "Couldn't create $file: $!\n";
close $fh;
my $attrib;

Win32::File::GetAttributes($file, $attrib)
or die "Couldn't get attribs of somefile.txt: $!";
printf "Attributes: 0x%04x\n", $attrib;

Win32::File::SetAttributes($file, $attrib|READONLY)
|| die "failed to set attribute";

Win32::File::GetAttributes($file, $attrib)
or die "Couldn't get attribs of somefile.txt: $!";

printf "Attributes: 0x%04x\n", $attrib;

__END__

Output:
Attributes: 0x0020
Attributes: 0x0021

Paul Lalli

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      10-12-2005
Shea Martin <null@void.0> wrote in
news:uP93f.107813$(E-Mail Removed):

> Using build 809, on winxp pro.
>
> Win32::File::GetAttributes("somefile.txt", \$attrib);
> Win32::File::SetAttributes("somefile.txt", $attrib|READONLY)
> || die "failed to set attribute";
>
> But it does not work. If I manually set the file to readonly, then
> run this code on it, it actually removes the readonly attribute, when
> I would expect it to do nothing.


Well, you should always use

use strict;

in your scripts, and post a short but complete program that still
exhibits the problem you are experiencing.

Looking at the documentation for Win32::File, I see

GetAttributes(filename, returnedAttributes)

admittedly, this is not great, but I see no indication that you need to
pass a reference to GetAttributes.

D:\Home\asu1\UseNet\clpmisc> cat tt.pl
#!/usr/bin/perl

use strict;
use warnings;

use Win32::File;

my $attr;

Win32::File::GetAttributes($ARGV[0], $attr)
or die "Cannot get attributes for $ARGV[0]: $!\n";

print "Attributes for $ARGV[0]: $attr\n";

Win32::File::SetAttributes($ARGV[0], $attr | READONLY)
or die "Cannot set READONLY attribute for $ARGV[0]: $!\n";


__END__


D:\Home\asu1\UseNet\clpmisc> attrib test.txt
A D:\Home\asu1\UseNet\clpmisc\test.txt

D:\Home\asu1\UseNet\clpmisc> tt test.txt
Attributes for test.txt: 32

D:\Home\asu1\UseNet\clpmisc> attrib test.txt
A R D:\Home\asu1\UseNet\clpmisc\test.txt

D:\Home\asu1\UseNet\clpmisc> tt test.txt
Attributes for test.txt: 33

D:\Home\asu1\UseNet\clpmisc> attrib test.txt
A R D:\Home\asu1\UseNet\clpmisc\test.txt

Sinan
--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
Shea Martin
Guest
Posts: n/a
 
      10-13-2005
Paul Lalli wrote:
> Shea Martin wrote:
>
>>Using build 809, on winxp pro.
>>
>>Win32::File::GetAttributes("somefile.txt", \$attrib);

>
> ^^
>
> The second argument to GetAttributes shouldn't be a reference. The
> function directly modifies its argument.


This fixed it. Thanks.

~S
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
MS work around on text wrapping in a datagrid does not work TB ASP .Net 2 02-22-2006 10:34 PM
Why does post or pre incremenent or decrement does not work inside a sizeof operator? Tarun C Programming 5 07-14-2005 03:58 PM
Why does this (very simple piece of) code does not work? jblazi Python 5 08-16-2004 01:30 PM
Webservice works once and then DOES NOT seem to work even though program does not crash Phi! ASP .Net Web Services 1 04-23-2004 08:42 AM
int('2.1') does not work while int(float('2.1')) does Vineet Jain Python 9 04-16-2004 10:12 AM



Advertisments