extract file name from a path string

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

extract file name from a path string

Luigi
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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: extract file name from a path string

John McKown
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
<[hidden email]> 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
>
> ______________________________________________
> [hidden email] 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

______________________________________________
[hidden email] 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.
Reply | Threaded
Open this post in threaded view
|

Re: extract file name from a path string

Shenglin Chen
This post has NOT been accepted by the mailing list yet.
In reply to this post by Luigi
Try this: path<-"\\home$\\lm667\\My Documents\\my file.xls" filename<-sub(".*\\\\(.*)$","\\1\\",path,perl=TRUE) path.w<-sub("(\\\\.*\\\\).*$","\\1\\",path,perl=TRUE)
Luigi 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 ______________________________________________ [hidden email] 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.