Copy files based on presence in list

9 views (last 30 days)
EstherJ
EstherJ on 31 May 2022
Answered: Jan on 31 May 2022
Hi all,
I am trying to copy specific files from different subfolders to a new folder. I managed to make a list of all the files I want to copy using the dir function, so I have a struct with the file names and folders. However, there are many duplicates in this list that I need to remove first. How can I do this?
After removing the duplicates, how can I copy the files in my list to a new folder? So I want Matlab to loop over all the subfolders and copy the files that are named in the struct. Hope you can help me with this!
Best,
Esther

Answers (2)

Image Analyst
Image Analyst on 31 May 2022
If it's a duplicate, the destination file will be there. In that case, just skip that file. Something like
topLevelFolder = 'C:\wherever you want';
% Define the "new" destination folder.
destinationFolder = 'C:\new folder';
% Make sure it's empty.
existingFiles = dir(fullfile(destinationFolder, '*.*'))
if length(existingFiles) >= 3
% It's not an empty folder. There are files in it.
fprintf('%s already exists. I am expecting an empty folder');
return;
end
% Create destination folder if it does not exist yet.
if ~isfolder(destinationFolder)
mkdir(destinationFolder);
end
% Get a list of all files under the top level folder.
filePattern = fullfile(topLevelFolder, '**\*.*');
fileList = dir(filePattern);
% Loop over source files copying them to the destination folder.
for k = 1 : length(fileList)
sourceFileName = fullfile(fileList(k).folder, fileList(k).name);
destinationFileName = fullfile(destinationFolder, fileList(k).name);
if isfile(destinationFileName)
% File has already been copied over, so this name must be a duplicate.
continue; % Skip to the next file.
else
% File does not exist yet so it's the first time seeing this source file name.
copyfile(sourceFileName, destinationFileName);
end
end

Jan
Jan on 31 May 2022
What exactly are "duplicates"? Which of them should be used? What does "removing" mean: from the list or delete the files?
List = dir('D:\Your\Folder\**\*.jpg'); % Recursive search
Name = {List.name};
[~, index] = unique(Name); % Not repetitions in the names
List = List(index);
for k = 1:numel(List)
SourceFile = fullfile(List(k).folder, List(k).name);
DestFolder = 'D:\IdoNotKnow';
copyfile(SourceFile, DestFolder);
end

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!