移动指定目录下的文件到目标目录并删除三天以前的文件。

清华大佬耗费三个月吐血整理的几百G的资源,免费分享!....>>>

#源文件路径
$workPath = "D:\d"
#目标文件路径
$destPath = "D:\a"

#遍历目录
Get-ChildItem $workPath | ForEach-Object -Process{
	if($_ -is [System.IO.FileInfo])
	{
		$destFilePath = $destPath+"\"+$_.name
		#Write-Host($destFilePath);
		#如果目标文件已存在则删除
		if(Test-Path $destFilePath)
		{
			 Remove-Item $destFilePath -Recurse -Force -ErrorAction "SilentlyContinue"
		}
		#移动文件到目标目录
		Move-Item $_.FullName $destPath  -ErrorAction "SilentlyContinue"
	}
}

#定义删除几天前的文件
$timeOutDay = 3

$allFile = Get-ChildItem -Path $destPath

foreach($file in $allFile)
{
    $daySpan = ((Get-Date) - $file.LastWriteTime).Days
    if ($daySpan -gt $timeOutDay)
    {
        Remove-Item $file.FullName -Recurse -Force -ErrorAction "SilentlyContinue"
    }
}