我有以下代码将查看修改日期,我可以复制一个单独运行的文件。但是,我仍然无法让代码继续查看文件以及何时检测到要复制的更改。
Get-Childitem "directory goes here" -File |
Where {$_.LastWriteTime -lt $date}
Copy-Item -Path 'directory goes here' -Destination 'directory goes here'
我能得到一些关于我哪里出错的方向吗?
您的示例本身几乎是完整的。您可以进一步完善您的管道:
$UNC = '\\share'
$Path = 'C:\Temp'
Get-ChildItem -Path $UNC |
Where-Object { $_.LastWriteTime -lt (Get-Date) } |
ForEach-Object {
Copy-Item -Path $_.FullName -Destination $Path
}
虽然,一个改进:
$Path = 'C:\Temp'
$UNC = Get-ChildItem -Path '\\share' -File
$Local = Get-ChildItem -Path $Path -File
ForEach ($File in $UNC)
{
$Compare = $Local | Where-Object { $_.Name -eq $File.Name }
If ($Compare -and $Compare.LastWriteTime -gt $File.LastWriteTime)
{
Copy-Item -Path $File.FullName -Destination $Path -Force
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句