【发布时间】:2016-10-17 21:31:07
【问题描述】:
我正在尝试将所有六个月或更早的文件移动到存档文件中 我有
#set root folder
$baseFolder = "C:\Users\Caleb\Desktop"
$archiveBase = "C:\Users\Caleb\Desktop"
$limit_to=(Get-date (Get-date -Format d)).AddDays(-180)
#get files inside root
$folder = Get-ChildItem $baseFolder -File
#for each folder
foreach($file in Where-$folder.CreationTime -lt $limit_to ) {
#build the move destination path
$a=$file.CreationTime.Date.Year
$archive="Archive"
$name=$a+" "+$archive
$destination = Join-Path $archiveBase $name
#Create Directory And Deposite Files
if(Test-Path $destination)
{
mv $file $destination
}
else
{
mkdir $destination
mv $file $destination
}
}
我似乎无法仅获取超过 180 天的文件。 我是初学者,所以我愿意做出任何效率上的改变
【问题讨论】:
-
您可以通过
((Get-ChildItem).CreationTime).Year访问文件的创建年份。只需将其适当地包含在copy-item的目标路径中即可。 -
好吧,对不起,我认为你现在可以解决我的问题@davidhigh
标签: powershell