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.
PhyrePhox
Posts: 1
Joined: Tue Apr 05, 2011 7:15 pm

Re: Directory Watcher - Copy Files

Post by PhyrePhox »

Well, I don't know about the original posters, but I have found this thread very useful. My scenario is: incoming torrents are TV shows, to be renamed according to certain criteria (using TheRenamer) and move to a shared drive. Incoming files are either:
1. loose avi files
2. folders containing avi/mkv files and other files that are unwanted
3. folders containing segmented RAR archives of avi /mkv and other unwanted files

So, my configuration tree is:
Image

An my modified version of the script is:

Code: Select all

import shutil
import os

IgnoreFileTypes = 'txt,nfo,pnr,!ut,sfv,rar,r00,r01,r02,r03,r04,r05,r06,r07,r08,r09,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19,r20,r21,r22,r23,r24,r25,r26,r27,r28,r29,r30'
IgnoreFileNames = ('sample', 'ignore')
FileName = ''.join(eg.event.payload)
isFolder = os.path.isdir(FileName)
DestFolder = 'D:\\Torrent\\TheRenamer\\'


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.'
As you can see, ParNRar gets called e every time the directory is updated, and it is set to close when it's finished extracting, so it pops up and disappears a lot. Not ideal, but usable.

I want it to exclude all the RAR segments, but the way I'm using is inelegant.

The only *problem* is, when the downloaded torrent is an AVI/MKV in a folder, the video file never gets copied.

It still beats the way I was doing it last week, but can anyone point me in the right direction here? Thanks!
Koke
Posts: 9
Joined: Fri Jul 10, 2009 1:52 am

Re: Directory Watcher - Copy Files

Post by Koke »

I ended up making my own script as I don't want to copy everything to the same folder for various complicated reasons but you should be able to mix and match the best from both scripts. I did some testing but couldn't recreate your issue with video files in a folder not copying over so not sure what's going on there... shutil.copy isn't popping up an error? I'd start by making sure the correct info is being passed to shutil.copy and trying the command manually and working from there. You can PM me if you want to try some things.

A white/black list file check if you only want to deal with a few file types:

Code: Select all

whiteList = ('.avi', '.mp4', '.mkv', '.divx')
blackList = ('\\temp\\', 'sample', '.!ut')

def FileCheck(filename):
    isWhite = False
    isBlack = False
    for text in whiteList:
        if text in filename:
            isWhite = True
            break
    for text in blackList:
        if text in filename:
            isBlack = True
            break
    return isWhite and not isBlack
