【问题标题】:powershell delete backed up file / archive bit / windows 2008 R2powershell 删除备份文件/存档位/windows 2008 R2
【发布时间】:2012-02-28 03:25:28
【问题描述】:

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

$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”在创建时自动设置,或者我发现的其他脚本是否错误。

【问题讨论】:

    标签: windows powershell attributes archive


    【解决方案1】:

    我认为你让这变得比它需要的更复杂。这是一个示例,其中我只返回带有 ARCHIVE 属性集的 TXT 文件:

    PS C:> 目录 c:\work*.txt |其中 {$_.attributes -match "存档"} |选择名称 ,属性

    如果您只想要比某个日期新或旧的文件,您可以根据需要添加额外的过滤。

    【讨论】:

    • Jeffery,是的,谢谢,这更干净。而且...如果设置了“存档”属性,这是否意味着文件已备份,或者需要备份。还在寻找这个,我以为我能很快找到这个信息,但是我在微软的网站上很难找到这个。
    • @user1178826 存档属性的状态指示文件是否已备份。当文件被创建或修改时,归档位被设置,当文件被备份时,归档位被清除。
    • 感谢您清除此问题,我刚刚测试了一些文件并能够验证这一点(抱歉应该从一开始就这样做)再次感谢您的帮助。
    猜你喜欢
    • 1970-01-01
    • 2011-10-24
    • 1970-01-01
    • 1970-01-01
    • 2011-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-20
    相关资源
    最近更新 更多