One of such cases just happened a few minutes ago. The aforementioned Garmin Forerunner 405 can cooperate quite nicely with Linux using the gant reader for the ant wireless communication protocol between the usb hardware dongle and the Garmin 405. (Sources for gant are both this file and this git archive.) I had meant to blog about this tool and the resulting files one of these days anyway, but today I just want to mention that the default filenames created by the program were somewhat horrid such as 20.09.2009 101112.TCX to denote the 20th of September of this year at 10:11h and 12 seconds. As we all know, filenames with spaces are bad for the environment as well as plain annoying. So I had made the simple change in the C sources to switch to a saner format such as 20090920-101112.TCX (and I see that the git archive now contains a similar fix). But that still left me with some 80+ files with the dreaded names.
There are of course many ways to skin this cat and to rename the files in bulk. However, I found the following four lines to be fairly succinct
#!/usr/bin/r files <- dir(".", pattern=".*\\.TCX$") res <- lapply(files, function(f) { pt <- strptime(f, "%d.%m.%Y %H%M%S.TCX") # parsed time ft <- strftime(pt, "%Y%m%d-%H%M%S.TCX") # formatted time file.rename(f, ft) })as they show, among other things,
Lastly, I do not mean to imply that Python or Perl or Ruby or (insert favourite tool here) cannot do it equally well. I simply meant to say that programmatically creating new filenames is definitely easier in R than it would have been in shell. And as an added bonus, we even get fully parsed time objects that I could have tested for. But then tests and documentation never get written on a Saturday.