Hi, 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 ? - thanks in advance Antony. -- View this message in context: http://r.789695.n4.nabble.com/Get-the-filename-from-the-given-path-tp4640578.html Sent from the R help mailing list archive at Nabble.com.
Hello, basename(FPath) Hope this helps, Rui Barradas Em 17-08-2012 10:28, Rantony escreveu:> Hi, > > 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 ? > > - thanks in advance > Antony. > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Get-the-filename-from-the-given-path-tp4640578.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
On 17.08.2012 11:28, Rantony wrote:> Hi, > > 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 ?basename(FPath) Uwe Ligges> - thanks in advance > Antony. > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Get-the-filename-from-the-given-path-tp4640578.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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. >
Thanks pramod. From: Pramod [via R] [mailto:ml-node+s789695n4640600h38@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=YW50b255LmFra2FyYUBnZS5jb218NDY0MDU3OHwxNTUx 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]]
Do splits <- strsplit(filePath,"file://%22,fixed=true)[[1/]] fileName <- splits[length(splits)]) [[alternative HTML version deleted]]
On Fri, Aug 17, 2012 at 5:28 AM, Rantony <antony.akkara at ge.com> wrote:> Hi, > > 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 ? >Quite possibly by yelling at it.... or regular expressions: you know, whatever> - thanks in advance > Antony. > > > > -- > View this message in context: http://r.789695.n4.nabble.com/Get-the-filename-from-the-given-path-tp4640578.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > 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.
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.
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.
On 17.08.2012 14:02, Michael Meyer wrote:> Do > > splits <- strsplit(filePath,"file://%22,fixed=true)[[1/]] > fileName <- splits[length(splits)])And what is your question now? If this is an answer, and we really appreciate people who try to help, please a) cite the question and b) provide answers that at least work for you. Yours has way too many errors or typos. Best, Uwe Ligges> [[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. >