0

I have a folder that has many DWG files and an example of some files might be

LINCOLN 09-17 #1-29H ASBUILT 10-26-2010.dwg
LINCOLN 09-17 #1-29H FINAL 01-20-2011.dwg
CAMPBELL 07-17 #5-29H DRAFT 01-20-2011.dwg
CAMPBELL 07-17 #5-29H FINAL 01-27-2011.dwg
CAMPBELL 07-17 #5-29H FINAL 01-27-2011_1.dwg

I would need to capture (maybe in a list?) only the dwg files that have FINAL and are the latest modified dwg file for that file name. So the files above that would be captured are

LINCOLN 09-17 #1-29 FINAL 01-20-2011.dwg
CAMPBELL 07-17 #5-29H FINAL 01-27-2011_1.dwg

Any suggestions?

Moderator, I cannot seem to add comments to the questions below. Why is that??

Here is what I have so far.. I placed comments where I think I need changes

import os 

rootdir='c:\Your\Path'

for subdir, dirs, files in os.walk(rootdir):    
    for file in files:

    # I need to check only files with the same name so I can 
    # get the latest of those files before checking next files      

        if os.path.getmtime(file):
            # I believe this will get time file was modified but
            # but how do I store this to compare to other files
            # with this name??
Josh
  • 5
  • 3
  • 3
    Search with searching Stack Overflow for related questions. Then, after reading about `os.walk`, post your code and the problems you're having. Example http://stackoverflow.com/questions/120656/directory-listing-in-python – S.Lott May 17 '11 at 18:18
  • Does this need to be python? This strikes me as being a hell of a lot easier in bash. – Daenyth May 17 '11 at 19:07
  • @Daenyth: From the looks of things he's on Windows (i.e.: the backslashes in rootpath) – initzero May 17 '11 at 20:48
  • You need at least 10 [reputation points](http://stackoverflow.com/faq#reputation) to post comments. – Oben Sonne May 18 '11 at 17:49

1 Answers1

0

You need to go searching for the suitable files, and remeber the newest of each one as you come accross it. Then report the latest.

In pseudo-code.

dates = dict() // key is filename, value is date
for  subdir,dirs, files in os.walk(startdir):
if filename ends with "dwg":
     if filename contains "FINAL":
          date = getDate from filename 
          if dates contains filename
              prev = dates(filename)
          else 
              prev = zero date // a value so next line tests true always
          if date > prev:
             dates(filename) = date
          // else previous is younger, so skip
    // else draft or as built and skip
// else filename not dwg and skip
for dates.values as v
    print v
Ian
  • 1,941
  • 2
  • 20
  • 35