Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Splitting a file name into name,extension

I have the name of a file like this: name1.csv and I would like to extract two substrings of this string. One that stores the name1 in one variable and other that stores the extension, csv, without the dot in another variable.

I have been searching if there is a function like indexOf of Java that allows to do that kind of manipulation, but I have not found anything at all.

Any help?

like image 519
Layla Avatar asked Jan 05 '13 16:01

Layla


People also ask

How do you split a file name and extension in Python?

We can use Python os module splitext() function to get the file extension. This function splits the file path into a tuple having two values - root and extension.

What separates the name of the file and extension?

Windows file names have two parts separated by a period: first, the file name, and second, a three- or four-character extension that defines the file type. In expenses. xlsx, for example, the first part of the file name is expenses and the extension is xlsx.

How do I add an extension to a file name?

Step 2: Right-click the file name and select Rename (or left-click and hold down the button for one second). Enter the new extension like so: After entering the new extension hit the Enter (return) key.

Which method is used to split filename into its component parts?

The fileparse function can be used to extract the extension. To do so, pass fileparse the path to decipher and a regular expression that matches the extension. You must give fileparse this pattern because an extension isn't necessarily dot-separated.


2 Answers

Use strsplit:

R> strsplit("name1.csv", "\\.")[[1]] [1] "name1" "csv"   R>  

Note that you a) need to escape the dot (as it is a metacharacter for regular expressions) and b) deal with the fact that strsplit() returns a list of which typically only the first element is of interest.

A more general solution involves regular expressions where you can extract the matches.

For the special case of filenames you also have:

R> library(tools)   # unless already loaded, comes with base R R> file_ext("name1.csv") [1] "csv" R>  

and

R> file_path_sans_ext("name1.csv") [1] "name1" R>  

as these are such a common tasks (cf basename in shell etc).

like image 188
Dirk Eddelbuettel Avatar answered Sep 28 '22 11:09

Dirk Eddelbuettel


Use strsplit():

http://stat.ethz.ch/R-manual/R-devel/library/base/html/strsplit.html

Example:

> strsplit('name1.csv', '[.]')[[1]] [1] "name1" "csv"   

Note that second argument is a regular expression, that's why you can't just pass single dot (it will be interpreted as "any character").

like image 24
Adam Stelmaszczyk Avatar answered Sep 28 '22 13:09

Adam Stelmaszczyk