问题
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\
The .bat
file is located in the same Sources
folder.
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.
回答1:
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
Note the use of 4 \
for 1 in directory name but nowhere else.
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
回答2:
Ok, you can do this in a rather complicated way with windows tools.
First, you need to enable Auditing.
- run GPEDIT and then go to: Computer Configuration --> Windows Settings --> Security Settings --> Local Policies --> Audit Policy --> Audit object Access
- enable i guess both success and failures.
- Now go to the folder you want to monitor, double-click and go to: security tab --> Advanced --> Auditing Tab
- add a rule with your user (or a groups or something like that) that will make a log for the rename (the is no rename event, but you can try different combination, like file creation, file delete or read/write attributes you can find)
- after that, now if you got to Event Viewer, Security log, a couple of events with Ids 4565/4663 will appear when the operation you selected is perfomed.
- last thing, open the Task Scheduler and create a new task, with the activation set to the trigger of the event and an action that will run the bat.
Some of the term may differ, my Windows is not in English so i might have translated something wrong. Also, you might do some tests to see if you have everything set up correctly.
Another option might be setting up a small application that monitors the folder and run the bat accordingly. You can do such a thing in Java or in other languages.
回答3:
This can be achieve easily with AutoHotkey using WatchDirectory() https://stackoverflow.com/a/30582696/883015
来源:https://stackoverflow.com/questions/41210568/bat-file-how-to-start-a-bat-file-when-i-rename-any-file-in-a-specific-folder