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

问题描述 投票:0回答:1

我正在按照这个SO答案来监视文件夹,但它只能工作一次。如果我创建文件夹或文件,批处理文件将运行,但是当我创建或编辑文件夹时,它不会触发。

### 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}
powershell batch-file
1个回答
3
投票

这样才能得到官方答复:

问题不是来自 cmd.exe 上的“/k”标志吗?这意味着执行命令并保持。 – 大卫·布拉班特

/K 运行命令,然后返回到 CMD 提示符。这对于测试、检查变量很有用 – Matt

使用 /C 代替。 – 黑暗的东西

© www.soinside.com 2019 - 2024. All rights reserved.