Dear all, The basename() function returns the extension also:> myfile <- "path1/path2/myoutput.txt" > basename(myfile)[1] "myoutput.txt" Is there any other function where it just returns plain base: "myoutput" i.e. without 'txt' - Gundala Viswanath Jakarta - Indonesia
You can use 'sub' to get rid of the extensions:> sub("^([^.]*).*", "\\1", 'filename.extension')[1] "filename"> sub("^([^.]*).*", "\\1", 'filename.extension.and.more')[1] "filename"> sub("^([^.]*).*", "\\1", 'filename without extension')[1] "filename without extension" On Thu, Jan 8, 2009 at 9:10 PM, Gundala Viswanath <gundalav at gmail.com> wrote:> Dear all, > > The basename() function returns the extension also: > >> myfile <- "path1/path2/myoutput.txt" >> basename(myfile) > [1] "myoutput.txt" > > > Is there any other function where it just returns > plain base: > > "myoutput" > > i.e. without 'txt' > > - Gundala Viswanath > Jakarta - Indonesia > > ______________________________________________ > 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. >-- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve?
Try this also: substr(basename(myfile), 1, nchar(basename(myfile)) - 4) On Fri, Jan 9, 2009 at 12:10 AM, Gundala Viswanath <gundalav@gmail.com>wrote:> Dear all, > > The basename() function returns the extension also: > > > myfile <- "path1/path2/myoutput.txt" > > basename(myfile) > [1] "myoutput.txt" > > > Is there any other function where it just returns > plain base: > > "myoutput" > > i.e. without 'txt' > > - Gundala Viswanath > Jakarta - Indonesia > > ______________________________________________ > R-help@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. >-- Henrique Dallazuanna Curitiba-Paraná-Brasil 25° 25' 40" S 49° 16' 22" O [[alternative HTML version deleted]]
On 1/8/2009 9:10 PM, Gundala Viswanath wrote:> Dear all, > > The basename() function returns the extension also: > >> myfile <- "path1/path2/myoutput.txt" >> basename(myfile) > [1] "myoutput.txt" > > > Is there any other function where it just returns > plain base: > > "myoutput" > > i.e. without 'txt'I'm curious about something: does "file extension" have a standard definition? Most (all? I haven't tried them all) of the solutions presented in this thread would return an empty string for the "plain base" if given the filename ".bashrc". Windows (where file extensions really mean something), though reluctant to create such a file, appears to agree that the extension is bashrc, even though to me it appears clear that that file has no extension. Duncan Murdoch
The S+ basename() function has an argument called suffix and it will remove the suffix from the result. This was based on the Unix basename command, but I missed the special case in the Unix basename that doesn't remove the suffix if the removal would result in an empty string. The suffix must include any initial dot. Unlike the Unix basename, the suffix is a regular expression (but other regexpr arguments like ignore.case and fixed are not acceptedi - there ought to be a regular expression class so these things get attached to the expression instead of added to the call). > basename(c("foobar", "dir/foo.bar"), suffix=".bar") [1] "fo" "foo" > basename(c("foobar", "dir/foo.bar"), suffix="\\.bar") [1] "foobar" "foo" Bill Dunlap TIBCO Software Inc - Spotfire Division wdunlap tibco.com