powershell删除备份文件/存档位/windows 2008 R2

发布于 2025-01-03 03:12:38 字数 1028 浏览 0 评论 0原文

我已经编写了一个脚本来删除备份文件,但相信我对存档位属性感到困惑。

$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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

記憶穿過時間隧道 2025-01-10 03:12:38

我认为你让事情变得比需要的更加复杂。以下是我仅返回设置了 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文