Reputation: 404
I have a Powershell script that copies files from one location to another. Once the copy is complete I want to clear the Archive attribute on the files in the source location that have been copied.
How do I clear the Archive attribute of a file using Powershell?
Upvotes: 30
Views: 57631
Reputation: 3626
$attr = [System.IO.FileAttributes]$attrString
$prop = Get-ItemProperty -Path $pathString
# SetAttr
$prop.Attributes = $prop.Attributes -bor $attr
# ToggleAttr
$prop.Attributes = $prop.Attributes -bxor $attr
# HasAttr
$hasAttr = ($prop.Attributes -band $attr) -eq $attr
# ClearAttr
if ($hasAttr) {$prop.Attributes = $prop.Attributes -bxor $attr }
Upvotes: 4
Reputation: 1082
# Show attributes
$file.Attributes
# Set (overwriting existing) attributes:
$file.Attributes = 'Archive, Readonly'
# Add (retaining existing) attributes:
$file.Attributes += 'Archive, Readonly'
# Remove specific attributes:
$file.Attributes -= 'Archive, Readonly'
# Check for specific attributes:
$file.Attributes.hasflag([IO.FileAttributes]'Archive, Readonly')
# Forget about bitwise operators ;-)
Upvotes: 2
Reputation: 9
I found Simon Steele's answer to be helpful, though I needed to modify more than just one file so I changed it to the following:
$BadAttributes = Get-ChildItem "C:\PATH" -Recurse -Force
foreach($File in $BadAttributes) {
if (Test-Path -Path $File -IsValid)
{
$File.attributes = 'Normal'
}
}
# Check file attributes
Get-ItemProperty -Path "C:\PATH" | Format-list -Property Attributes -Force
Upvotes: -1
Reputation: 246
Mitch's answer works well for most attributes, but will not work for "Compressed." If you want to set the compressed attribute on a folder using PowerShell you have to use the command-line tool compact
compact /C /S c:\MyDirectory
Upvotes: 2
Reputation: 31
You may use the following command to toggle the behaviour
$file = (gci e:\temp\test.txt)
$file.attributes
Archive
$file.attributes = $file.Attributes -bxor ([System.IO.FileAttributes]::Archive)
$file.attributes
Normal
$file.attributes = $file.Attributes -bxor ([System.IO.FileAttributes]::Archive)
$file.attributes
Archive
Upvotes: 3
Reputation: 24370
As the Attributes is basically a bitmask field, you need to be sure clear the archive field while leaving the rest alone:
PS C:\> $f = get-item C:\Archives.pst PS C:\> $f.Attributes Archive, NotContentIndexed PS C:\> $f.Attributes = $f.Attributes -band (-bnot [System.IO.FileAttributes]::Archive) PS C:\> $f.Attributes NotContentIndexed PS H:\>
Upvotes: 18
Reputation: 300719
From here:
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;}
}
Upvotes: 13
Reputation: 11608
You can use the good old dos attrib command like this:
attrib -a *.*
Or to do it using Powershell you can do something like this:
$a = get-item myfile.txt
$a.attributes = 'Normal'
Upvotes: 26