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

2015-02-03 0 755
移动指定目录下的文件到目标目录并删除三天以前的文件。
#源文件路径
$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"
    }
}

遇见资源网 shell 移动指定目录下的文件到目标目录并删除三天以前的文件。 http://www.ox520.com/16819.html

常见问题

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务