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 - Copy Files

If you have a question or need help, this is the place to be.
kirkb
Posts: 4
Joined: Sat Feb 06, 2010 6:17 pm

Directory Watcher - Copy Files

Post by kirkb »

I'm trying to monitor a folder for completed torrents (c:\downloads\completed). When a new file is dropped in that folder, I want to copy the file that triggered the event to another folder. I don't want to move it because I want to continue seeding the file. I do want to make a copy of it so I can then perform other actions on it (renaming etc) without impacting the original file. EventGhost appears that is may be able to do this. I just dont know where to start. Obviously, directory watcher will be the starting point. However, I dont quite get how to pass variables (triggering filename) to scripts. Can some one point me in the best direction.

Awesome App BTW. Works Great with my USB-UIRT / XBMC.
merdok1981
Posts: 8
Joined: Wed Dec 16, 2009 12:53 am

Re: Directory Watcher - Copy Files

Post by merdok1981 »

I'd like to see this functionality too, espeically if there is a way for it to only be performed on files with a specific extension (eg .mp4). I've not got a clue how to use Python so I would not know where to start.
User avatar
Livin
Experienced User
Posts: 792
Joined: Wed Oct 08, 2008 4:56 am

Re: Directory Watcher - Copy Files

Post by Livin »

Ideally it would monitor the root folder and sub-folders and then unRAR files that are RAR'd also.
setup... XBMC, W7MC for DVR & Live OTA TV, JRMC for multi-zone audio, EG, MiCasaVerde Vera3, USB-UIRT IR receiver, Harmony remote, 5.2 home theater system
kirkb
Posts: 4
Joined: Sat Feb 06, 2010 6:17 pm

Re: Directory Watcher - Copy Files

Post by kirkb »

As for the RAR files, it would require additional scripting. However, I use the following script to accomplish copying a file:

import shutil
filename = ''.join(eg.event.payload)
shutil.copy(filename,'C:\\torrent\\therenamer\\')

Just add an action type of python script and paste the above in the editor. You will of course want to replace the path with a folder of your liking. Important to note the \\ in the path. This resolves to a single slash. It is important to keep the double slash, otherwise the script wont work. Also, if you are using a network path (i.e. \\mydrive\media\tv\) you will want to double slash each slash in the string (i.e. \\\\mydrive\\media\\tv\\).

Keep in mind that this script makes a copy of the file. In my case, i have a folder c:\torrent\complete. Once Vuze has completed downloading a show, EventGhost triggers my script. Prior to the script, I have a 10sec wait. Then I perform the copy. The script picks up the filename and path that triggered the job. So, you only have to worry about your destination path in the 3rd line of the script. Again, this makes a copy of whatever VUZE dumps in my completed folder. The reason I make a copy and not attempt to move the file is that VUZE and most other torrent apps keep a lock on the file while its seeding. This will cause a move to fail. So, I create a duplicate that i push to a folder called therenamer. From there, I kick off an application called ... TheRenamer which standardizes the name of the TV show and moves the file to its final destination where XBMC can view it. I then use a VUZE plugin "Download Deleter & Starter". I've configured the plugin to delete the original file in my completed folder once I've seeded to my desired ratio.

So far everything works great. I've had some issues when the torrents are running hot and heavy and the network copies start up. I've controlled the impact by keeping everything on the local HD except for when TheRenamer moves the file, it goes to a network drive. Keep in mind my setup is running on a VM on my living room HTPC. I decided to use a VM so I could log into it without having to log into the HTPC. The allows for uninterrupted TV viewing while I'm tweaking or monitoring things in the background. Works extremely well.

One more thing to keep in mind. Most TV show download as a single file. However, I sometimes get a folder plus a few misc files. TheRenamer handles with with its ability to scan subfolders. It then moves the file. However, it leaves behind the folder and misc files. This can be manually deleted and causes no serious issue other than clutter. I'll probably create a job at some point to clean up the clutter for me. Otherwise, I may update the python script to only copy certain file types.

See the attached image for what the entire EventGhost job looks like. I add a delay on startup to ensure that the drive is accessible. Not required for a local drive. However, for a network drive, you may want to give it some time. I do NOT recommend monitoring a network drive.

Enjoy, Kirk
Attachments
Screen shot 2010-02-16 at 11.22.12 PM.png
Screen shot 2010-02-16 at 11.22.12 PM.png (19.2 KiB) Viewed 22196 times
Flook
Posts: 20
Joined: Thu Mar 04, 2010 1:07 pm

