Powershell - 检查新文件

Powershell - checking for new files

此代码似乎只有在创建 'new' 文件时才有效。它似乎不接受复制到文件夹中的文件,而这正是我所需要的。我们有一个处理 .csv 文件并将它们放入我想每天通过计划任务监控的文件夹的应用程序。我可以尝试更改此代码中的任何内容吗?

Param (
[string]$Path = "C:\Users\MG\Desktop\ScanFolder"
)                                                                        
$File = Get-ChildItem $Path | Where { $_.LastWriteTime -ge (Get-Date).AddHours(-1) }                                                                           
If ( $File ) {                                                          
Write-Output "Error File Found"                                              
}                                                                         
else { Write-Output "Nothing Found" }

测试 .CreationTime / .CreationTimeUtc 的值:

  • 对于新创建的个文件,它会反映创建时间。

  • 对于新 复制的 文件,它将反映文件复制到文件夹的时间(即使这比文件的 .LastWriteTime值).

应用于您的代码:

Param (
  [string] $Path = "C:\Users\MG\Desktop\ScanFolder"
)                                                                        

$file = Get-ChildItem $Path | Where { $_.CreationTime -ge (Get-Date).AddHours(-1) }                                                                           

If ($file) {                                                          
  "Error File Found"                                              
} else { 
  "Nothing Found" 
}