powershell删除备份文件/存档位/windows 2008 R2
我已经编写了一个脚本来删除备份文件,但相信我对存档位属性感到困惑。
$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
}
}
但是,当我对此进行测试时,该脚本会删除刚刚创建的全新文件,因为新创建的文件具有“A”属性或存档位。
当我查看其他示例脚本时,它们通常具有确保设置文件存档位的行:
attribute = [io.fileattributes]::archive #archive bit
If((Get-ItemProperty -Path $file.fullname).attributes -band $attribute)
{
log file name
Remove file
}
所以...我想知道这是否是 Windows 2008 r2 的新功能,其中存档位“A”自动设置创建,或者我发现的其他脚本是否错误。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为你让事情变得比需要的更加复杂。以下是我仅返回设置了 ARCHIVE 属性的 TXT 文件的示例:
PS C:>目录 c:\work*.txt |其中 {$_.attributes -match "Archive"} |选择姓名
,属性
如果您只想更新或早于特定日期的文件,则可以根据需要添加其他过滤。
I think you are making this more complicated than it needs to be. Here's an example where I return only TXT files with the ARCHIVE attribute set:
PS C:> dir c:\work*.txt | where {$_.attributes -match "Archive"} | select name
,attributes
You can add additional filtering as needed if you only want files newer or older than a certain date.