我刚刚开始使用PowerShell,并且通过一本书和大量Google搜索获得了一些帮助,我已经提出了这个脚本。我遇到的问题是它没有保留文件夹结构。它正在检查一个网络位置以查找超过5天的文件。如果超过5天,则会将其移至其他网络位置。在第二个位置,它会检查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
}
}
答案 0 :(得分:1)
您需要添加逻辑来维护文件夹结构。
$FileName = $_.FullName.Replace($MovePath, $ArchPath);