Notice: This forum has been recovered from an old backup, so some content, links, and dates may be outdated. The forum is currently read-only while we restore sign-in and registration functionality. Details

If you find this forum valuable and would like to help keep it online, donations to help cover hosting and domain costs are greatly appreciated, but never expected. You can support the forum through Buy Me a Coffee or Ko-fi. Thank you for helping preserve the EventGhost community.

Directory Watcher

If you have a question or need help, this is the place to be.
User avatar
Luca Brasi
Experienced User
Posts: 262
Joined: Sat Oct 11, 2008 12:39 pm

Re: Directory Watcher

Post by Luca Brasi »

Hey guys,

I have absolutely no clue about python. So I would like to ask for your help.
I think my task is a really simple...
This is what I want to do:
I want to trigger an action only if files with extensions like .avi,.mp4 or .srt are created.
I am sorry if this has been asked before but I couldn't find anything here.
I understand that I would need some sort of python script which analyses the payload.

Could someone of you please help me with this?

Thanks in advance
Luca
Win10 x64 Prof. / Eventghost latest / MCE Plugin / MCE RC6 Receiver / Logitech Harmony Hub / MediaPortal / kodi
valdolopes
Posts: 2
Joined: Sat Dec 01, 2012 3:52 pm

Re: Directory Watcher

Post by valdolopes »

Problems torrents that contain folders. If i download a torrent that is a file only, it works great, but if it is a file inside a folder the python script returns an error: IOError: [Errno 13] Permission denied: u'D:\\Documentos\\Organizar\\renomear\\')


Please do not find the solution anywhere
cfull1
Experienced User
Posts: 124
Joined: Thu Aug 19, 2010 4:52 am

Re: Directory Watcher

Post by cfull1 »

I'm not exactly sure what valdolopes is trying to say, but DirectoryWatcher can also watch subdirectories.

Luca try out this script:

Code: Select all

import shutil
import os

FileName = ''.join(eg.event.payload)
FileExtension = FileName[-3:]
isFolder = os.path.isdir(FileName)

if "avi" == FileExtension and isFolder == False:
    shutil.move(FileName, "D:\\Downloads\\Movies")
or you could trigger a new event or whatever.
User avatar
Luca Brasi
Experienced User
Posts: 262
Joined: Sat Oct 11, 2008 12:39 pm

Re: Directory Watcher

Post by Luca Brasi »

Thanks cfull1!
Working perfect! I tweaked the last line so an event will be triggered...
Any chance you could give a hint how I could only get only the filename and not the complete path of the file as payload? ("test.avi" and not "C:\Users\user1\Desktop\test.avi")
Win10 x64 Prof. / Eventghost latest / MCE Plugin / MCE RC6 Receiver / Logitech Harmony Hub / MediaPortal / kodi
bodiroga
Posts: 4
Joined: Thu Mar 22, 2012 8:07 pm

Re: Directory Watcher

Post by bodiroga »

Luca Brasi wrote:Thanks cfull1!
Working perfect! I tweaked the last line so an event will be triggered...
Any chance you could give a hint how I could only get only the filename and not the complete path of the file as payload? ("test.avi" and not "C:\Users\user1\Desktop\test.avi")
Hi Luca!

Here is my suggestion, although I'm not a python expert:

Code: Select all

import os

Path = ''.join(eg.event.payload)
FileName = Path.split('\\')[len(Path.split('\\'))-1]
FileExtension = FileName.split('.')[len(FileName.split('.'))-1]

isFolder = os.path.isdir(Path)

if "avi" == FileExtension and isFolder == False:
    eg.TriggerEvent('Suffix','Payload','Prefix')
You can see what the code does looking at it in this way:

Code: Select all

import os

Path = ''.join(eg.event.payload)
Path_split = Path.split('\\')
FileName = Path_split[len(Path_split)-1]
FileName_split = FileName.split('.')
FileExtension = FileName_split[len(FileName_split)-1]

isFolder = os.path.isdir(Path)

if "avi" == FileExtension and isFolder == False:
    eg.TriggerEvent('Suffix','Payload','Prefix')
Let's go step by step.

1.- Path will be: C:\Users\Aitor\Desktop\Prueba\Movie.avi (Personal example)
2.- As files directories are separated by "\" in Windows, we will use the Python function "Split" to split the path. Path_split will be an array [u'C:', u'Users', u'Aitor', u'Desktop', u'Prueba', u'Movie.avi'] with 6 items.
3.- Although Path_split's length is 6, in Python array's are counted from 0..5, so if we want to take the last item of the array we need to do FileName = Path_split[len(Path_split)-1] = Path_split[6-1] = Path_split[5] = Movie.avi
4.- To take filename's extension we will follow the same approach, but instead of spliting with "\", using split('.') because filenames and it's extensions are always separated by dots. FileName_split = [u'Movie', u'xml']
5.- FileExtension again is going to be FileName_split[len(FileName_split)-1] = FileName_split[2-1] = FileName[1]. Remenber that FileName[0] = Movie because array's items start at 0.

Et voila.

