"wendy" <[EMAIL PROTECTED]> writes:

> how  to change a file attribute in perl script (NT env)? Because the chmod 
> didn't work. (I need to change a read-only file to write-able file.)
> how to use the Win32::Admin ?
> 
> can any 1 help me?
> Thank for all.

ActivePerl contains a module named `Win32::File' for this purpose.
The program below illustrates its use. First it creates a sample
file and sets its attributes to READONLY and ARCHIVE. If the program
is run a second time, the READONLY attribute is removed.

The attributes of the sample file and some standard WINNT files
are displayed by the sub `attributes'.

--David

                            # # #

#!/usr/bin/perl -w

use strict;
use Win32::File qw(:DEFAULT GetAttributes SetAttributes);
use subs qw(attributes);

use constant SAMPLE_FILE => 'C:/TEMP/sample.txt';

if (-e SAMPLE_FILE) {
    my $attr = 0;
    GetAttributes(SAMPLE_FILE, $attr);
    SetAttributes(SAMPLE_FILE, $attr ^ READONLY);
} else {
    open(FP, ">".SAMPLE_FILE) or die "Can't create sample file: $!\n";
    print FP "This is a sample file.\n";
    close FP;
    SetAttributes(SAMPLE_FILE, READONLY|ARCHIVE);
}

my @file =
  (
   'C:/NTLDR',
   'C:/TEMP',
   'C:/BOOT.INI',
   'C:/WINNT/SYSTEM32/NOTEPAD.EXE',
   'C:/WINNT/SYSTEM32/WINSOCK.DLL',
   SAMPLE_FILE,
  );

my $attr = 0;
foreach my $file (@file) {
    next if (not -e $file);
    GetAttributes($file, $attr);
    print "<<< $file >>>\n", attributes($attr);
}

sub attributes {
    my ($attr) = @_;

    my %attrlist = 
      (
       'ARCHIVE'    => ARCHIVE,
       'COMPRESSED' => COMPRESSED,
       'DIRECTORY'  => DIRECTORY,
       'HIDDEN'     => HIDDEN,
       'NORMAL'     => NORMAL,
       'OFFLINE'    => OFFLINE,
       'READONLY'   => READONLY,
       'SYSTEM'     => SYSTEM,
       'TEMPORARY'  => TEMPORARY,
      );

    my $buf = "";
    foreach my $key (sort keys %attrlist) {
        $buf .= sprintf("%-10s:%d\n", $key,
                        ($attr & $attrlist{$key}) ? 1 : 0);
    }
    $buf;
}

__END__


---
You are currently subscribed to perl-win32-users as: [archive@jab.org]
To unsubscribe, forward this message to
         [EMAIL PROTECTED]
For non-automated Mailing List support, send email to  
         [EMAIL PROTECTED]

Reply via email to