2012-08-23 129 views
0

我剛剛開始使用PowerShell,並從書籍和大量Google搜索中獲得一些幫助,我已經提出了此腳本。我遇到的問題是它不保留文件夾結構。它正在檢查一個網絡位置是否有超過5天的文件。如果它大於5天,則會將其移至其他網絡位置。在第二個位置,它檢查超過30天的文件並刪除這些文件。Powershell存檔腳本

$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 
} 
} 

回答

1

您需要添加邏輯來維護文件夾結構。

$FileName = $_.FullName.Replace($MovePath, $ArchPath);