简体   繁体   English

如何使用Powershell 2.0复制早于特定日期的文件夹

[英]How to copy folders older than a specific date with Powershell 2.0

I have a number of folders that I am trying to copy. 我有许多要复制的文件夹。 The folders are all name 'yyyymmdd' ie 20190615, going back up to a year or more. 这些文件夹全都名为“ yyyymmdd”,即20190615,可以追溯到一年或更长时间。 I am trying to figure out a way to copy only the last 45 days of these folders. 我正在尝试找出一种仅复制这些文件夹的最后45天的方法。 The biggest issue that I've run into is that the computers that I am running this on only have Powershell 2.0, which seems to have some limitations that 5 or greater does not have. 我遇到的最大问题是,我在其上运行的计算机仅具有Powershell 2.0,它似乎具有一些5或更高版本没有的限制。

I have been able to get the list of all of the folders in the path with : 我已经能够使用以下命令获取路径中所有文件夹的列表:

$datedsubs = Get-ChildItem -path $path | where-object { $_ -like "20*" }

From there though, I am a little stuck. 从那里,我有点卡住。 I feel this would be easier with PS 5 or greater. 我觉得使用PS 5或更高版本会更容易。 I've tried Robocopy, even though that is not a PS solution, but that copies everything, and I just want the folders. 我尝试过Robocopy,尽管那不是PS解决方案,但是可以复制所有内容,而我只想要文件夹。

I've tried something like the following, but it doesn't seem to work in PS 2.0. 我已经尝试了以下类似的方法,但是在PS 2.0中似乎不起作用。

Get-ChildItem -Path $path | Where-Object { ($_ -like '20*') -and ($_.LastAccessTime -lt $datedlimit)} | Copy-Item -Destination $destination -Recurse

Any help would be appreciated here. 任何帮助将不胜感激。

Thanks 谢谢

Putting what Lee has said into some code. 将Lee所说的内容放入一些代码中。

Get-ChildItem -Path $Path -Filter "20*" | Where-Object {($_.PSIsContainer) `
    -and ($_.LastAccessTime -gt ((Get-Date).AddDays(-45)))} | `
    Copy-Item -Destination $destination -Recurse

This should get you what you are after. 这应该为您提供所需的服务。

I have put it to -gt 45 days ago because this will only show folders accessed within the last 45 days. 我将它放在-gt 45天之前,因为它只会显示过去45天内访问的文件夹。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM