如何监视Windows中的文件夹运行批处理文件

时间:2016-12-21 12:16:47

标签: powershell batch-file

我正在关注此SO answer来监控文件夹,但它正在运行一次。我的意思是如果创建一个文件夹或文件批处理文件将运行,我再次创建一个文件夹或编辑它,它不会触发

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "C:\Users\uuu\Desktop\new folder\tttt"
    $watcher.Filter = "*.*"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true  

### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
    $action = { $path = $Event.SourceEventArgs.FullPath
                $changeType = $Event.SourceEventArgs.ChangeType
                $logline = "$(Get-Date), $changeType, $path"
                Add-content "D:\Copyof\PSlog.txt" -value $logline
                cmd.exe /k "D:\Copyof\move.bat"
              }

### DECIDE WHICH EVENTS SHOULD BE WATCHED 
    Register-ObjectEvent $watcher "Created" -Action $action
    Register-ObjectEvent $watcher "Changed" -Action $action
    Register-ObjectEvent $watcher "Deleted" -Action $action
    Register-ObjectEvent $watcher "Renamed" -Action $action
while ($true) {sleep 1}

1 个答案:

答案 0 :(得分:3)

这样才得到正式答案:

  

问题来自你的" / k"在cmd.exe上的标志?这意味着执行命令并保持不变。 - David Brabant

     

/ K Run Command然后返回CMD提示符。这对于测试,检查变量 - Matt

非常有用      

使用/ C代替。 - SomethingDark