私は PowerShell を使い始めたばかりで、本を読んだり Google で検索したりして、このスクリプトを思いつきました。私が抱えている問題は、フォルダー構造を保持していないことです。5 日以上経過したファイルの 1 つのネットワーク ロケーションをチェックしています。5 日以上経過している場合は、別のネットワークの場所に移動します。2 番目の場所では、30 日以上経過したファイルをチェックし、それらのファイルを削除します。
$movepath = "C:\test"
$archpath = "C:\test2"
$deletepath = "C:\files"
$movedays = "5"
$deletedays = "30"
$datetime = get-date
$deletelog = "c:\logs\deletelog.txt"
$movelog = "c:\logs\movelog.txt"
$errorlog = "c:\logs\errorlog.txt"
write-progress -activity "Archiving Data" -status "Progress:"
Get-Childitem -Path $movepath -Recurse | Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-$movedays)} |
ForEach {
$filename = $_.fullname
try
{
Move-Item $_.FullName -destination $archpath -force -ErrorAction:SilentlyContinue
"Moved $filename to $archpath at $datetime successfully" | add-content $movelog
}
catch
{
"Error moving $filename: $_ " | add-content $errorlog
}
}
Get-Childitem -Path $deletepath | Where-Object {$_.LastWriteTime -lt (get-date).AddDays(-$deletedays)} |
ForEach {
$filename = $_.fullname
try
{
Remove-Item $_.FullName -force -ErrorAction:SilentlyContinue
"Removed $filename at $datetime successfully" | add-content $deletelog
}
catch
{
"Error moving $filename: $_ " | add-content $errorlog
}
}