Announcement

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

  • Identifying most recent .dta file in a directory

    Hi,
    Is there any way to identify the most recently modified .dta file in a given directory?

    Alternatively, I was thinking of simply displaying the content of the directory, then to manually enter the name of the file to be used. But even that, I wouldn't know how to do! How can I key in a macro?
    Thanks a lot in advance for your help!
    TG
    Last edited by Thierry Geiger; 27 May 2014, 10:41.

  • #2
    Thierry,

    This sounds like it should be easy, but I can't find any easy way to do this. There is a brute force method that might work, but which may require some fixing depending on what operating system you are using:

    Code:
    capture log close
    log using dir.log, replace
    dir
    log close
    infix str size 1-8 str datetime 10-23 str name 25-50 using dir.log, clear
    gen double datetime1=clock(datetime,"MD20Yhm")
    format datetime1 %tc
    gsort -datetime1
    list
    Note that if you have files created before 1/1/2000 you will have to modify the clock() function accordingly. Also note that dir.log will probably be the first file in the list, so you will have to account for that (perhaps by looking at the second observation or by saving dir.log to a different directory). Finally, you may want to exclude directories by getting rid of observations with size containing "<dir>".

    I don't understand your alternate method. If you know the name of the file already why do you need to know the most recently modified file? And why would you need a macro?

    Regards,
    Joe

    P.S. Depending on what operating system you are using you can also do the following, which may yield better results:

    Code:
    shell dir > dir.lst    // Or "ls" instead of "dir" if using Linux (& Mac?)
    infix str datetime 1-20 using dir.lst, clear
    gen double datetime1=clock(datetime,"MDYhm")
    etc...

    Comment


    • #3
      A belated big thank you for your solution, Joe. It worked perfectly!
      TG

      Comment

      Working...
      X