Re: Directory Watcher - Copy Files

Post by Flook »

For rar files I use parnrar. It can be run by command line so you can have a directory monitor event launch it with command line options.
User avatar
Livin
Experienced User
Posts: 792
Joined: Wed Oct 08, 2008 4:56 am

Re: Directory Watcher - Copy Files

Post by Livin »

This would make a cool plugin...

Watch Directory for new files and take action based on file type

b) uncompress if required (for RAR, ZIP, etc): - uncompress to location for viewing/streaming
a) prepare (for AVI, MKV, etc): copy or move to location for viewing/streaming
c) rename: use theRenamer or like app to rename files
d) cleanup: remove unneeded files
e) notification: send msg to notify users (email, network, sms, etc)
setup... XBMC, W7MC for DVR & Live OTA TV, JRMC for multi-zone audio, EG, MiCasaVerde Vera3, USB-UIRT IR receiver, Harmony remote, 5.2 home theater system
only one haze
Posts: 1
Joined: Sun Mar 14, 2010 5:22 am

Re: Directory Watcher - Copy Files

Post by only one haze »

hello, great script, however I have a question. How do you handle 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:

while testing i tried dropping a folder in there
for House\Season 6\File:
Python Script
Python script "1", line 3, in <module>
shutil.copy(filename,'G:\\Completed uTorrent Downloads\\')
File "shutil.pyc", line 88, in copy
File "shutil.pyc", line 52, in copyfile
IOError: [Errno 13] Permission denied: u'G:\\uTorrent Downloads\\House'
then I tried just Season 6\File and got:
Python Script
Python script "1", line 3, in <module>
shutil.copy(filename,'G:\\Completed uTorrent Downloads\\')
File "shutil.pyc", line 88, in copy
File "shutil.pyc", line 52, in copyfile
IOError: [Errno 13] Permission denied: u'G:\\uTorrent Downloads\\Season 6'
but if i just drop the File into the folder then it works great! What do I need to do to make it work in case the downloaded file is inside a folder? Thanks for your help and the great script! And of course thanks for creating this wonderful program!
kirkb
Posts: 4
Joined: Sat Feb 06, 2010 6:17 pm

Re: Directory Watcher - Copy Files

Post by kirkb »

Looks like EventGhost does not have permissions on the sub folders. Check permissions on your main torrent folder. Grant full access to "everyone" if you aren't sure. Delete and recreate the sub folders. Those should now inherit the permissions from the parent folder.

Here is my latest script:

Code: Select all

import shutil
import os

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

if IgnoreFileTypes.find(FileExtension) == -1 and isFolder == False:
    shutil.copy(FileName,'C:\\torrent\\therenamer\\')

It will NOT attempt to copy folders or file extensions in the variable IgnoreFileTypes. This way, if you get a torrent with a folder, avi, txt & nfo file (as I often do) it will only attempt to copy the avi file.

This probably wont solve your issue as you are getting a file permission error. Solve that and update your script with the one above and you should be good to go.
kirkb
Posts: 4
Joined: Sat Feb 06, 2010 6:17 pm

Re: Directory Watcher - Copy Files

Post by kirkb »

If i wasn't clear, your permission issue is on:

Permission denied: u'G:\\uTorrent Downloads\\Season 6'

Not your completed torrents folder. "Season 6" is most likely the issue permission wise.

It also looks like this may be an issue with copying the folder. My new script doesnt attempt to copy folders. I use theRenamer to organize my files and dont attempt to do that with the torrent application. I found that to be a better solution.
iamcyrus
Posts: 2
Joined: Mon Nov 01, 2010 8:54 am

Re: Directory Watcher - Copy Files

Post by iamcyrus »

The above script was very helpful to me. Thanks a lot Kirkb! However, I required it to copy folders as well. Adding these two lines to the end will allow it to also work for folders.

Code: Select all

if isFolder == True:
    shutil.copytree(FileName,os.path.join('E:\\media\\unprocessed\\',os.path.basename(FileName)))
I'm no expert with Python (or programming at all), so if there is a better/easier way, feel free to let me know.
simon
Posts: 2
Joined: Tue Nov 02, 2010 12:19 am

Re: Directory Watcher - Copy Files

Post by simon »

iamcyrus how do you apply ignore file types to the folder.

