Reputation: 53
I would like to ask for your help regarding renaming a list of image files inside a directory and then put some other image files in that directory. For example, I have directory, Dir1, in which I have a list of images, as follows:
Dir1 = [Image-001.jpg, Image-002.jpg, Image-003.jpg,...
Image-004.jpg, Image-005.jpg, Image-006.jpg,...
Image-007.jpg, Image-008.jpg, Image-009.jpg,...
Image-010.jpg, Image-011.jpg, Image-012.jpg];
I divided the list of images in two sub-directories, Dir2 and Dir3. Dir3 contains every 3rd images of Dir1; and Dir2 contains rest of the images.
Dir2 = [Image-001.jpg, Image-002.jpg, Image-004.jpg,...
Image-005.jpg, Image-007.jpg, Image-008.jpg,...
Image-010.jpg, Image-011.jpg];
Dir3 = [Image-003.jpg, Image-006.jpg, Image-009.jpg, Image-012.jpg];
Now, after some processing on the images of Dir2 and Dir3, the filenames of Dir2 and Dir3 becomes:
Dir2 = [Image-001.jpg, Image-002.jpg, Image-003.jpg,...
Image-004.jpg, Image-005.jpg, Image-006.jpg,...
Image-007.jpg, Image-008.jpg];
Dir3 = [Image-001.jpg, Image-002.jpg, Image-003.jpg,...
Image-004.jpg];
Can anyone please tell me how can I merge the files of Dir2 and Dir3 and go back to the filename-pattern of Dir1; that is, I have to rename Image-001.jpg of Dir3 to Image-003.jpg and put it on the 3rd position of Dir2 where Image-003.jpg is located; and Image-003.jpg and rest of the images in Dir2 will be renamed accordingly. Same will happen for Image-002 for Dir3 and so on.
Please ask me if I couldn't explain the problem clearly. Any help would be really appreciated. Any sample code in Matlab or C++ would be a great help.
Upvotes: 0
Views: 491
Reputation: 114786
You should use movefile
and copyfile
to do the job.
For example, on Dir3 you can do
for ii=1:4,
dir3filename = fullfile( 'Dir3', sprintf('Image-%03d.jpg', ii ) );
dir1filename = fullfile( 'Dir1', sprintf('Image-%03d.jpg', 3*ii) ); % new name
copyfile( dir3filename, dir1filename );
end
Note that dir3filename
and dir1filename
are relative paths, the code will only work if the relative paths are correct with respect to the current working directory. You may change the code to use absolute paths.
You may also want to look at fullfile
and string formatting.
Upvotes: 4