MATLAB Answers


Renaming batch names removing extensions

Christopher Hamm さんによって質問されました 2019 年 6 月 14 日
最新アクティビティ dpb
さんによって コメントされました 2019 年 6 月 17 日
さんの 回答が採用されました
Hello, I have multiple file names that were accidently labeled with a '.' in them, and my other scripts will no longer recognize the file name correctly. I need to remove the '.' fromt he file names in large batches, about 400 files at a time in a folder.
For example, I need a way to make a batch like this:
into a batch with just the "." removed like this:
I am new to matlab and have no idea how to accomplish this, any and all help would be much appreciated.
Thank you!

  0 件のコメント

サインイン to comment.


2 件の回答

回答者: dpb
2019 年 6 月 14 日
編集済み: dpb
2019 年 6 月 15 日

Job for command. Make a .cmd batch file from the following--name DORENAME.CMD or somesuch...
echo off
for %f in (1801*.25*) do (
set filename=%~xf%
set newname=%filename:.=-%
ren %f% %newname%
echo on
Execute the above for each directory containing files...I built in a matching wildcard pattern to your example--if all the files in the subdirectory are affected, then *.* would work just as well. If there are differing names you could pass the appropriate wild card as a parameter and use it instead of hardcoded value.
Each filename %f% returned from the directory search is save in temporary environment variable and then the new name built by character substitution of the hyphen for the period/dot. Then those are used to RENAME the file.
You could write similar logic in ML using the DIR() structure but there isn't a direct support to the REN command built into ML so you have to COPYFILE then delete the old one which is an extra step.

  3 件のコメント

I agree with dpb this is a job better done outside matlab. There are also plenty of batch rename utilities available to download that would completely remove the need to write any code.
Christopher Hamm 2019 年 6 月 17 日
dpb was correct, it was much easier to use a bulk utility renamins software that was free. Had to do each file partition seperately, but it was easy and relatively painless. Thank you!
2019 年 6 月 17 日
Yeah...I just gave a rudimentary .CMD file instead of looking to find an available utility.
I use the JPSoftware command line replacement for the MS CMD shell which has all these kinds of features built into a much more powerful and easier to use command syntax so have all the tools at hand...

サインイン to comment.

Image Analyst
回答者: Image Analyst
2019 年 6 月 14 日

Try this
% Specify the folder where the files live.
myFolder = 'C:\Users\yourUserName\Documents\My Pictures';
% Check to make sure that folder actually exists. Warn user if it doesn't.
if ~isfolder(myFolder)
errorMessage = sprintf('Error: The following folder does not exist:\n%s', myFolder);
% Get a list of all files in the folder with the desired file name pattern.
filePattern = fullfile(myFolder, '*.*'); % Change to whatever pattern you need.
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
originalFullFileName = fullfile(myFolder, baseFileName);
% If there is no dot, skip it.
if ~contains(baseFileName, '.')
continue; % Skip to bottom of loop.
% If it gets to here, there is a dot in the name.
% Remove it
newBaseFileName = baseFileName; % Initialize
newBaseFileName(baseFileName == '.') = [];
newFullFileName = fullfile(myFolder, newBaseFileName);
fprintf(1, 'Now renaming %s to %s\n', originalFullFileName, newFullFileName);
movefile(originalFullFileName, newFullFileName); % Do the rename.
You might also want to do all folder and subfolders all in one shot, instead of a folder at a time, using fileDatastore.

  0 件のコメント

サインイン to comment.

Translated by