将文件添加到文件夹时运行 .bat 文件的 Powershell 脚本 [英] Powershell script to run a .bat file when a file is added to a folder

查看:29
本文介绍了将文件添加到文件夹时运行 .bat 文件的 Powershell 脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想监视 Windows 7 文件夹并在将任何新文件添加到文件夹时运行 .bat 文件.似乎我应该能够使用安装在计算机上的 powershell 来做到这一点.

I'd like to monitor a Windows 7 folder and have a .bat file run when any new files are added to the folder. It seems like I should be able to do this using powershell, which is installed on the computer.

我已经阅读了一些答案,例如 这个 但我还不能通过修改我看到的东西来使任何东西工作.详情:

I've read some answers like this one but I'm not able to get anything to work yet by modifying what I see. Details:

要监控的文件夹:

c:\aaa\bbb\monitorThis

每当将 .htm 文件添加到受监控文件夹时运行的批处理文件:

Batch file to run whenever an .htm file is added to the monitored folder:

c:\aaa\bbb\runA.bat

Powershell 脚本文件:

Powershell script file:

c:\aaa\bbb\folderWatcher.ps1

谁能描述一下folderWatcher.ps1的内容应该是什么样的,包括运行.bat文件的命令、注册和取消注册事件等等?

Can someone describe what the content of folderWatcher.ps1 should look like, including the line containing the command to run the .bat file, registering and unregistering the event, and so on?

另外,右键单击 .ps1 文件并选择Run with PowerShell"是否是启动监控的方式,如果是,您如何停止它?

Also, is right-clicking the .ps1 file and selecting "Run with PowerShell" the way to start the monitoring, and if so, how do you stop it?

更新:

根据要求,这是我目前对 folderWatcher.ps1 所做的,但这只是一个开始,从我所看到的想法来看:

As requested, here is what I have so far for folderWatcher.ps1, but it's just a start, from ideas I've seen:

$folder = "c:\aaa\bbb\toConvert"
$filter = "*.*"

$fsw = new-object System.IO.FileSystemWatcher $folder, $filter -Property @{
  IncludeSubDirectories=$false
NotifyFilter = [System.IO.NotifyFilters]'FileName, LastWrite'
}

 $onCreated = Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action {

  Start-Process cmd -ArgumentList "/c runA.bat" -WorkingDirectory "C:\aaa\bbb"
}

注意:重新过滤,我不在乎添加了什么样的文件,因为我们只会将 .htm 文件放在该文件夹中,所以添加到其中的任何内容我都想触发 .bat.

Note: re filtering, I don't care what kind of file is added, since we will only be putting .htm files in that folder, so anything added to it I want to trigger the .bat.

更新二

我尝试了下面来自 Dennis 的代码,但什么也没得到.我只是仔细检查了我所有的路径,以确保它们与他拥有的路径相同.我也刚刚用更简单的路径制作了一个新的测试版本,这样我就可以准确地发布我拥有的内容而无需匿名:

I tried the code from Dennis below but I get nothing. I just double-checked all my paths to be sure they were the equivalent of what he has. I also just made a new test version with simpler paths so I can post exactly what I have without having to anonymize:

 $folder = 'C:\Developer\psTest' # Enter the root path you want to monitor. 
 $filter = '*.htm'  # You can enter a wildcard filter here. 

 $fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property  @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}

 Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action { 
 $name = $Event.SourceEventArgs.Name 
 $changeType = $Event.SourceEventArgs.ChangeType 
 $timeStamp = $Event.TimeGenerated 
 Write-Host "The file '$name' was $changeType at $timeStamp" -fore green 
 cmd.exe /c 'C:\Developer\psTest\runAnt.bat'
 }

要清楚我在做什么:

我现在有一个名为 C:\Developer\psTest\FolderWatcherTest.ps1 的文件,其中包含正上方的代码.

I have a file now called C:\Developer\psTest\FolderWatcherTest.ps1 that contains the code directly above.

当我右键单击它并选择使用 PowerShell 运行时,控制台窗口会闪烁一些文本,但在关闭之前阅读速度太快.

When I right-click it and select Run with PowerShell, a console window flashes with some text but it's too fast to read before it closes.

当我将一个 .htm 文件拖入 C:\Developer\psTest 时,没有任何反应.

When I then drag an .htm file into C:\Developer\psTest, nothing happens.

我将注销代码放入一个名为 FolderWatcherStop.ps1 的文件中,当我点击它时,控制台闪烁着一些红色文本,同样太快而无法阅读,然后它关闭了.

I put the unregister code into a file called FolderWatcherStop.ps1, and when I click that, the console flashes with some red text, again too quick to read, then it closes.

我做错了什么?有一点我很确定.

What am I doing wrong? Something I'm sure.

更新三

按照丹尼斯的建议,我完成了这项工作.这需要稍微修改批处理文件以包含我想要运行的 ANT 构建的完整路径,但它有效.

Following Dennis's advice I got this working. This took modifying the batch file a little to include the full path of the ANT build I want to run, but it works.

注意:我想我知道为什么触发器会重复,会更新.

Note: I think I just got why the trigger is repeating, will update.

推荐答案

给你:

$folder = 'f:\test' # Enter the root path you want to monitor. 
$filter = '*.html'  # You can enter a wildcard filter here. 

$fsw = New-Object IO.FileSystemWatcher $folder, $filter -Property @{IncludeSubdirectories = $false;NotifyFilter = [IO.NotifyFilters]'FileName, LastWrite'}

Register-ObjectEvent $fsw Created -SourceIdentifier FileCreated -Action { 
$name = $Event.SourceEventArgs.Name 
$changeType = $Event.SourceEventArgs.ChangeType 
$timeStamp = $Event.TimeGenerated 
Write-Host "The file '$name' was $changeType at $timeStamp" -fore green 
write-host "test"
Invoke-Item 'f:\test\test.bat'
}

这篇关于将文件添加到文件夹时运行 .bat 文件的 Powershell 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