Win32::File::SetAttribute does not work

Discussion in 'Perl Misc' started by Shea Martin, Oct 12, 2005.

  1. Shea Martin

    Shea Martin Guest

    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
    Shea Martin, Oct 12, 2005
    #1
    1. Advertising

  2. Shea Martin

    Paul Lalli Guest

    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
    Paul Lalli, Oct 12, 2005
    #2
    1. Advertising

  3. Shea Martin <null@void.0> wrote in
    news:uP93f.107813$:

    > 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 <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
    A. Sinan Unur, Oct 12, 2005
    #3
  4. Shea Martin

    Shea Martin Guest

    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
    Shea Martin, Oct 13, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matt
    Replies:
    7
    Views:
    14,712
    John C. Bollinger
    Nov 8, 2004
  2. setAttribute() does not work

    , Aug 22, 2003, in forum: Javascript
    Replies:
    3
    Views:
    150
  3. ItsMillerTime4u
    Replies:
    1
    Views:
    479
    Randy Webb
    Mar 4, 2005
  4. fj
    Replies:
    2
    Views:
    126
  5. RC
    Replies:
    0
    Views:
    444
Loading...

Share This Page