Windows:文件监控脚本(批处理/VBS)
我目前正在编写一个脚本来创建自定义备份脚本,我唯一缺少的是文件监视器。我需要某种形式的脚本来监视文件夹中的文件更改,然后使用已更改的文件运行命令。
因此,例如,如果文件发生更改,它将执行“c:/syncbatch.bat %Location_Of_File%”
I'm currently in working on a script to create a custom backup script, the only piece I'm missing is a file monitor. I need some form of a script that will monitor a folder for file changes, and then run a command with the file that's changed.
So, for example, if the file changes, it'll execute "c:/syncbatch.bat %Location_Of_File%"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 VBScript 中,您可以通过订阅 WMI
来监视文件夹中的文件更改__InstanceModificationEvent
事件。这些文章包含示例代码,您可以从中学习并适应您的特定需求:In VBScript, you can monitor a folder for file changes by subscribing to the WMI
__InstanceModificationEvent
event. These articles contain sample code that you can learn from and adapt to your specific needs:调用 WMI 相当神秘,它会导致 WMI 服务开始运行,这可能会导致膨胀,因为它相当大,并且您实际上无法在不重新启动的情况下取消向其请求的文件更改通知。一些尝试从 Dropbox 文件夹进行远程打印的人发现,运行无限循环并在循环中调用 10 秒 WScript.Sleep 的简单 VBScript 程序使用的资源要少得多。当然,要停止它,您必须任务杀死该脚本或程序,然后使用一些退出触发器,它可以在监视文件夹中找到一个专门命名的空文件,但这仍然比弄乱 WMI 更容易。
文件夹间谍 http://venussoftcorporation.blogspot.com/2010/05/thefolderspy.html< /a>
是一个免费的、基于 DOT.NET 的轻量级文件/文件夹监视 GUI 应用程序,我之前使用过它来根据文件更改运行脚本。看起来新版本可以将事件文件名传递给启动的命令。我的旧版本尚不支持文件事件信息,因此启动时,我的脚本必须实例化文件系统对象并扫描监视的文件夹以根据日期戳和大小等标准查找新文件。
如果您在可选脚本调用条目上输入 myscript.vbs“*f”,则此较新版本似乎允许您将文件名传递给脚本。当传递文件夹名称中包含空格的文件路径时,引号可能很重要。请记住,如果您正在观看更改事件,随着文件的增长或编辑,您会收到很多更改事件,通常您只需要文件添加或删除的通知。
脚本可以执行的另一个技巧是将文件大小放入变量中,休眠几秒钟,然后再次检查文件以查看其是否已更改。如果它在几秒钟内没有发生变化,您通常可以假设创建的任何内容都已完成写入磁盘。如果它不断变化,则循环直到稳定。
Calling WMI is fairly cryptic and it causes the WMI service to start running which can contribute to bloat since its fairly large and you really can't cancel the file change notifications you've requested from it without rebooting. Some people experimenting with remote printing from a Dropbox folder found that a simple VBScript program that ran an endless loop with a 10 second WScript.Sleep call in the loop used far less resource. Of course to stop it you have to task kill that script or program into it some exit trigger it can find like a specifically named empty file in the watch folder, but that is still easier to do than messing with WMI.
The Folder Spy http://venussoftcorporation.blogspot.com/2010/05/thefolderspy.html
is a free lightweight DOT.NET based file/folder watching GUI application I'ved used before to run scripts based on file changes. It looks like the new version can pass the event filename to the launched command. The old version I had didn't yet support file event info so when launched, my script had to instance a File System Object and scan the watched folder to locate the new files based on criteria like datestamps and sizes.
This newer version appears to let you pass the file name to the script if you say myscript.vbs "*f" on the optional script call entry. Quotes can be important when passing file paths that have spaces in folder names. Just remember if you are watching change events you will get a lot of them as a file grows or is edited, usually you just want notification of file adds or deletes.
Another trick your script can do is put the file size in a variable, sleep for a few seconds, and check the file again to see if its changed. if it hasn't changed in a few seconds you can usually assume whatever created it is done writing it to disk. if it keeps changing just loop until its stable.