Borrar filtros
Borrar filtros

How to check if file name has a certain ending?

42 visualizaciones (últimos 30 días)
Aaron
Aaron el 9 de Ag. de 2014
Comentada: Stelios Fanourakis el 30 de Abr. de 2018
In the current folder are some files, which do or don't have a filename extension. I would like to check, whether they already have the filename extension or not and add the extension, if necessary.
How do I check the filename extension?
  2 comentarios
Azzi Abdelmalek
Azzi Abdelmalek el 9 de Ag. de 2014
Do you mean a specific extension?
Aaron
Aaron el 9 de Ag. de 2014
No, in general, although the extension in my case would be ".mat".

Iniciar sesión para comentar.

Respuesta aceptada

Image Analyst
Image Analyst el 9 de Ag. de 2014
Editada: Image Analyst el 9 de Ag. de 2014
Try this:
myFolder = 'C:\whatever';
if ~isdir(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
uiwait(warndlg(errorMessage));
return;
end
% Look for all files and add extension of .m if they are missing extension.
filePattern = fullfile(myFolder, '*.*');
allFiles = dir(filePattern);
for k = 1 : length(allFiles)
baseFileName = allFiles(k).name;
oldFullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', oldFullFileName);
[folder, baseFileName, extension] = fileparts(baseFileName);
if isempty(extension)
% No extension, so add a .m extension
newBaseFileName = sprintf('%s.m', baseFileName);
newFullFileName = fullfile(myFolder, newBaseFileName);
movefile(oldFullFileName, newFullFileName);
fprintf(1, 'Renamed %s to %s\n', oldFullFileName, newFullFileName);
end
end
  2 comentarios
Aaron
Aaron el 9 de Ag. de 2014
Thanks!
Stelios Fanourakis
Stelios Fanourakis el 30 de Abr. de 2018
Very helpful answer. Thanks a lot!

Iniciar sesión para comentar.

Más respuestas (2)

dpb
dpb el 9 de Ag. de 2014
Editada: dpb el 9 de Ag. de 2014
dNoExt=dir('*.');
Returns all files with no extension in CWD in the dir structure dNoExt. Process as
for i=1:length(dNoExt)
if dNoExt(i).isdir, continue, end % skip directory entries
movefile(dNoExt(i).name,[dNoExt(i).name '.yourextension']);
end
Or, one can cleanup the returned directory entries first before the loop as
dNoExt=dNoExt(~[dNoExt.isdir]); % retain only files in structure
  4 comentarios
Aaron
Aaron el 9 de Ag. de 2014
Thanks!
dpb
dpb el 9 de Ag. de 2014
Note the simplification of eliminating the directory entries above if still interested...

Iniciar sesión para comentar.


Azzi Abdelmalek
Azzi Abdelmalek el 9 de Ag. de 2014
d=dir('*.*')
e={d.name}'
f=e(~cellfun(@isdir,e))
ii=regexp(f,'\.+','match')
jj=find(cellfun(@isempty ,ii))
g1=f(jj)
g2=strcat(g1,'.ext')
cellfun(@(x,y) movefile(x,y),g1,g2)
  1 comentario
Aaron
Aaron el 9 de Ag. de 2014
I didn't try your code, because the other answers here seem a bit easier to me. But thank you for your answer!

Iniciar sesión para comentar.

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by