0

我刚刚开始使用 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
}
}
4

1 回答 1

1

您需要添加逻辑来维护文件夹结构。

$FileName = $_.FullName.Replace($MovePath, $ArchPath);
于 2012-08-23T16:46:57.387 回答