我正在尝试创建一个脚本,主要任务是每次将文件添加到文件夹时执行一些PS命令。 我使用"做"它将循环进入程序。但是当我将文件添加到文件夹中时,命令只执行一次,然后我收到新添加文件的错误。下面是我的代码示例 你知道我怎么能这样做吗? 提前谢谢!
do{
Dir *.txt | rename-item -newname { [io.path]::ChangeExtension($_.name, "eml")}
Invoke-Item C:\Users\*.eml
Remove-Item C:\Users\*.eml
} until ("condition that is never met")
答案 0 :(得分:0)
您正在寻找FileSystemWatcher
Register-ObjectEvent在添加文件时收到通知:
$folder = 'c:\test'
$filter = '*.txt'
$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{ IncludeSubdirectories = $true}
$onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {
$path = $Event.SourceEventArgs.FullPath
# A file has been added, run your ps commands here. e. g.
write-host $path
}
要取消注册该事件,请使用:
Unregister-Event -SourceIdentifier FileCreated