Announcement

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

  • Obtain the timestamp of a file

    I am seeking guidance about how to fetch the timestamp of a file from the file system. Searches within this forum or the Web have not proved fruitful for me and I came up with a solution a couple years ago but I think there must be a better way to do this. I am currently using 64-bit Stata IC 15.1 on Windows 10.

    Use case example: in some cases, I'll check the timestamp of a CSV file, import the CSV only if it is newer than my DTA file, or skip the import step and use my existing DTA if the CSV is not newer.

    For an example, I'll use a file found within BASE: arch.ado:
    Code:
    dir "C:\Program Files\Stata15\ado\base\a\arch.ado"
    returns
    58.2k 2/23/19 20:56 arch.ado

    My current solution:
    Code:
    tempfile dirCmdResult
    shell dir "`=subinstr("`c(sysdir_base)'", "/", "\", .)'a\arch.ado" > `dirCmdResult'
    import delimited `dirCmdResult', clear delimiters(tab)
    gen double timestamp = clock(v1, "MDY hm # #") // my Windows Regional Format is US mm/dd/yyyy hh:mm AP
    quietly: summarize timestamp
    local filesystemtimestamp `r(max)'
    
    di %tc `filesystemtimestamp'
    correctly returns
    23feb2019 20:56:00

    But I'm assuming there must be a far simpler way to do this. Any advice will be appreciated.

    Thank you,
    Marc Peterson

  • #2
    I've not tried this, but I imagine that you should be able to slim this down at least to


    Code:
    tempfile dirCmdResult
    shell dir "`=subinstr("`c(sysdir_base)'", "/", "\", .)'a\arch.ado" > `dirCmdResult'
    import delimited `dirCmdResult', clear delimiters(tab)
    di %tc clock(v1, "MDY hm # #")
    exploiting the fact that display shows the value in the first observation even if given a variable name.

    Comment


    • #3
      You should be able to achieve this using my shell wrapper -inshell- (available on the SSC) in two lines without creating a dataset. Though I don't have a Windows machine to test this, if:
      Code:
      inshell dir "`=subinstr("`c(sysdir_base)'", "/", "\", .)'a\arch.ado"
      results in:
      58.2k 2/23/19 20:56 arch.ado
      then
      Code:
      local timestamp =clock("`:word 2 of `r(no1)'' `:word 3 of `r(no1)''", "MD20Y hm")
      display %tc `timestamp'
      23feb2019 20:56:00

      Comment


      • #4
        Thank you, both!

        I guess my first take-away is that there doesn't appear to be any Stata or Mata commands that get to this information more directly.

        Regarding Nick's suggestion, I only needed to make sure that the timestamp line was in the first row after import, so each of these revisions also work:
        Code:
        tempfile dirCmdResult
        shell dir "`=subinstr("`c(sysdir_base)'", "/", "\", .)'a\arch.ado" >> `dirCmdResult'
        import delimited `dirCmdResult', clear delimiters(tab) rowrange(5)
        local filesystemtimestamp = clock(v1, "MDY hm # #")
        di %tc `filesystemtimestamp'
        Code:
        tempfile dirCmdResult
        shell dir "`=subinstr("`c(sysdir_base)'", "/", "\", .)'a\arch.ado" > `dirCmdResult'
        import delimited `dirCmdResult', clear delimiters(tab)
        keep if regexm(v1, " [AP]M ")
        di %tc clock(v1, "MDY hm # #")
        Code:
        tempfile dirCmdResult
        shell dir "`=subinstr("`c(sysdir_base)'", "/", "\", .)'a\arch.ado" | findstr /r "[AP]M" > `dirCmdResult'
        import delimited `dirCmdResult', clear delimiters(tab)
        di %tc clock(v1, "MDY hm # #")

        Regarding Matthew's suggestion, I grabbed the -insheet- package and have been playing with it. This looks very useful for some of the work I do.

        Thanks again,
        Marc

        Comment

        Working...
        X