0

'''I am currently trying to copy files from one folder to another folder using shutil but I can't seem to get it to work, the process is saying it has finished but nothing happens?'''

The current criteria I have added raw_input that lets the user choose file extension. The next criteria I am looking to add is a date range function so I can choose a date range for example: 17/07/2020 to 04/08/2020 or the day's date.

*UPDATED CODE

import os
import shutil
import os.path, time
from pip._vendor.distlib.compat import raw_input

os.chdir('C://')
src = ("C:/Users/eldri/OneDrive/Desktop/")
dst = ("C:/Users/eldri/OneDrive/Desktop/output")
ext = raw_input("[+] File format: ")
created = (" last modified: %s" % time.ctime(os.path.getmtime(src)))
start = raw_input("[+] Date start: ")
end = raw_input("[+] Date end: ")

def date_to_num(date):
    return int("".join(date.split('/')[::-1]))

def date_in_range(date, start, end):
    return date_to_num(date) > date_to_num(start) and date_to_num(date) < date_to_num(end)

for filename in os.listdir(src):
    if filename.endswith('.'+ext) and created.startswith(start) and created.endswith(end):
        shutil.copy( src + filename, dst)
        print("[+] File transferred "+filename + created)
    else:
        print("[+] File not transferred "+filename + created)
        
print("[+] Transfer complete")

I was looking at maybe pandas? but not sure as still quite new to python.

example on terminal
file extension = .csv
startdate = 12/05/2020
enddate = 07/08/2020

once the user has input these fields it would copy only the required files over.

The current output of the created files are:

[+] File transferred BASE1011.xls last modified: Fri Jul 17 10:11:40 2020
[+] File transferred BASE1112.xls last modified: Fri Jul 17 10:11:40 2020
[+] File transferred BASE1213.xls last modified: Fri Jul 17 10:11:40 2020
[+] File transferred BASE1314.xls last modified: Fri Jul 17 10:11:40 2020
[+] File transferred BASE1415.xls last modified: Fri Jul 17 10:11:40 2020

I want these to be in an easier format for user input as explained above: example: start 12/05/2020 end date = 07/08/2020

Thank you for your help, I am not the best at python but I am trying to learn so any help would be amazing.

Thanks

Ooo
  • 39
  • 9
  • 1
    you might find this answer useful for what you are trying to achieve https://stackoverflow.com/a/39501288/13906433 – David sherriff Aug 05 '20 at 08:48
  • Thank you @Davidsherriff can I ask whether there is a way to only cp files created/ modified between two dates? I have updated my question# – Ooo Aug 06 '20 at 13:43

2 Answers2

1

For specific range you can:

create a function that parse the date to a number:

def date_to_num(date):
    return int("".join(date.split('/')[::-1]))

def date_in_range(date, start, end):
    return date_to_num(date) > date_to_num(start) and date_to_num(date) < date_to_num(end)

And then use it like that:

date_in_range("03/02/2020", "01/01/2020", "05/05/2020")
Or Y
  • 2,088
  • 3
  • 16
  • Hi, Thank you so much for your help. Really sorry but I am still really new to python, I have updated the code in the question, is that correct? I have tried it and the files are not transferring still. [+] File format: csv [+] Date start: 06/07/2020 [+] Date end: 06/08/2020 – Ooo Aug 06 '20 at 14:45
  • 1
    @Ooo No problems :), Can you attach a code or an example so I can understand what didn't work? – Or Y Aug 06 '20 at 15:01
  • I have updated the questions code to add in the def date_to_num and def date_in_range. ''' [+] File format: csv [+] Date start: 06/08/2020 [+] Date end: 06/08/2020 [+] File not transferred 02002.csv last modified: Thu Aug 6 15:26:24 2020 ''' The code works if I do not put a date but when I put a date range in it doesn't work. – Ooo Aug 06 '20 at 15:14
  • 1
    @Ooo you've added my function but didn't call them anywhere :/ You need to call the `date_in_range` funcion with your date and range and it should work – Or Y Aug 06 '20 at 15:16
  • I have also just realised that created = (" last modified: %s" % time.ctime(os.path.getmtime(src))) does not work. – Ooo Aug 06 '20 at 15:28
1

I've carried on your work using time.ctime(os.path.getmtime(src)) and created a function dateRange(createdDate, startDate, endDate) that uses datetime to convert the strings into datetime objects and returns True or False if the created date falls between start and end dates

import os
import shutil
import time
from datetime import datetime

src = "C:/Users/eldri/OneDrive/Desktop/"
dst = "C:/Users/eldri/OneDrive/Desktop/output"
ext = input("[+] File format: ")  # "txt"
start = input("[+] Date start: ")  # "01/07/2020"
end = input("[+] Date end: ")  # "30/07/2020"


def dateRange(createdDate, startDate, endDate):
    """determines if date is in range"""
    createdDate = datetime.strptime(createdDate, '%a %b %d %H:%M:%S %Y')
    startDate = datetime.strptime(startDate, '%d/%m/%Y')
    endDate = datetime.strptime(endDate, '%d/%m/%Y')
    return startDate < createdDate < endDate


for filename in os.listdir(src):
    created = time.ctime(os.path.getmtime(src + filename))
    if filename.endswith('.' + ext) and dateRange(created, start, end):
        shutil.copy(src + filename, dst)
        print("[+] File transferred " + filename + created)
    else:
        print("[+] File not transferred " + filename + created)

print("[+] Transfer complete")

I've added examples at the end of the ext, start, and end variables to provide an idea of the format

Ooo
  • 39
  • 9
David sherriff
  • 466
  • 1
  • 3
  • 9
  • Thank you so much for your help, I have copied the code over and it run perfectly, I am going to research the date time as never used it before. So i can use input rather then raw_input? is that better then raw_input? also can I ask when you get time can you have a look at my other post please if you don't mind. [link] https://stackoverflow.com/questions/63157777/new-to-python-excel-password-cracker-in-python [link] – Ooo Aug 06 '20 at 16:10
  • 1
    You're welcome. `input` is a built in python method, no need to import anything, it will return a string. If you needed a integer input for example you can use `number = int(input("please enter a integer: ")` Yes I will have a look at your other post :) – David sherriff Aug 06 '20 at 16:25
  • Thank you so much for for your help, I will have a few test codes now using the number = int(input("please enter a integer: "). Excellent thank you, been struggling for a while, seem to be very hit and miss with getting codes write and then I follow the rabbit and then get screwed lol – Ooo Aug 06 '20 at 17:33
  • Can i ask whether you know how to add a progress bar to this at all? I tried progressbar = tqdm(os.listdir(src)) for item in progressbar: time.sleep(0.010) pass but it only counts the files in the folder and measures time it takes to count them, not how long it takes to transfer? – Ooo Aug 11 '20 at 16:28
  • @Ooo I might have an idea to get an estimate.... Let me get back to you – David sherriff Aug 11 '20 at 18:48
  • @Ooo please refer to your other question, hopefully that will help you get started – David sherriff Aug 12 '20 at 09:25