Windows:文件监控脚本(批处理/VBS)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5762064/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:42:10  来源:igfitidea点击:

Windows: File Monitoring Script (Batch/VBS)

windowsvbscriptbatch-file

提问by Dustin

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%"

因此,例如,如果文件更改,它将执行“c:/syncbatch.bat %Location_Of_File%”

Any help on where I should start is greatly appreciated. Thanks a lot!

非常感谢我应该从哪里开始的任何帮助。非常感谢!

采纳答案by Helen

In VBScript, you can monitor a folder for file changes by subscribing to the WMI __InstanceModificationEventevent. These articles contain sample code that you can learn from and adapt to your specific needs:

在 VBScript 中,您可以通过订阅 WMI__InstanceModificationEvent事件来监视文件夹中的文件更改。这些文章包含您可以学习并适应您的特定需求的示例代码:

回答by klaatu

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.

调用 WMI 是相当神秘的,它会导致 WMI 服务开始运行,这可能会导致膨胀,因为它相当大,并且您真的无法在不重新启动的情况下取消您从它请求的文件更改通知。一些尝试从 Dropbox 文件夹进行远程打印的人发现,一个简单的 VBScript 程序运行无限循环,在循环中调用 10 秒 WScript.Sleep 使用的资源要少得多。当然,要阻止它,您必须执行任务杀死该脚本或将程序写入其中一些退出触发器,它可以在监视文件夹中找到一个特定命名的空文件,但这比弄乱 WMI 更容易做到。

The Folder Spy http://venussoftcorporation.blogspot.com/2010/05/thefolderspy.htmlis 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.

Folder Spy http://venussoftcorporation.blogspot.com/2010/05/thefolderspy.html是一个免费的轻量级基于 DOT.NET 的文件/文件夹监视 GUI 应用程序,我以前曾用于根据文件更改运行脚本。看起来新版本可以将事件文件名传递给启动的命令。我的旧版本尚不支持文件事件信息,因此在启动时,我的脚本必须实例化一个文件系统对象并扫描监视的文件夹以根据日期戳和大小等标准定位新文件。

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.

如果您在可选脚本调用条目上说 myscript.vbs "*f",则此较新版本似乎允许您将文件名传递给脚本。传递文件夹名称中包含空格的文件路径时,引号可能很重要。请记住,如果您正在观看更改事件,随着文件的增长或编辑,您会收到很多更改事件,通常您只需要文件添加或删除的通知。

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.

脚本可以做的另一个技巧是将文件大小放在一个变量中,休眠几秒钟,然后再次检查文件以查看它是否已更改。如果它在几秒钟内没有改变,您通常可以假设它创建的任何内容都已将其写入磁盘。如果它不断变化只是循环直到它稳定。