rename files using MATLAB
显示 更早的评论
I need to rename my subfolders and files in those subfolders in an ascending order (subfolders are named time002, time003,... & files inside are named slice001time002, slice002time002,...). I need to change time002 to time001, time003 to time003, and so on. I have the base of the code but I am not sure how to go about changing it.
% Specify the folder where the files live.
myFolder = 'C:\Users\Ambika\Desktop\Images';
% 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\nPlease specify a new folder.', myFolder);
uiwait(warndlg(errorMessage));
myFolder = uigetdir();
if myFolder == 0
return;
end
end
% Get a list of all files in the folder, and its subfolders, with the desired file name pattern.
filePattern = fullfile(myFolder, '/time%3.3d/slice%3.3dtime%3.3d.png');
theFiles = dir(filePattern);
for k = 1 : length(theFiles)
baseFileName = theFiles(k).name;
fullFileName = fullfile(theFiles(k).folder, baseFileName);
fprintf(1, '/time%3.3d/slice%3.3dtime%3.3d.png', fullFileName);
imageArray = imread(fullFileName);
imshow(imageArray);
drawnow;
end
回答(1 个)
This cannot work:
filePattern = fullfile(myFolder, '/time%3.3d/slice%3.3dtime%3.3d.png');
theFiles = dir(filePattern);
The file pattern contains a pattern for the creation of strings, but this does not find matching file names.
this is failing also:
fprintf(1, '/time%3.3d/slice%3.3dtime%3.3d.png', fullFileName)
The pattern expects 3 nuimerical values, but fullFileName is a char vector. A valid command would be:
fprintf(1, '%s', fullFileName)
How are the original files stored? The shown code does not perform any renaming and does not work at all. What does this mean: "change time003 to time003"?
What are the original names and what have to be changed to what?
9 个评论
Ambika Bhardawaj
2022-2-4
Allmost there: If you want to change "time002" to "time001", should the existing "time001" become "time000"?
Then:
base = '\your\base\folder';
for k = 1:44 % Adjust manually
origName = sprintf('time%3d', k);
newName = sprintf('time%3d', k - 1);
origFolder = fullfile(base, origName);
newFolder = fullfile(base, newName);
movefile(origFolder, newFolder, 'f');
list = dir(fullfile(newName, '*.png'));
for kk = 1:numel(list)
origFileName = list(kk).name;
newFileName = strrep(origFileName, origName, newName);
movefile(fullfile(newFolder, origFileName), ...
fullfile(newFolder, newFileName));
end
end
As usual: Create a backup of the data before running the code.
Ambika Bhardawaj
2022-2-6
Ambika Bhardawaj
2022-2-6
Image Analyst
2022-2-6
Before you actually do the renaming, I STRONGLY suggest you verify the filenames first with fprintf:
fprintf('Renaming\n %s\nto\n %s\n', fullfile(a, fileNames{ iFile }), newName );
% movefile( fullfile(a, fileNames{ iFile }), newName );
That way, if a mistkae is made you don't have to "undo" the naming.
Ambika Bhardawaj
2022-2-6
Image Analyst
2022-2-6
Again, explain "I need to change time002 to time001, time003 to time003, and so on."
So I read this as saying if the time is 00n (like 002) to decrement n so that the name is 00(n-1), (like 001), EXCEPT if the number is 3. If it's 3, do not rename it and leave it as 3.
Is that they way you want it?
Ambika Bhardawaj
2022-2-6
编辑:Ambika Bhardawaj
2022-2-6
Jan
2022-2-7
@Ambika Bhardawaj: "Hi so I need to change 001 to 095 (the last file basically)" - adding such details after a longer discussion is a shot in your knee. If you explain this directly, the users of the forum do not waste their and your time with posting solutions, which do not match your problem.
I cannot run my suggested code, because I do not have your data. So it is your turn to use the debugger to check, what's going on. Set a breakpoint in the line:
newFileName = strrep(origFileName, origName, newName);
and find out, if it creates the names as expected or if there is a problem. Use my code as point to start from to create, what you exactly need.
类别
在 帮助中心 和 File Exchange 中查找有关 File Operations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