Thinking about how directories and extensions are separated and with the split function you can make your personal scripts very easily and without great programming skills (believe me, it's my own case :D)

If you have any question don't hesitate to ask, we are here to help each other ;)

Best regards,

Aitor
User avatar
Luca Brasi
Experienced User
Posts: 262
Joined: Sat Oct 11, 2008 12:39 pm

Re: Directory Watcher

Post by Luca Brasi »

Hi bodiroga/Aitor!

thank you so much for this! Your explanation is perfect and it is working great!
Very nice of you taking the time to explain this to me, I think this is a good start to try these scripts by myself... :-))

Thanks again!
Luca
Win10 x64 Prof. / Eventghost latest / MCE Plugin / MCE RC6 Receiver / Logitech Harmony Hub / MediaPortal / kodi
bodiroga
Posts: 4
Joined: Thu Mar 22, 2012 8:07 pm

Re: Directory Watcher

Post by bodiroga »

Luca Brasi wrote:Hi bodiroga/Aitor!

thank you so much for this! Your explanation is perfect and it is working great!
Very nice of you taking the time to explain this to me, I think this is a good start to try these scripts by myself... :-))

Thanks again!
Luca
You're welcome Luca ;-)

You can change some part of the scripts according to this:

http://stackoverflow.com/questions/9303 ... -of-a-list

It's better to use the form Path_split[-1] rather tan Path_split[len(Path_split)-1). Shorter, faster and more elegant :D

Best regards and merry Christmas to all.

Aitor
thach1ef
Posts: 9
Joined: Mon Jun 01, 2015 8:25 pm

Re: Directory Watcher

Post by thach1ef »

Hi Pako,

I know this thread is old and seems to have been derailed by another topic, but I wanted to ask you something about your python code that this thread was originally about.

I'm not good with python code, but it seems that the code checks to see if a file is being used, and if not it sets up DirectoryWatcher.AccessAllowed. However if the file is in use, it continues to check every 5 seconds? Is that correct?

If I only want to check 1 time, how do I edit this code to do that?
thach1ef
Posts: 9
Joined: Mon Jun 01, 2015 8:25 pm

Re: Directory Watcher

Post by thach1ef »

I've been trying my best with this for the past couple days and no luck. Does anyone have any thoughts? I took Pako's code and removed some pieces so that I'm left with

Code: Select all

def CheckFileAccessFunction_1(filePath):
    from os import path
    f = None
    if path.exists(filePath):
        try:
            f = open(filePath, 'ab+', 2)
            if f:
                eg.TriggerEvent("AccessAllowed",prefix="DirectoryWatcher2", payload=(filePath,))
        finally:
            if f:
                f.close()
My hope is that this would check if the file can be accessed, if it can, it sets up DirectoryWatcher2.AccessAllowed, otherwise it does nothing. Unfortunately, it doesn't appear to work as I'm expecting. I don't think it's setting up DirectoryWatcher2.AccessAllowed, or I'm doing something wrong :( This is very new to me, so could be the latter.
thach1ef
Posts: 9
Joined: Mon Jun 01, 2015 8:25 pm

Re: Directory Watcher

Post by thach1ef »

No one is able to help me out with this?
krysn95
Posts: 10
Joined: Thu Jul 16, 2015 10:42 am

Re: Directory Watcher

Post by krysn95 »

I can not make the next with EG:

The event should be an url open, and the macro is speaker mute, but unfortunatelly, I can not make this...

How can I solve that If I launch a file or app, then do something automatically?
eirik226
Experienced User
Posts: 142
Joined: Wed Nov 07, 2012 5:22 pm

Re: Directory Watcher

Post by eirik226 »

Sorry for necroing and old thread but i've run into some problems with Directory Watcher.

I have it watching a network drive installed on my NAS (I do not think any permission problems are here) - So I have it watching z:/tvshows for example and i've pressed the button to "watch sub-directory" - when I create a folder in the tvshow folder everything is fine, but if i create a folder in tvshows/fargo for example the plugin does not detect anything.

Anyone know what can be wrong?
rebirth24
Posts: 9
Joined: Sat Jul 15, 2017 7:40 pm

Re: Directory Watcher

Post by rebirth24 »

Is there a way to PREVENT this from reporting changes to folders within a directory? Only Files? Or is there another Plugin that does that?
cableghost
Posts: 37
Joined: Thu Oct 10, 2013 9:43 pm

Re: Directory Watcher

Post by cableghost »

Is it possible to watch multiple directories? I can add another plugin, but what do I put in for the Event?
User avatar
Boolean263
Plugin Developer
Posts: 81
Joined: Sat Aug 12, 2017 7:52 pm
Location: Ottawa, Canada
Contact:

Re: Directory Watcher

Post by Boolean263 »

cableghost wrote:Is it possible to watch multiple directories? I can add another plugin, but what do I put in for the Event?
You're right, that you'll have to load multiple copies of the plugin. The second instance of the plugin will create events such as `DirectoryWatcher2.Updated`. This is handy if you want to do different things for different watched directories.

If you want all your watched directories to trigger the same macro, the simplest way is to drag events from each plugin into your macro. You might also be able to use a wild-card event string, but I'm not clear what the syntax would be.
Windows 10 Home, version 1703, build 15063.540 ┬À EventGhost 0.5.0-rc4 ┬À wxPython 3.0.2.0
Python programmer for fun and profit. Dabbler in EventGhost plugin writing. Wiki gardener. General geek.
Post Reply