.bat文件-重命名特定文件夹中的任何文件时如何启动.bat文件 [英] .bat file - How to start a .bat file when I rename any file in a specific folder

查看:92
本文介绍了.bat文件-重命名特定文件夹中的任何文件时如何启动.bat文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在重命名Sources文件夹中的文件后运行我的.bat文件:

I want my .bat file to run after I rename a file in the Sources folder that is located here:

C:\Users\UserName\Videos\Gameplays\HeroesOfTheStorm\Sources\

.bat文件位于同一Sources文件夹中.

The .bat file is located in the same Sources folder.

如何在不手动双击.bat文件的情况下执行此操作?我希望它在重命名Sources文件夹中的文件后自动运行.

How can I do that without double-clicking on the .bat file manually? I want it to run automatically after I rename a file in the Sources folder.

推荐答案

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set MonitoredEvents = WMI.ExecNotificationQuery("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' and TargetInstance.GroupComponent= 'Win32_Directory.Name=""C:\\\\Users\\\\David Candy""'")
Do
    Wscript.Echo MonitoredEvents.NextEvent.TargetInstance.PartComponent
    WshShell.Run "cmd /c ""C:\folder\batchfile.bat""", 1, false
Loop

请注意,目录名中的1使用4 \,但无其他地方.

Note the use of 4 \ for 1 in directory name but nowhere else.

这是一个vbs文件.如果您在该目录中重命名或创建文件,它将监视目录并运行命令. WITHIN 10表示它每10秒进行一次测试.

It a vbs file. It monitors a directory and will run commands if you rename or create files in that directory. WITHIN 10 means it tests every 10 secs.

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WMI = GetObject("winmgmts:{impersonationLevel=impersonate}!\\.\root\cimv2")
Set MonitoredEvents = WMI.ExecNotificationQuery("SELECT * FROM __InstanceCreationEvent WITHIN 10 WHERE Targetinstance ISA 'CIM_DirectoryContainsFile' and TargetInstance.GroupComponent= 'Win32_Directory.Name=""C:\\\\Users\\\\David Candy""'")
Do
    WMIPath = Split(MonitoredEvents.NextEvent.TargetInstance.PartComponent, "=")(1)
    FilePath = Replace(WMIPath, "\\", "\")
    WshShell.Run "cmd /k echo File Renamed is " & FilePath & "&" & Filepath
Loop

这篇关于.bat文件-重命名特定文件夹中的任何文件时如何启动.bat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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