2

I was following this SO answer to monitor a folder , but it is working one time. I mean if create a folder or file the batchfile will run , I created again a folder or edite it , it wont trigger

### SET FOLDER TO WATCH + FILES TO WATCH + SUBFOLDERS YES/NO
    $watcher = New-Object System.IO.FileSystemWatcher
    $watcher.Path = "C:\Users\uuu\Desktop\new folder\tttt"
    $watcher.Filter = "*.*"
    $watcher.IncludeSubdirectories = $true
    $watcher.EnableRaisingEvents = $true  

### DEFINE ACTIONS AFTER AN EVENT IS DETECTED
    $action = { $path = $Event.SourceEventArgs.FullPath
                $changeType = $Event.SourceEventArgs.ChangeType
                $logline = "$(Get-Date), $changeType, $path"
                Add-content "D:\Copyof\PSlog.txt" -value $logline
                cmd.exe /k "D:\Copyof\move.bat"
              }

### DECIDE WHICH EVENTS SHOULD BE WATCHED 
    Register-ObjectEvent $watcher "Created" -Action $action
    Register-ObjectEvent $watcher "Changed" -Action $action
    Register-ObjectEvent $watcher "Deleted" -Action $action
    Register-ObjectEvent $watcher "Renamed" -Action $action
while ($true) {sleep 1}
Community
  • 1
  • 1
Moudiz
  • 7,211
  • 22
  • 78
  • 156
  • Don't use these alot but we cant see `move.bat` so something could be locked there possibly. If you remove that line does pslog.txt update? Not sure if you need to remove the old event watchers before changes take effect. – Matt Dec 21 '16 at 12:45
  • @Matt yes when I did remove the batch file it worked normally again , not sure what its making locked in the batchfile , because if I run the bach manually it works normally. I cannot show the full move.bat here because its long. do you have any idea how to stop locking it ? a batch file command or something ? – Moudiz Dec 21 '16 at 12:49
  • 2
    Isn't the problem coming from your "/k" flag on cmd.exe? It means execute the command and remain. – David Brabant Dec 21 '16 at 12:56
  • @DavidBrabant Oh how did I miss that. `/K Run Command and then return to the CMD prompt. This is useful for testing, to examine variables` – Matt Dec 21 '16 at 12:57
  • @DavidBrabant so the /k is the mistake .. with what should I replace it – Moudiz Dec 21 '16 at 13:34
  • 4
    Use a `/C` instead. – SomethingDark Dec 21 '16 at 13:49
  • 1
    @Moudiz, you can get help for any batch file command you are trying to run by typing the command name followed by a slash and a question mark. ex: `cmd /?` – Squashman Dec 21 '16 at 14:17
  • 1
    See http://stackoverflow.com/questions/41210568/bat-file-how-to-start-a-bat-file-when-i-rename-any-file-in-a-specific-folder/41214708#41214708 –  Dec 21 '16 at 20:05

1 Answers1

3

Just so this gets an official answer:

Isn't the problem coming from your "/k" flag on cmd.exe? It means execute the command and remain. – David Brabant

/K Run Command and then return to the CMD prompt. This is useful for testing, to examine variables – Matt

Use a /C instead. – SomethingDark

Community
  • 1
  • 1
geisterfurz007
  • 5,292
  • 5
  • 33
  • 54