Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Storing File Names AND Date/Time Created To Sort Them

    Hi all,

    I have used .filelist (SSC) to pull in names of files in folders and then to sort through them for various purposes.

    I have a related problem with files of the same or similar names in files that have different "Dates/Times Created" values.

    For example I might have:
    "file x", created 11/2/16;
    "file x (1)", created 11/4/16, and
    "file x (2)", created 11/8/16.

    In this case it is "file x (2)" that I want to retrieve, because it is the most recent.

    Similarly I might have the following:

    "file y", created 11/8/16 11:40:00 AM;
    "file y (1)", created 11/8/16 10:20:00 AM, and
    "file y (2)", created 11/2/16.

    And it would be "file y" that I want to retrieve, even the the dates are the same between it and "file y (1)", but the time is more recent.

    So, the hope would be that I could retrieve the most recent file of any set with the file name "file x*" and "file y*".

    I can manage building the code to sort through the names but do not know how to/if I can pull in creation dates/times of files to do the same with those.

    Does anyone have any ideas?

    Thanks, in advance, for any advice you might have.

    Best,

    Ben Hoen
    Berkeley Lab

  • #2
    Hi Ben,

    It looks like dirlist might be the way to go.

    Looking at the help file, one could write:

    Code:
    cd "C:\mydir"
    dirlist "*myfile*"
    
    // set an initial minimum date very high
    local min_date = 999999999999
    
    // loop over all files and find the newest
    forvalues i = 1/`r(nfiles)' {
        local date : word `i' of `r(fdates)'
        local times : word `i' of `r(ftimes)'
        
        // mess around with these locals until you have a yyyymmddHHMM
        
        local yyyymmddHHMM = some date
        
        // if this local is the smallest, then keep that value
        if `yyyymmddHHMM' < `min_date' {
            local min_file : word 2 of `r(fnames)'
            local min_date `yyyymmddHHMM'
        }
    
    }
    display("`min_file'")
    Best,
    Eric
    Last edited by Eric Haavind-Berman; 28 Nov 2016, 16:33.

    Comment


    • #3
      Eric,

      This is perfect. Once I get something working with it i will post that into this thread.

      Thanks!

      Ben

      Comment

      Working...
      X