用户登录
用户注册

分享至

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

  • 作者: 楓27110021
  • 来源: 51数据库
  • 2022-09-22
#源文件路径
$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"
    }
}
软件
前端设计
程序设计
Java相关