gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 23:24:11 24 4
gpt4 key购买 nike

我正在按照此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}

最佳答案

这样便得到了官方的答案:

Isn't the problem coming from your "/k" flag on cmd.exe? It means execute the command and remain. – David Brabant

/K Run Command and then return to the CMD prompt. This is useful for testing, to examine variables – Matt

Use a /C instead. – SomethingDark

关于powershell - 如何在Windows中监视文件夹并运行批处理文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41262648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com