MATLAB Versions 1 through 13
7 views (last 30 days)
Show older comments
Back in the old days with DOS, PC-MATLAB, AT-MATLAB, 386 MATLAB and early versions for Windows (MATLAB 4, MATLAB 5, etc.) nobody could care less whether the .m extension is UPPER CASE or lower case. Versions after 2010 (or so) do. Anybody have a clue how to "rename" over 6500 m-files in one easy swoop?
C:\> rename *.M *.m doesn't seem to work to well in the Windows "DOS" prompt. :)
Case sensitive variables are great. Case sensitive m-files? Not so much. Picky, picky, picky.
Thanks.
3 Comments
Jan
on 28 Feb 2017
What exactly does "doesn't seem to work well" mean? The command works well and I hesitate now to suggest it again, but I cannot guess, what's the problem.
Accepted Answer
Jan
on 28 Feb 2017
Edited: Jan
on 28 Feb 2017
You can use one of the many recursive dir() versions from the FileExchange, perhaps FEX:15859-subdir--a-recursive-file-search:
FileList = subdir('C:\YourBasePath\*.M');
for iFile = 1:numel(FileList)
newName = [FileList{iFile}(1:end-2), '.m'];
movefile(FileList{iFile}, newName);
end
But there are smarter tools for doing this directly, e.g. BulkRename or AF5 Rename your files. (Note: Bother tools can be downloaded free of charge for personal use, but there are professional licenses also.)
[EDITED] Thanks Guillaume:
Since Matlab R2016b and an alternative using fileparts:
FileList = dir('C:\YourBasePath\**\*.M');
for iFile = 1:numel(FileList)
folder = FileList{iFile}.folder;
name_ext = FileList{iFile}.name;
oldFile = fullfile(folder, name_ext);
[dummy, name] = fileparts(name_ext);
newFile = fullfile(folder, [name, '.m']);
movefile(oldFile, newFile);
end
Hm. A leaner version:
FileList = dir('C:\YourBasePath\**\*.M');
FileName = fullfile({FileList.folder}, {FileList.name});
for iFile = 1:numel(FileName)
newName = [FileName{iFile}(1:end-2), '.m'];
movefile(FileName{iFile}, newName);
end
3 Comments
Walter Roberson
on 28 Feb 2017
If you are using MS Windows with FAT* then I gather that it is not possible to turn off case *in*sensitivity at the file system level. If you are using MS Windows with NTFS then I gather that it is possible to turn off case *in*sensitivity at the file system level, but not often done.
When case *in*sensitivity is on at the filesystem level, then if you attempt to movefile() a file to a name that is the same but a different case combination, then movefile() will consider the source and destination to be the same and will leave them unchanged. Therefore on such systems, you need to movefile() to a name that is not a case insensitive match for any existing file, and the movefile() to the desired name.
You might want to use tempname() to pick a temporary file name that is not in use.
Remember to use a bunch of try/catch and success tests in case one of the movefiles fails
More Answers (0)
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!