首页 > 解决方案 > 排除最大上次修改日期的文件

问题描述

文件需要从四个文件夹中移动...如何使用逻辑排除文件的最大上次修改日期

robocopy D:\ven\one1\  D:\ven\one\two1\ /MOVE /xd D:\ven\one\ven_program_kl

举个例子

file1 last modified date:6/19/18 20:00
file2 last modified date:6/8/18 20:00

在这种情况下,要排除 file1 并将 file2 移至D:\ven\one\two1 folder....需要简单的代码,谢谢

标签: windowsbatch-filecmd

解决方案


这在 PowerShell 中很容易。将以下代码保存在文件中,例如keeplast.ps1.

目前尚不清楚您为什么使用 /XD 参数。

按 LastWriteTime 降序排序,然后跳过第一个条目,将产生其他文件名。如果您对正确的移动操作感到满意,请-WhatIfMove-Itemcmdlet 中删除 。

$sourcedir = 'C:\src\t'
$destdir = $Env:TEMP

Get-ChildItem -File -Path $sourcedir |
    Sort-Object -Property LastWriteTime -Descending |
    Select-Object -Skip 1 |
    Move-Item -Destination $destdir -WhatIf

如果必须从 cmd shell 运行它,请使用:

powershell -NoProfile -File .\keeplast.ps1

如果需要,可以创建一个 .bat 文件脚本。

@ECHO OFF
powershell -NoProfile -File .\keeplast.ps1
EXIT /B

推荐阅读