Saturday, October 27, 2007

File Attributes helper functions

 

This file attributes functions returns True/False if the operation you're running succeeded or not. All functions are  Each function gets a file path and an attribute name as parameters.

Attribute names can be one of the [System.IO.FileAttributes] enum type names. I didn't check all of them but here's the full list:

ReadOnly
Hidden
System
Directory
Archive
Device
Normal
Temporary
SparseFile
ReparsePoint
Compressed
Offline
NotContentIndexed
Encrypted

 

function Get-FileAttribute{
    param($file,$attribute)
    $val = [System.IO.FileAttributes]$attribute;
    if((gci $file -force).Attributes -band $val -eq $val){$true;} else { $false; }
}

 

function Set-FileAttribute{
    param($file,$attribute)
    $file =(gci $file -force);
    $file.Attributes = $file.Attributes -bor ([System.IO.FileAttributes]$attribute).value__;
    if($?){$true;} else {$false;}
}

 

function Clear-FileAttribute{
    param($file,$attribute)
    $file =(gci $file -force);
    $file.Attributes -= ([System.IO.FileAttributes]$attribute).value__;
    if($?){$true;} else {$false;}
}

 

Testing the functions:

# create test file
$file = new-item -path . -name testFile.txt -type file;

write-host "`nIs file ReadOnly? " -nonewline
Get-FileAttribute $file.FullName ReadOnly
"attributes: " +(gci $file.FullName -force).attributes;

write-host "`nSet file as ReadOnly. Action succeeded? " -nonewline
Set-FileAttribute $file.fullname readonly
"attributes: " +(gci $file.FullName -force).attributes;

write-host "`nSet file as Hidden. Action succeeded? " -nonewline
Set-FileAttribute $file.FullName hidden
"attributes: " +(gci $file.FullName -force).attributes;

if(Get-FileAttribute $file.FullName hidden){
    write-host "`nFile is marked as Hidden"
    if(Clear-FileAttribute $file.FullName hidden){
        write-host "File is no longer Hidden"
    } else {
        write-host "error clearing hidden attribute"
    }
}

# delete test file
remove-item .\testFile.txt -force

 

The output:

Is file ReadOnly? False
attributes: Archive

Set file as ReadOnly. Action succeeded? True
attributes: ReadOnly, Archive

Set file as Hidden. Action succeeded? True
attributes: ReadOnly, Hidden, Archive

File is marked as Hidden
File is no longer Hidden
attributes: ReadOnly, Archive

 


A modified template/version of the functions with error handling:

function Do-FileAttribute{
    param(
        [string] $file=$(throw "Invalid path."),
        [string] $attribute=$(throw "Invalid attribute.")
    )
   
    # check if the file exists
    if(-not (test-path -path $file)){
        write-error "Invalid file path";
        return;
    }
   
    # check if the attribute name is valid
    $attrNames = [enum]::getNames([System.IO.FileAttributes]);
    if($attrNames -notcontains $attribute){
        write-error "Invalid attribute name. Possible values: $([string]::Join(', ',$attrNames))."
        return;
    }


    ###
    code for get/set/clear operation
    ###

}

1 comment:

Anonymous said...

Don't know if you ever tested or if Powershell 2 behaves different:

function Get-FileAttribute
{
param($file,$attribute)
$val = ([System.IO.FileAttributes]$attribute).value__;
if(((Get-Item -force $file).Attributes -band $val) -eq $val){$true;} else { $false; }
}


function Set-FileAttribute
{
param($file,$attribute)
$file =(Get-Item $file -force);
$file.Attributes = $file.Attributes -bor ([System.IO.FileAttributes]$attribute).value__;
if($?){$true;} else {$false;}
}


function Clear-FileAttribute
{
param($file,$attribute)
$allExcept = ([int]0xFFFFFFFF -bxor ([System.IO.FileAttributes]$attribute).value__);

$file =(Get-Item $file -force);
$file.Attributes = [System.IO.FileAttributes]($file.Attributes.value__ -band $allExept);
if($?){$true;} else {$false;}
}

...is how I got it going..
Regards,
Leu