Extract files with the same names in subfolders and save them into a new folder with a different name

I have .png files with the same name stored in different folders. My goal is to rename and copy them into a new folder. The following code just saves the last image in the new folder. I am not able to figure out how to save PNG files with incremental values in name. I would appreciate any help. Thanks in advance.
myFolder = 'current folder path';
filePattern = fullfile(myFolder, '**/*.png');
theFiles = dir(filePattern);
numFiles = length(theFiles);
outputFolder = 'new folder path';
for K = 1 : numFiles
baseFileName = theFiles(K).name;
fullFileName = fullfile(theFiles(K).folder,baseFileName);
limages = imread(fullFileName);
fullFileName = fullfile(outputFolder, theFiles(K).name);
imwrite(limages,fullFileName);
end

2 Comments

Is there any character that is valid in file names, that we are permitted to assume does not occur in the file names in question? For example are we permitted to assume that the original filenames will never end with underscore and then a number just before the extension?
When the files get renamed as they are copied into the new directory, is it necessary to identify which directory each version came from? For example cracked/20220311.png -> 20220311_cracked.png ?
@Walter Roberson Thank you for your response. There are no such requirements. I just want to retrieve and store all the files in a single folder. A number before the file extension with underscore is also fine. My files in subfolders are named as output_img.png.

Sign in to comment.

 Accepted Answer

%%
myFolder = 'current folder path';
filePattern = fullfile(myFolder,'**','*.png');
theFiles = dir(filePattern);
numFiles = length(theFiles);
outputFolder = 'new folder path';
mkdir( outputFolder )
for K = 1 : numFiles
source_file = fullfile( theFiles(K).folder, theFiles(K).name );
target_name = sprintf( '%s_%03d.png', theFiles(K).name, K ); % less than 999 files
target_file = fullfile( outputFolder, target_name );
copyfile( source_file, target_file );
end
And some error handling and testing.

7 Comments

(This code would preserve the original name but add _ and then a 3+ digit number at the end. The number is certain to be unique, so duplicate names cannot happen. Should work well.)
@per isakson @Walter Roberson Thank you very much for the solution. The code works exactly as I wanted.
I'm not seeing how it finds "files with the same name stored in different folders". The code looks like it just copies all images to a new folder. I would think you'd need to get all the base file names, and the corresponding folder names where they live. Then sort all the base file names and go down the list. If one base file name is the same as the prior name, then it exists in a different folder. Then you could copy one or both to a new folder.
A simple way to honor the requirement, "files with the same name stored in different folders", is to replace the statement
filePattern = fullfile(myFolder,'**','*.png');
by
filePattern = fullfile(myFolder,'**','the_same_name.png');
I am sorry actully I missed seeing the names. The above code saves files as 'same name.png_3 digit.png' basically it is saved as 'output_img.png_001.png'. The 3 digit numbers are concatinated at the end after the extention and again I have .png extention.
Instead of this:
source_file = fullfile( theFiles(K).folder, theFiles(K).name );
target_name = sprintf( '%s_%03d.png', theFiles(K).name, K ); % less than 999 files
try this
% Create source full file name.
baseFileName = theFiles(K).name;
inputFolder = theFiles(K).folder;
sourceFullFileName = fullfile(inputFolder, baseFileName);
% Create full destination file name.
[~, baseFileNameNoExt, ext] = fileparts(baseFileName);
targetBaseFileName = sprintf('%s_%03d.png', baseFileNameNoExt, K); % less than 999 files
targetFullFileName = fullfile(outputfolder, targetbaseFileName);
% Copy the source file, with it's new name, to the destination folder.
copyfile(sourceFullFileName, targetFullFileName);

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB Coder in Help Center and File Exchange

Tags

Asked:

on 14 Apr 2022

Commented:

on 20 Apr 2022

Community Treasure Hunt

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

Start Hunting!