Dan
Dan

Reputation: 519

Python - if directory contains file beginning with...move file

I need to create a script in Python which will look to a directory (which contains only one file) and move it if the file is not for the current day. For reference the file has a suffix which relates to the current day (myfile_030811.xls)

Does anyone have any ideas for this?

Upvotes: 0

Views: 1580

Answers (2)

Aneesh
Aneesh

Reputation: 934

try this? This assumes that a directory called 'archived' is present under the current directory. you might want to tweak it to suit your needs. Also this assumes that all files under your directory have a name of the structure _ddmmyy. format. It won't work otherwise

from stat import *
import os
import time
import shutil
import sys

for file in os.listdir(sys.argv[1]):
  ct = time.localtime()
  datestamp_on_filename = file.split('_')[1].split('.')[0]
  current_date_in_ddmmyy = str(ct.tm_mday) + (('0' + str(ct.tm_mon)) if ct.tm_mon < 10 else str(ct.tm_mon)) + str(ct.tm_year)[2:]
  if datestamp_on_filename != current_date_in_ddmmyy:
    print 'moving ' + file
    shutil.move(sys.argv[1] + "/" + file, 'archived')

Upvotes: 2

Stephen
Stephen

Reputation: 3432

How about bash?

Test it with :

for m in `find /some/base/dir -mtime 1`;do echo mv $m /new/directory;done

If everything looks good, remove the "echo" command in front of mv.

Upvotes: 0

Related Questions