0

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.

aschipfl
  • 33,626
  • 12
  • 54
  • 99
Reaper
  • 1
  • Is there any restriction? Like, do you need to use only windows options, or you can like write a small program running to help you? – bracco23 Dec 18 '16 at 17:11
  • No restrictions. I want it to auto-start itself after I rename a file. – Reaper Dec 18 '16 at 17:23
  • P.S. : If I don't need to code it, there is no problem at all. – Reaper Dec 18 '16 at 17:25
  • Look at a program called WatchDirectory. I have used that in the past to trigger events like that. – Squashman Dec 18 '16 at 18:35
  • @Squashman Thanks but I prefer not downloading a program. I'm a little bit paranoid (and pretty careful). – Reaper Dec 18 '16 at 19:20
  • I work for a Fortune 500 company and we have dozens of licenses for it. – Squashman Dec 18 '16 at 20:38
  • I said I don't want to download it... – Reaper Dec 19 '16 at 01:34
  • @Reaper, I realize you said you did not want to download it, but you said you were paranoid and careful about what you download, which you should be. I was just assuring you that the program is legitimate. It works very well. I have over 600 WatchDirectory tasks that run 24/7/365. – Squashman Dec 19 '16 at 15:05
  • You cannot trigger a batch file on renaming a file; you could use a script running in the background observing the directory, but it is not that easy to detect renames only (what if the file is deleted and another one is added?). You could consider the opposite way: let the batch file do the file rename and the other action (which you did not show) afterwards... – aschipfl Dec 20 '16 at 14:59

3 Answers3

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
  • Not sure where to type that and what it does exactly. But heh the thing is that I don't want to type ever and ever again the code. I want that each time I rename a file in the "Sources" folder, it runs the bat file I made. – Reaper Dec 19 '16 at 01:42
0

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.

bracco23
  • 2,181
  • 10
  • 28
  • Thanks for your answer. Unfortunately, I don't have GPEDIT because it doesn't exist in Windows 10 Home version. I don't want to download such a thing, or mess with it because I'm affraid I'll break something. I prefer double-clicking the .bat file for now! Maybe I'll find another way later. – Reaper Dec 18 '16 at 19:18
0

This can be achieve easily with AutoHotkey using WatchDirectory() https://stackoverflow.com/a/30582696/883015

Community
  • 1
  • 1
Joe DF
  • 5,438
  • 6
  • 41
  • 63