Windows 如何删除除在一周中特定日期创建的文件以外的所有文件?

Windows 如何删除除在一周中特定日期创建的文件以外的所有文件?,windows,batch-processing,Windows,Batch Processing,我有一个包含SQL完整备份和增量备份的文件夹结构,所有备份都具有文件扩展名.bak。我只想删除所有的增量。我所有的完整备份都是在星期天创建的,所以基本上我想删除除星期天以外任何一天创建的所有*.bak文件 如何实现这一点?PowerShell可用于根据特定条件筛选文件,包括按星期几进行筛选。下面的模块可以教您如何实现。注释是内联的 # Get all files which are NOT folders, where the extention matches your required ex

我有一个包含SQL完整备份和增量备份的文件夹结构,所有备份都具有文件扩展名.bak。我只想删除所有的增量。我所有的完整备份都是在星期天创建的,所以基本上我想删除除星期天以外任何一天创建的所有*.bak文件


如何实现这一点?

PowerShell可用于根据特定条件筛选文件,包括按星期几进行筛选。下面的模块可以教您如何实现。注释是内联的

# Get all files which are NOT folders, where the extention matches your required extention.  Use -Recurse to do this recursively - i.e. all subfolders
$allfiles = Get-ChildItem "G:\SQL Backups" -Recurse | where {!$_.PSIsContainer -and $_.Extension -match ".bak"}

# Filter your file list and select objects which do NOT have the Day Of Week value equal to 0 (Which is a Sunday)
$nonsundayfiles = $allfiles | Where-Object { !$_.CreationTime.DayOfWeek.value__ -eq 0 }

#Iterate through all the non sunday filees and delete each one.  Verbose logging to screen.
$nonsundayfiles | ForEach-Object { 
    Write-Host "The File " $_.FullName "was created on" $_.CreationTime.DayOfWeek "and therefore is being deleted"
    Remove-Item $_.FullName -WhatIf
    }

#Empty variables - useful if in PowerShell ISE
$allfiles = @()
$nonsundayfiles = @()
如果你有信心,你可以在一条线上完成这一切

Get-ChildItem "G:\SQL Backups" -Recurse | where {!$_.PSIsContainer -and $_.Extension -match ".bak" -and !$_.CreationTime.DayOfWeek.value__ -eq 0} | Remove-Item -WhatIf
如果您对最终结果感到满意,并且希望实际删除该文件,请删除-WhatIf参数