比较不同文件夹中的文件时如何使用LastWriteTime

问题描述 投票:0回答:1

该脚本通过NameLengthLastWriteTime比较FILE对象。

cls
$Source= "C:\Source"
$Destination = "C:\Destination"                  

Compare-Object (ls $Source) (ls $Destination) -Property Name, Length, LastWriteTime | Sort-Object {$_.LastWriteTime} -Descending 

输出:

Name                 Length LastWriteTime          SideIndicator
----                 ------ -------------          -------------
11.0.3127.0.txt           6 8/31/2013 10:01:19 PM  <=
11031270.txt              0 8/31/2013 9:43:41 PM   <=
11.0.3128.0.txt          13 8/31/20131:20:15PM     =>
11.0.3129.0.txt           0 8/28/2013 11:34:38 AM  <=

我需要创建一个脚本来检索当前的数据库版本并检查单个或多个补丁是否可用。

它的工作方式如下:

  1. 对数据库运行SQL查询
  2. 将SQL Info存储到C:\ Destination上的fileName(例如,11.0.3128.0.txt)
  3. 将.txt文件中的信息与Source文件夹中存在的文件/补丁进行比较
  4. 项目清单
  5. 如果Source文件夹包含较旧的文件/补丁 - 什么都不做
  6. 如果Source文件夹包含较新的文件,则将这些文件复制到C:\ NewPatchFolder
  7. 然后运行脚本以应用所有新修补程序

我已经照顾了#1,#2。我打算修改/添加上面的脚本来简化#3,#4和#5中的步骤。

是否可以修改上述脚本以实现我的目标如下:

  • 将C:\ Source文件夹中的文件的LastWriteTime与C:\ Destination中的文件进行比较
  • 如果LastWriteTime等于或大于C:\ Destination文件夹中的文件的LastWriteTime,则将C:\ Source文件夹中的文件复制到C:\ NewPatchFolder
powershell powershell-v2.0 powershell-v3.0
1个回答
0
投票

我不会为此使用Compare-Object。请尝试以下方法:

Get-ChildItem $Source | % {
  $f = Join-Path $Destination $_.Name
  if (Test-Path -LiteralPath $f) {
    if ($_.LastWriteTime -ge (Get-Item $f).LastWriteTime) {
      Move-Item $_.FullName 'C:\NewPatchFolder'
    }
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.