0

I am trying to create a script that will move only new or updated files from the past 24 hours into a new folder. I created a script so far that will move files in general, any leads or suggestions would be greatly appreciated.

import os, shutil

source = os.listdir('C:\Users\Student\Desktop\FolderA')
destination = 'C:\Users\Student\Desktop\FolderB'

os.chdir('C:\Users\Student\Desktop\FolderA')

for files in os.listdir("C:\Users\Student\Desktop\FolderA"):
    if files.endswith(".txt"):
        src = os.path.join("C:\Users\Student\Desktop\FolderA",files)
        dst = os.path.join(destination,files)
        shutil.move(src,dst)
martineau
  • 119,623
  • 25
  • 170
  • 301
T. Nay
  • 11
  • 1
  • Here's a question that could help you get the creation dates of a file: http://stackoverflow.com/questions/237079/how-to-get-file-creation-modification-date-times-in-python With the creation dates, you can compare them to the current date. – Have_Mercy Dec 07 '16 at 20:37
  • 2
    (1) Don't repeat your hardcoded literal `FolderA` path 4 times in the same code; (2) use `os.stat(filename).st_mtime` to get the last-modified timestamp of a file. – jez Dec 07 '16 at 20:38
  • Possible duplicate of [How do I get the time a file was last modified in Python?](http://stackoverflow.com/questions/375154/how-do-i-get-the-time-a-file-was-last-modified-in-python) – kmaork Dec 07 '16 at 20:40
  • Check out the [`filecmp`](https://docs.python.org/2/library/filecmp.html#module-filecmp) module—and related [_filecmp.cmp() ignoring differing os.stat() signatures?_](http://stackoverflow.com/questions/8045564/filecmp-cmp-ignoring-differing-os-stat-signatures) question. – martineau Dec 07 '16 at 20:41

1 Answers1

0

I believe I found a solution, let me know what you guys think.

# copy files from folder_a to folder_b
# if the files in folder_a have been modified within the past 24 hours
#   copy them to folder_b
#


import shutil
import os
from os import path
import datetime
from datetime import date, time, timedelta


def file_has_changed(fname):
#  print 'in file_has_changed with file : %s' % fname
#  print str(path.getmtime(fname))

# get file modified time
  file_m_time = datetime.datetime.fromtimestamp(path.getmtime(fname))

#  print datetime.datetime.now()
#  print file_m_time

  #get the delta between today and filed mod time
  td = datetime.datetime.now() - file_m_time

#  print td
#  print 'days : %d' % td.days

 # file can be archived if mod within last 24 hours
  if td.days == 0:
    global ready_to_archive
    ready_to_archive = ready_to_archive + 1
    return True
  else: return False



def main():
  global ready_to_archive
  global archived
  ready_to_archive, archived = 0, 0

  # src = "c:\users\gail\desktop\foldera"
  # dst = "c:\users\gail\desktop\folderb"

  for fname in os.listdir('c:\users\gail\Desktop\FolderA'):

    src_fname = 'c:\users\gail\Desktop\FolderA\%s' % fname

    if file_has_changed(src_fname):    
      dst_fname = 'c:\users\gail\Desktop\FolderB\%s' % fname
      dst_folder = 'c:\users\gail\Desktop\FolderB'


      try:
        shutil.copy2(src_fname, dst_folder)
        global archived;
        archived = archived + 1
      #  print 'Copying file : %s ' % (src_fname)
      #  print '      To loc : %s ' % (dst_fname)
      except IOError as e:
        print 'could not open the file: %s ' % e



if __name__ == "__main__":

  main()

  print '******   Archive Report for %s   ******' % datetime.datetime.now()
  print '%d files ready for archiving ' % ready_to_archive
  print '%d files archived' % archived
  print '******   End of Archive Report   ******'
T. Nay
  • 11
  • 1