首页 > 解决方案 > Powershell - 在查看创建新文件的文件夹时 - 如果“Register-ObjectEvent”中的语句未触发

问题描述

目标:文件观察者监视暂存文件夹中的新文件。如果新文件名包含“JDE_Reimb”,则将文件移动到另一个文件夹“ $reimb_jde_path”,同时重命名新文件。

问题:If声明根本没有解雇我。直到创作$fullpath作品,但我的If声明没有执行。任何想法为什么?我还需要查看此文件夹中的 2 个其他文件,这些文件在文件名中包含特定字符串。在一个脚本中完成此任务的最佳方法是什么?

$reimbFile = "JDE_Reimb"
$reimb_jde_path = "\\rootfolder\reimbfolder"
$staging_folder_path = "\\rootfolder\stagingfolder\"                                    #"
$filter = "*.txt"

$watcherCRExport_Staging = New-Object IO.FileSystemWatcher $staging_folder_path, $filter -Property @{
  IncludeSubdirectories = $false
}

Register-ObjectEvent $watcherCRExport_Staging Created -SourceIdentifier foldertest -Action {

  $text = $eventArgs.Name
  Write-Host $text# Write-Host $eventArgs.ChangeType
  $fullpath = Join-Path -Path $staging_file_path -ChildPath $text# Write-Host "Full path is: $fullpath"

  If($text.Contains($reimbFile)) {
    $newtext = $text.Substring(0, $text.IndexOf("_") + 1 + $text.Substring($text.IndexOf("_") + 1).IndexOf("_"))# Write-Host "Created: $($newtext)"
    $newfilename = $newtext + ".txt"
    $nextpath = Join-Path -Path $reimb_jde_path -ChildPath $newtext
    Copy-Item-Path $fullpath -Destination $nextpath
    Write-Host "---------------------------------"
    Write-Host $newfilename
    Write-Host $fullpath
    Write-Host $nextpath
    Write-Host "Reimbursement Done copying"
    Write-Host "---------------------------------"
  }
  Else {
    Write-Host "No Match!"
  }

}

标签: powershell

解决方案


推荐阅读