To stop ParNRar getting called every time you want to stop the macro from continuing to run if no work is done (doesn't pass the file check). Here's a check for that:

Code: Select all

WorkDone = False
if not isFolder and CopyFileCheck(FileName):
    shutil.copy(FileName, DestFolder)
    WorkDone = True
elif isFolder:
    for root, dirs, files in os.walk(FileName):
        for f in files:
            if CopyFileCheck(f):
                shutil.copy(root+'\\'+f,DestFolder)
                WorkDone = True
if not WorkDone:
    eg.StopMacro()
spazlon
Posts: 14
Joined: Fri Jun 17, 2011 6:43 pm
Location: Mangaf, Kuwait
Contact:

Re: Directory Watcher - Copy Files

Post by spazlon »

This looks very close to what I am looking for.

I currently have a "Downloads" folder that has "New Movies", "New Shows", etc. Inside those folders I have video files and subfolders with RARs or video files. I made a script in VBScript to automatically copy any new video files and unrar any new RARs to another folder. The problem is that if I have a video in a subfolder it doesn't move it.

Using python, how can I copy video files (avi, mkv, mp4) from D:\Downloads\New Shows\ (or any sub directory of D:\Downloads\) to D:\Media\New Shows\ (or whatever the source subfolder of D:\Downloads\ was) and copy video files from subfolders (like D:\Downloads\New Shows\New Show Name\ShowName.avi) to the downloads folder (D:\Media\New Shows\ShowName.avi) and unRAR files using the same folder structure listed earlier?

I know this was really long and drawn out, but this has been bugging me for a really long time now.

Thank you for any help!

- Ryan
kicks66
Posts: 3
Joined: Fri Sep 30, 2011 11:21 am

Re: Directory Watcher - Copy Files

Post by kicks66 »

Sorry to ressurrect a bit of an old thread but:

I want Eventghost to watch a folder.

If a folder is downloaded into the directory, I want it to copy it to DirectoryA

If a file is downloaded, I want it to copy it to DirectoryB

Is there a way I can get Eventghost to sepereate folders and files like this?

Also, if a folder is downloaded, can I get it to copy just the files inside the folder, and not the whole folder itself?
themaster1
Experienced User
Posts: 133
Joined: Wed Feb 03, 2010 9:02 pm

Re: Directory Watcher - Copy Files

Post by themaster1 »

Hi guys,
I just want to copy a file from a folder to two sub folders whenever there is a change (file edited), someone give me a hand, these python scripts always confuses me.

file name: myfile.ini
main folder: folder
sub folders: folder 1 & 2
Koke
Posts: 9
Joined: Fri Jul 10, 2009 1:52 am

Re: Directory Watcher - Copy Files

Post by Koke »

If you're just worried about the one file here's how I'd do it:

Code: Select all

import shutil

fileName = 'myfile.ini'
workingFolder = 'D:\\temp\\'
destinationFolders = ['folder1', 'folder2']

if fileName in eg.event.payload[0].lower():
	for folder in destinationFolders:
		shutil.copy(workingFolder + fileName, workingFolder + folder)
pyto00
Posts: 1
Joined: Mon Apr 06, 2015 8:49 am

Re: Directory Watcher - Copy Files

Post by pyto00 »

I have added an extra function, it came handy to me and I would like to share it.

So after it copied a file from a given location, it also deletes the source file and/or folder tree. you can see this as a cleanup function.

Code: Select all

import shutil
import os
from os import path



IgnoreFileTypes = 'txt,nfo,!ut,sfv,rar,r00,r01,r02,r03,r04,r05,r06,r07,r08,r09,r10,r11,r12,r13,r14,r15,r16,r17,r18,r19,r20,r21,r22,r23,r24,r25,r26,r27,r28,r29,r30'
IgnoreFileNames = ('sample', 'ignore')
FileName = ''.join(eg.event.payload)
FileExtension = FileName[-3:]
isFolder = os.path.isdir(FileName)
DestFolder = 'G:\\Moving\\SERIES\\'
FilePath = path.abspath(eg.event.payload[0])
eg.globals.miscPath = FilePath


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.'
    os.remove(FileName)
    print os.path.basename(FileName) + ' deleted '
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.'
                shutil.rmtree(FilePath)
                print os.path.basename(FileName) + ' deleted. '
charisma_charisma
Posts: 9
Joined: Sat Oct 24, 2015 7:26 am

Re: Directory Watcher - Copy Files

Post by charisma_charisma »

I've tried google-ing for it and browsing this forum, but no success - does anybody have a script which moves files from folder A to folder B (I've found out how to do this) BUT stops doing so in case of duplicates (if it finds out that a certain file from folder A already is in folder B) ?
The idea is to sync my phone's photos folder with a folder on my PC using Google Drive (folder A) and a folder from which Google Photos desktop app backs photos up (folder B).
But the problem is that this is an infinite process, bcs when pics are downloaded to folder A and then moved to folder B (and uploaded to Photos folder in Drive), they're once again downloaded in folder A; and then again and again..
Is there a script which would recognize that a pic has already been moved to folder B and would stop moving files until a new pic shows up?
eirik226
Experienced User
Posts: 142
Joined: Wed Nov 07, 2012 5:22 pm

Re: Directory Watcher - Copy Files

Post by eirik226 »

Edit: nvm
Post Reply