[R] folders of path - platform independent (repost)
murdoch.duncan at gmail.com
Wed Dec 28 22:37:55 CET 2011
On 11-12-28 4:30 PM, Ben quant wrote:
> Hello, (sorry re-posting due to typo)
> I'm attempting to get the folders of a path in a robust way (platform
> independent, format independent). It has to run on Windows and Linux and
> tolerate different formats.
> For these: (The paths don't actually exist in Linux but you get the idea.)
> file_full_path = "C://Program Files//R//R-2.13.1//NEWS.pdf"
> file_full_path = "C:\Program Files\R\R-2.13.1\NEWS.pdf"
> file_full_path = "~/Program FilesR/R-2.13.1/NEWS.pdf"
> file_full_path = "/home/username/Program FilesR/R-2.13.1/NEWS.pdf"
> I would get for Windows: "C", "Program Files", "R", "R-2.13.1","NEWS.pdf"
> I would get for Linux: "home","username", "Program Files", "R",
> (The drive and/or home/username aren't necessary, but would be nice to
> have. Also, that file name isn't necessary, but would be nice.)
> Thank you for your help,
If you use the normalizePath() function with winslash="/", then all
current platforms will return a path using "/" as the separator, so you
could do something like
strsplit(normalizePath(filename, winslash="/"), "/")[]
You need to be careful with normalizePath: at least on Windows, it will
not necessarily do what you wanted if the filename doesn't exist.
More information about the R-help