2017-08-11 66 views
-2

试图获取能够将45天的文件复制到新网络位置的脚本,验证它们是否已复制,然后从原始位置删除这些文件。任何帮助是极大的赞赏将文件复制45天,验证然后删除

+3

酷,你怎么有这么远吗? – evilSnobu

+0

我对PowerShell非常陌生,我通过复合视图学习了一门课程,但超越了它对我的新课程。我负责完成这项任务,经过一些研究并找到了几个与我正在寻找的选项相近的选项,我发现这个网站并认为这将是一个很好的学习场所,可能有助于继续前进,因为我将使用powershell越来越多。谢谢你的帮助! – John

+0

**约翰:** *“我需要一个脚本”* ....显示没有工作。不要像约翰。 – abelenky

回答

0
  1. 这将让一个文件夹中的所有子项(文件夹和文件)获取-ChildItem递归
  2. 选择项目年长然后45天$ _LastWriteTime -lt(获取最新).AddDays( -45)
  3. 将它们移动到新的位置移动项目-destination
  4. 卸下旧的项目中移除,项目

    得到-childitem -Path “C:\ HERE” -Recurse | where-object {$ _。LastWriteTime -lt(get-date).AddDays(-45)} | move-item -destination“\ D:\ THERE”|删除-项目

UPDATE 2017年8月12日 添加验证文件之前移动删除。如果(测试的路径$目的地$($ _。名称))

$Source = "C:\Source\" 
$Destination = "D:\Destination\" 
get-childitem -Path "C:\Source\" -Recurse | where-object {$_.LastWriteTime -lt (get-date).AddDays(-45)} | move-item -destination "D:\Destination\" -force | ForEach-Object{ 
    if(Test-Path $Destination$($_.Name)){ 
     Remove-Item $_ -Force 
    } 
} 

UPDATE 2017年8月14日 所有在一行

get-childitem -Path "C:\Source\" -Recurse | where-object {$_.LastWriteTime -lt (get-date).AddDays(-45)} | move-item -destination "D:\Destination\" -force | ForEach-Object{ if(Test-Path $Destination$($_.Name)){Remove-Item $_ -Force}} 
+0

谢谢,我会试一试,让你知道它是怎么回事! – John

+0

请确保标记答案为完整的,如果它适用于您在我运行此PowerShell时 – ArcSet

+0

: get-childitem -Path“D:\ RnDDepts \ Dropbox \ zTest \ SQLSrv2k5.msi”-Recurse | where-object {$ _。LastWriteTime -lt(get-date).AddDays(-45)} | move-item -destination“D:\ RnDDepts \ Dropbox \ zTest \ test2”|删除项目 我正在选择一个特定的文件,它可以完美地将其复制到test2文件夹并从zTest文件夹中删除它。但是,如果我只是这样写: get-childitem -Path“D:\ RnDDepts \ Dropbox \ zTest \”-Recurse | where-object {$ _。LastWriteTime -lt(get-date).AddDays(-45)} | move-item -destination“D:\ RnDDepts \ Dropbox \ zTest \ test2”|删除项 – John