检查循环中的文件创建

时间:2014-04-08 03:35:51

标签: powershell

当.exe显示在文件夹上时,我可以自动运行此脚本吗?

Get-ChildItem -Filter *.exe | ForEach {
    &$_.Fullname /s

我想要一个脚本来检查.exe文件是否显示在文件夹上,然后运行该命令,否则再次检查.exe文件。

1 个答案:

答案 0 :(得分:2)

您可以使用FileSystemWatcher收听这些事件。

Get-EventSubscriber -SourceIdentifier ExeCreated -ErrorAction SilentlyContinue | Unregister-Event;
$Watcher = New-Object -TypeName System.IO.FileSystemWatcher;
$Watcher.Filter = '*.exe';
$Watcher.Path = 'c:\test';

$Action = { Write-Host -Object ('New file created: {0}' -f $event.SourceArgs[1].Name); };
Register-ObjectEvent -InputObject $Watcher -EventName Created -Action $Action -SourceIdentifier ExeCreated;