[R] Get the filename from the given path

William Dunlap wdunlap at tibco.com
Fri Aug 17 23:46:05 CEST 2012


Yes, basename(filepath) assumes that filepath represents
path to a file on the OS that R is running on.  Hence, on
Windows we get
  > basename("dir\\path")
  [1] "path"
  > basename("dir/path")
  [1] "path"
because both / and \ are valid separators (and invalid in file names)

Linux allows \ in a file name so we get
  > basename("dir\\file")
  [1] "dir\\file"
  > basename("dir/file")
  [1] "file"

The same goes for dirname():
  Windows> dirname("c:/file") # "c:" is not  a directory but "c:/" is
  [1] "c:/"
  Linux> dirname("c:/file")
  [1] "c:"
 
Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com


> -----Original Message-----
> From: arun [mailto:smartpink111 at yahoo.com]
> Sent: Friday, August 17, 2012 1:20 PM
> To: William Dunlap
> Cc: Rantony; R help
> Subject: Re: [R] Get the filename from the given path
> 
> 
> 
> Hi,
> 
> I guess basename() output depends on the system.
>  basename(getwd())
> #[1] "BehaviorStudy"
> 
> So, if I wanted to get the filename from the FPPath, I have to replace "\\" to "//".
> FPath <- "D:\\MyFolder\\MyFile.txt"
>  basename(FPath)
> #[1] "D:\\MyFolder\\MyFile.txt"
>  FPath1<-"D://MyFolder//MyFile.txt"
>  basename(FPath1)
> #[1] "MyFile.txt"
> gsub(".*\\\\.*\\\\(.*)","\\1",FPath)
> #[1] "MyFile.txt"
>  gsub(".*\\//.*\\//(.*)","\\1",FPath1)
> #[1] "MyFile.txt"
> A.K.
> 
> 
> 
> 
> ----- Original Message -----
> From: William Dunlap <wdunlap at tibco.com>
> To: Rantony <antony.akkara at ge.com>; "r-help at r-project.org" <r-help at r-project.org>
> Cc:
> Sent: Friday, August 17, 2012 11:41 AM
> Subject: Re: [R] Get the filename from the given path
> 
> 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.
> 
> ______________________________________________
> 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