#!/usr/bin/env python3

# python3.7 ../scripts/move_from_subdirs -src fastq -dst raw -ext *.fastq.gz
#
# A function that generates files that match a given filename pattern and move these files to another directory

import argparse
import os
import shutil
import fnmatch

parser = argparse.ArgumentParser()
parser.add_argument('-src', help='SOURCE folder containing the desired files in subdirectories.', required=True)
parser.add_argument('-dst', help='DESTINATION folder of the desired files.', required=True)
parser.add_argument('-ext', help='EXTENSION of the files to be moved.', required=True)
args = parser.parse_args()

src = args.src
dst = args.dst
ext = args.ext

def gen_find(filepat,top):
    for path, dirlist, filelist in os.walk(top):
        for name in fnmatch.filter(filelist,filepat):
            yield os.path.join(path,name)

# Example use

if __name__ == '__main__':
    #src = 'O:\DATA\ASTER GDEM\Original\North America\UTM Zone 14\USA\Extracted' # input
    #dst = 'O:\DATA\ASTER GDEM\Original\North America\UTM Zone 14\USA\Analyses' # desired     location

    filesToMove = gen_find(ext ,src)
    for name in filesToMove:
        shutil.move(name, dst)
