[R] Get the filename from the given path

William Dunlap wdunlap at tibco.com
Fri Aug 17 17:41:32 CEST 2012


Q.
>> Here i have a file path, for eg : - FPath <- "D:\\MyFolder\\MyFile.txt"
>> HOW IS POSSIBLE TO GET THE FILE NAME FROM THIS GIVEN PATH ?
A.
> x <- substr(FPath,13,nchar(FPath)) should give you the desired output.

But this is much like saying
   x <- "MyFile.txt"
will give you the desired output - where did the 13 come from?

Use basename() for a general solution.
  > basename(FPath)
  [1] "MyFile.txt"
dirname(FPath) gives you the directory that FPath is in in.

(To other responders, those who don't know that all caps means shouting
probably don't know that the answer '?basename' means to type that into
R to look at the help file for the basename function.)

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] On Behalf
> Of Rantony
> Sent: Friday, August 17, 2012 4:56 AM
> To: r-help at r-project.org
> Subject: Re: [R] Get the filename from the given path
> 
> Thanks pramod.
> 
> 
> 
> From: Pramod [via R] [mailto:ml-node+s789695n4640600h38 at n4.nabble.com]
> Sent: Friday, August 17, 2012 5:24 PM
> To: Akkara, Antony (GE Energy, Non-GE)
> Subject: Re: Get the filename from the given path
> 
> 
> 
> x <- substr(FPath,13,nchar(FPath)) should give you the desired output.
> 
> ________________________________
> 
> If you reply to this email, your message will be added to the discussion
> below:
> 
> http://r.789695.n4.nabble.com/Get-the-filename-from-the-given-path-tp464
> 0578p4640600.html
> 
> To unsubscribe from Get the filename from the given path, click here
> <http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=unsubscrib
> e_by_code&node=4640578&code=YW50b255LmFra2FyYUBnZS5jb218NDY0MDU3OHwx
> NTUx
> OTQzMDI5> .
> NAML
> <http://r.789695.n4.nabble.com/template/NamlServlet.jtp?macro=macro_view
> er&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.Bas
> icNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.tem
> plate.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml
> -instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemai
> l.naml>
> 
> 
> 
> 
> 
> --
> View this message in context: http://r.789695.n4.nabble.com/Get-the-filename-from-
> the-given-path-tp4640578p4640601.html
> Sent from the R help mailing list archive at Nabble.com.
> 	[[alternative HTML version deleted]]
> 
> ______________________________________________
> R-help at r-project.org mailing list
> 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.




More information about the R-help mailing list