powershell - 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!"
}
}
解决方案
推荐阅读
- javascript - 为什么我的选择字段事件处理程序在 Chrome/Android 中不起作用?
- c++ - 出现语法和意外令牌错误,有人知道为什么吗?
- android - 使用 Kotlin 密封类路由到不同的屏幕
- php - Laravel:回显动态嵌套数组不起作用
- r - 抓取许多动态页面时的 rvest/httr 性能
- javascript - 自动检测格式 HEX 到 RGB 或 RGB 到 HEX
- java - Java 添加错误 Leetcode 39 组合问题
- php - 使用 codeIgniter 使用内容类型 application/x-www-form-urlencode 创建 curl get 请求的正确方法是什么
- docker - Docker daemon 错误响应:创建挂载时出错 - 权限被拒绝
- git - 我应该在 rebase 之前将 master 合并到一个共享的稳定集成分支吗?