2

I'd like to update the modification time of some files in Nim, without actually modifying them, like Unix touch. I see there is an os.getLastModificationTime function, or more broadly os.getFileInfo, but I do not see corresponding set functions.

How do you touch a file in Nim? (If platform matters, I am currently working on Windows, sadly.)

Keith Pinson
  • 7,835
  • 7
  • 61
  • 104

2 Answers2

4

Unfortunately that is system specific in the current Nim version 0.18.0. On posix you'd do:

import posix
utimes("filename", nil)

In the next release you can use the platform independent os.setLastModificationTime: https://github.com/nim-lang/Nim/pull/7543

def-
  • 5,275
  • 20
  • 18
  • 1
    Excellent, thanks. Do you happen to know what the plan is for a release date of this version? Also do you know if the best way to get a prerelease build to build it myself? – Keith Pinson Aug 21 '18 at 14:55
  • 1
    Don't know when, you can use an older version of Nim to compile it yourself: https://github.com/nim-lang/nim#compiling – def- Aug 21 '18 at 17:03
  • 1
    Another option is of course to use choosenim. With that utility you can easily switch between versions, including the latest development version. Simply run `choosenim devel` to switch to your devel branch, or `choosenim update devel` to compile the newest source on GitHub and switch to that. – PMunch Aug 23 '18 at 07:37
4

Piggybacking on def-'s answer, here is a complete example showing how you can update file modification time to "now", of file passed as first argument:

import os
import times

let file = commandLineParams()[0]
setLastModificationTime(file, now().toTime())

Behind the scenes (with Nim 0.19) on posix systems this delegates to posix.utimes() and will changes the 3 file timestamps for access, modify, change. On Windows the "behind the scenes" details will differ, reading source code I think it will only change "last write time", not "last access time".

Hugues M.
  • 19,846
  • 6
  • 37
  • 65