I have put together a script to delete backed up files, but believe I am have the archive bit attribute confused.
$path = "D:\logs\"
$files = Get-ChildItem -Path $path -Recurse
$attribute = [io.fileattributes]::archive #archive bit
$date = get-date -format d # strip time out of date, date needed for Filename
$date = $date.replace('/','.') # strip out forward slashes and replac with . in date
$filename = "\ArchiveLog"+$date+".txt" # Filename for log file.
$location = $path+$filename
cd D:\logs
Foreach($file in $files)
{
If((Get-ItemProperty -Path $file.fullname).attributes -band $attribute)
{
add-content -path "$path" "$file.DirectoryName has been deleted"
Remove-Item $file -force
}
}
however when I test this, this script deletes brand new files that have just been created, as the files that are newly created have the "A" attribute or archive bit.
When I have looked at other sample scripts they usually have the line, that ensures the files archive bit is set:
attribute = [io.fileattributes]::archive #archive bit
If((Get-ItemProperty -Path $file.fullname).attributes -band $attribute)
{
log file name
Remove file
}
So ... I was wondering if this is something new to windows 2008 r2 where the archive bit "A" is set automatically upon creation, or if perhaps the other scripts I have found are wrong.