Enable the read-only attribute of a file (Perl)

This code can be found in Chapter 8 of Windows XP Cookbook

Purchase XP Cookbook or Networking Recipes for only $25 plus shipping! While supplies last.

Find out how to download all of the Perl code from this site.

# From the book "Windows XP Cookbook"
# ISBN: 0596007256

# ------ SCRIPT CONFIGURATION ------

use Win32::OLE;

$strFile = '<FilePath>';
# e.g. d:\mysecretscript.vbs
$boolReadOnly = 1;
# True = read-only, False = not read-only
# ------ END CONFIGURATION ---------
$objFSO = Win32::OLE->new('Scripting.FileSystemObject');

# Change this to GetFolder to hide/unhide a folder
$objFile = $objFSO->GetFile($strFile);

if ($boolReadOnly == 1) {
    if ($objFile->Attributes && 1) {
        print "File already read-only\n";
    }
    else {
        $objFile->{Attributes} = $objFile->Attributes + 1;
        print "File is now read-only\n";
    }
}
else {
    if ($objFile->Attributes && 1) {
        $objFile->{Attributes} = $objFile->Attributes - 1;
        print "File is not read-only\n";
    }
    else {
        print "File is already not read-only\n";
    }
}

This code has been viewed 1650 times.

New from the creators of TechTasks.com: StatSheet.com