2017-07-15 4 views
0

私はファイルの拡張子.bakを持つSQLの完全バックアップと増分バックアップを含むフォルダ構造を持っています。すべてのインクリメンタルを削除したいだけです。すべての私のフルバックアップは日曜日に作成されるので、基本的に日曜日以​​外に作成された* .bakファイルをすべて削除します。特定の曜日に作成されたファイル以外のすべてのファイルを削除するにはどうすればよいですか?

これはどのように達成できますか?

答えて

0

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 = @() 

これはすべて1行で行うことができます。

Get-ChildItem "G:\SQL Backups" -Recurse | where {!$_.PSIsContainer -and $_.Extension -match ".bak" -and !$_.CreationTime.DayOfWeek.value__ -eq 0} | Remove-Item -WhatIf 

あなたが最終的な結果に満足していると、あなたが実際にファイルを削除したい場合は-WhatIfパラメータを削除します。

関連する問題