Python Forum
need to define date range of files to copy over
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
need to define date range of files to copy over
#1
Hi,

This is my first post so really sorry if I have done this incorrectly.

I have added raw_input that lets the user choose file extension.
The next criteria I am looking to add is a date range so they can choose a date range for example:

17/07/2020 to 04/08/2020 or Today() like in excel

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

os.chdir('C://')
src = ("")
dst = ("")
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: ")

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)
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 so really sorry about poor coding but I am trying to learn so any help would be amazing.

Thanks
Reply
#2
solved my own issue thanks
Reply
#3
why do you import raw_input from pip._vendor.distlib.compat? You should be using python3 and input to take user input.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#4
Thank you I have changed the code so it is only using input, I just read up about raw_input and been using it for a couple of weeks, will need to go through all my codes and replace it.

here is the code I have.

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")
Can i ask whether it is possible to add a progress bar to my code? if so how would I go about it?
Reply
#5
tqdm package
example by @ snippsat: https://python-forum.io/Thread-tqdm-Prog...mmand-line

progressbar2 package

There are other options too (e.g. click.progressbar, part of click - but this will be overkill if not using click for CLI interface).
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy and Paste Files - Command MicheliBarcello 2 863 Jun-25-2024, 05:04 AM
Last Post: rodiongork
  Invalid Date Format fo Cached Files jland47 1 963 May-22-2024, 07:04 PM
Last Post: deanhystad
  Why is the copy method name in python list copy and not `__copy__`? YouHoGeon 2 1,287 Apr-04-2024, 01:18 AM
Last Post: YouHoGeon
  Compare current date on calendar with date format file name Fioravanti 1 1,874 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  Copy Paste excel files based on the first letters of the file name Viento 2 1,510 Feb-07-2024, 12:24 PM
Last Post: Viento
  Python date format changes to date & time 1418 4 2,512 Jan-20-2024, 04:45 AM
Last Post: 1418
  Downloading time zone aware files, getting wrong files(by date))s tester_V 9 2,732 Jul-23-2023, 08:32 AM
Last Post: deanhystad
  Create new folders and copy files cocobolli 3 4,186 Mar-22-2023, 10:23 AM
Last Post: Gribouillis
  Copy only hidden files and folders with rsync Cannondale 2 2,236 Mar-04-2023, 02:48 PM
Last Post: Cannondale
  Copying files if the name is in range tester_V 9 3,016 Nov-24-2022, 12:21 AM
Last Post: tester_V

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020