[R] extract file name from a path string

John McKown john.archie.mckown at gmail.com
Thu Feb 26 18:04:25 CET 2015


Look at "dirname()" and "basename()". The first would be what you call
the path. The second is the file.name without the path.

On Thu, Feb 26, 2015 at 10:58 AM, Luigi Marongiu
<marongiu.luigi at gmail.com> wrote:
> Dear all,
> what code should I write in order to extract the file name from a give
> path? Let's say that I want to get the file "my file.xls" which is in
> the directory/folder "My documents"; since I work both with Windows
> and Linux, the paths I am looking at are in the format:
>
> path.windows<-"\\home$\\lm667\\My Documents\\my file.xls"
> path.linux<-"/home/My Documents/my file.xls"
>
> I used two words for the file name because sometimes the file names
> have multiple words rather than a single one separated by capitals,
> "." or "_".
> The code should now get the file name, which is included between "\\"
> (or "/") and ".xls" but I don't know what regular expression will do
> the trick.
> Once the file name has been assigned to a vector, it should be easy to
> remove it from the path.windows/.linux and obtain a vector with the
> path on its own.
> Essentially the output should be as follows:
>
>> file.name
> [1] "my file.xls"
>> path.w
> [1] "\\home$\\lm667\\My Documents\\"
>> path.l
> [1] "/home/My Documents/"
>
> Thank you,
> Luigi
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



-- 
He's about as useful as a wax frying pan.

10 to the 12th power microphones = 1 Megaphone

Maranatha! <><
John McKown



More information about the R-help mailing list