Powershell命令复制多个文件的最新版本

时间:2016-12-31 16:02:43

标签: powershell

我正在尝试恢复我的OneNote笔记本,但它们恢复过程并不理想。它存储注释的副本并向它们添加日期,如果每天存在多个注释,则存储一个数字。我想获取最新文件(注释)并将其移动到一个文件夹,然后我可以恢复它们而不是使用它们的进程。 enter image description here

我尝试使用gci | select last -1只获取整个目录中的最新文件。但我需要每个笔记最新版本。理想情况下,我会制作一个副本,将其转储到与其所在的当前目录名称相匹配的另一个目录中,并在.one之后删除所有内容,但我会很满意目录中每个备注最新版本的副本。

2 个答案:

答案 0 :(得分:0)

此脚本可以为您提供帮助。

$dirPath = "<The directory where the files are located>"

Get-ChildItem $dirPath |
    Group-Object { ($_.Name -split '\.one' | Select-Object -First 1) } |
    Select-Object @{ Name = 'FileName'; Expression = { $_.Name } },
                  @{ Name = 'LastFileDate'; Expression = { ($_.Group -split ',') |
        ForEach-Object { [DateTime](($_ -split ' ')[2] -split '\)')[0] } |
            Sort-Object $_.LastFileDate |
            Select-Object -Last 1
    } } |
    ForEach-Object { "{0}.one (On {1:dd-MM-yyyy}).one" -f $_.FileName, $_.LastFileDate }

答案 1 :(得分:0)

Using Mathias' suggestion, and expanding on it to get what you wanted (the most recently modified file in each group), here is a solution that should work for you:

$grps = Get-ChildItem | Group-Object {$_.Name -split '\.one' | Select -First 1}
foreach ($group in $grps) {
    $group.Group | Sort LastWriteTime | Select -Last 1 | Select Name, FullName, LastWriteTime
}

You can just grab the FullName property of each file there and use that to Copy-Item it somewhere else.