Also is there any way of moving the contents only of each folder. Just that i would like to avoid having to have an extra step to clear up all the junk files.

Great work by the way guys.
iamcyrus
Posts: 2
Joined: Mon Nov 01, 2010 8:54 am

Re: Directory Watcher - Copy Files

Post by iamcyrus »

simon wrote:iamcyrus how do you apply ignore file types to the folder.

Also is there any way of moving the contents only of each folder. Just that i would like to avoid having to have an extra step to clear up all the junk files.

Great work by the way guys.
Try this

Code: Select all

import shutil
import os

IgnoreFileTypes = 'txt,nfo'
FileName = ''.join(eg.event.payload)
FileExtension = FileName[-3:]
isFolder = os.path.isdir(FileName)
DestFolder = 'E:\\media\\unprocessed\\'

if IgnoreFileTypes.find(FileExtension) == -1 and isFolder == False:
    shutil.copy(FileName,DestFolder)
    print os.path.basename(FileName) + ' copied.'
if isFolder == True:
    for root, dirs, files in os.walk(FileName):
        for f in files:
            if IgnoreFileTypes.find(f[-3:]) == -1:
                shutil.copy(root+'\\'+f,DestFolder)
                print f + ' copied.'
Change E:\\media\\unprocessed\\ and txt,nfo to whatever you want.
simon
Posts: 2
Joined: Tue Nov 02, 2010 12:19 am

Re: Directory Watcher - Copy Files

Post by simon »

Thanks alot mate, it work brilliantly.


Had the dilemma of sample avi's. If i lose the folder structure, "The Renamer" cant delete it so have had to use "belvedere" program to delete avi etc files below a certain file size. Ahh well it also automatically deletes my torrent downloads after a certain period (giving me time to get my ratio up) so not too bad.

Could have gone the folder structure route and either used a batch file (to delete empty folders) or get belvedere to delete everything after renamer has run, but guess i prefer to play to safe incase renamer fails to rename any files.
photosis
Posts: 4
Joined: Wed Mar 16, 2011 4:19 am

Re: Directory Watcher - Copy Files

Post by photosis »

I know this is a pretty dead thread, but I was wondering if you might be able to help me with something. I was trying to modify your script so it would ignore files that said "sample" in it so it wouldn't copy them to my media folders. Here's what I added, but it doesn't work. (I know it's probably a simple mistake on my part, I'm just beginning with this)

Code: Select all

import shutil
import os

IgnoreFileTypes = 'txt,nfo'
IgnoreFileNames = 'sample'
FileName = ''.join(eg.event.payload)
FileExtension = FileName[-3:]
isFolder = os.path.isdir(FileName)
DestFolder = 'E:\\Sort\\'

if IgnoreFileTypes.find(FileExtension) == -1 and isFolder == False and IgnoreFileNames.find(FileName) == -1:
    shutil.copy(FileName,DestFolder)
    print os.path.basename(FileName) + ' copied.'
if isFolder == True:
    for root, dirs, files in os.walk(FileName):
        for f in files:
            if IgnoreFileTypes.find(f[-3:]) == -1 and IgnoreFileNames.find(FileName) == -1:
                shutil.copy(root+'\\'+f,DestFolder)
                print f + ' copied.'
Any ideas? Am I doing this backwards?
Koke
Posts: 9
Joined: Fri Jul 10, 2009 1:52 am

Re: Directory Watcher - Copy Files

Post by Koke »

If you still need this photosis, I expanded the logic a bit

Code: Select all

import shutil
import os

IgnoreFileTypes = 'txt,nfo'
IgnoreFileNames = ('sample', 'ignore')
FileName = ''.join(eg.event.payload)
isFolder = os.path.isdir(FileName)
DestFolder = 'E:\\Sort\\'


def CopyFileCheck(File):
    CopyFile = True
    for IgnoreText in IgnoreFileNames:
        if IgnoreText in File:
            CopyFile = False
    if File[-3:] in IgnoreFileTypes:
        CopyFile = False
        
    return CopyFile


if not isFolder and CopyFileCheck(FileName):
    shutil.copy(FileName, DestFolder)
    print os.path.basename(FileName) + ' copied.'
elif isFolder:
    for root, dirs, files in os.walk(FileName):
        for f in files:
            if CopyFileCheck(f):
                shutil.copy(root+'\\'+f,DestFolder)
                print f + ' copied.'
Post Reply