MATLAB Versions 1 through 13

7 views (last 30 days)
Chris Conant
Chris Conant on 28 Feb 2017
Edited: Walter Roberson on 28 Feb 2017
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
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.
Chris Conant
Chris Conant on 28 Feb 2017
Edited: Walter Roberson on 28 Feb 2017
Jan - for some reason the DOS rename didn't seem to change the letter case. rename (star).M (star).m still puked on the script when trying to execute it. Doesn't matter. This worked: dir (star).(star) /s > mathworks.txt
Open that in notepad, and then copy to Excel. After some magic - a column of commands show up:
cd('F:\MathWorks\MATLAB')
movefile BODEEX.M bodeex.m_bad
movefile bodeex.m_bad bodeex.m
movefile EXMODEL3.M exmodel3.m_bad
movefile exmodel3.m_bad exmodel3.m
movefile F2VXFN.M f2vxfn.m_bad
movefile f2vxfn.m_bad f2vxfn.m
movefile OPENH.M openh.m_bad
movefile openh.m_bad openh.m
movefile REPLOT.M replot.m_bad
movefile replot.m_bad replot.m
movefile SAMPLE.M sample.m_bad
movefile sample.m_bad sample.m
Copy and paste those cells into an m-file. Amazingly enough - all 5736 files converted in a heartbeat.
Thanks for your response.

Sign in to comment.

Accepted Answer

Jan
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
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
Chris Conant
Chris Conant on 28 Feb 2017
Thanks. Work arounds are always available. I got around it - see my response to Jan. Thank you all for your responses.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!