1
votes

I have some excel files with file extension of .xls or .xlsx. I want remove this file extensions from end of file names. Some of these files are.xls and others are.xlsx, So first I want find type of file excretion (.xls and .xlsx - for other processings in code) and after that remove that extension.

How can I do this with regular expression?

Thanks.

1
in what format is your list of file names? Did it come from dir? If so any reason you can't specify *.xls*? - Dan
But also Matlab has the fileparts which will help here instead of regex - Dan
@Dan File names are in a list box in GUI with both extensions. When I read it, the file name is a string. - Eghbal
And in what format were you planning on using regex on them? On individual strings? A cell array of strings? How were you going to extract the names from the GUI list box (or probably more relevant is how they are getting into that list box?). You really need to put much more detail in this question, some code would help a lot. - Dan
There isn't any problem in reading file names from list-box. Suppose that I have single string for this problem. We can expand answer to other cases. - Eghbal

1 Answers

5
votes

I'd just use the fileparts() function.

[sPath, sFilename, sExt] = fileparts( sFile );
sFileBase = [ sPath, sFilename];

As a hint for your method, you can do open fileparts and look at the source code.

Let's try:

[sFiles, sPath] = uigetfile({'*.xls;*.xlsx','Select Excel files'; ...
                             '*.*', 'Select any file'}, ...
                            'MultiSelect', 'on');

and select 'Boolk1.xls', 'Book1.xlsx' and 'Book1.txt'. sFiles is a cell array of strings, so we can do this:

for i = 1:length(sFiles)
    [~, sFilename, sExt] = fileparts( sFiles{i} );
    if( strcmpi( sExt, '.xls' ) )
        fprintf( '%s%s is a .xls file.\n', sFilename, sExt );
    elseif( strcmpi( sExt, '.xlsx' ) )
        fprintf( '%s%s is a .xlsx file.\n', sFilename, sExt );
    else
        fprintf( '%s%s is neither a .xls nor a .xlsx file.\n', sFilename, sExt );
    end
end

the result is:

Book1.txt is neither a .xls nor a .xlsx file.
Book1.xls is a .xls file.
Book1.xlsx is a .xlsx file.

(Matlab is easy!)