How to move/copy files from folder and subfolders to a new folder?
Show older comments
Hello,
I am trying to move/copy files from a main folder with multiple subfolders to a new folder.
The code below runs but I receive this error when I use this is in the loop...
for k = 1:numel(FilesName):
...
Error using movefile
No matching files were found.
Here is my attempt...
clear all, close all,
% Folders/subfolders where files are located
% I want to move only the .png files located under D:\2015\
myDir = dir('D:\2015\**\**\*.png');
% New destination folder
destFolder = 'D:\NewFolder';
%Cell array with file names
FilesName = {myDir.name};
for k = 1:numel(FilesName)
sourceFile = fullfile(myDir(k).folder, myDir(k).name);
movefile(sourceFile, destFolder);
end
whos
Name Size Bytes Class Attributes
FilesName 1x5022 1051326 cell
destFolder 1x12 24 char
k 1x1 8 double
myDir 5022x1 4280856 struct
However, when I use... for k = 1:3:numel(FilesName) the code does what I want (move all .png files under folder D:\2015\...
My question is why myDir above is repeating the same file name 3 times? I believe that is because when k=2 cannot find the file as it was already moved. Then, I am obligated to skip every 3 files. Maybe I am using the wildcards in the wrong context here?
Your comments are welcome.
4 Comments
dpb
on 26 Feb 2020
Well, it's not clear why you use
%Cell array with file names
FilesName = {myDir.name};
at all when as your whos output shows it simply duplicates all the entries that are in the dir() struct array myDir. So, more straightforward would be to write the loop index upper limit as numel(myDir). They're the same so that's not the issue; just there's no point in creating another whole array of the same information you already have.
As for the "why" Q? of there being three copies of the same file apparently -- that's going to be the result of something you've done previously, probably accidentally.
Your dir() call is over a wildcard string of two levels of subdirectories so it's going to return everything it finds there. Apparently there are multiple copies of the same file--that wouldn't be too surprising to have happened given a nested directory structure.
You'll just have to go spelunking and see what directory and file are actually returned but dir() will only return something that is there that matches the wildcard specification.
It would be less confusing probably to traverse the directory structure one subdirectory at a time instead.
Mario
on 26 Feb 2020
dpb
on 26 Feb 2020
Well, that would indicate that's how many files there are in toto, then -- at least with that wildcard pattern.
Have you exhaustively verified you have the subdirectories wanted and only the subdirectories wanted?
I forget who wrote it but at least one of the other regulars here has written examples of recursive subdirectory traversing...do a search by subject for the phrase. There may also be (and almost certainly is) submissions at the File Exchange.
Mario
on 28 Feb 2020
Accepted Answer
More Answers (0)
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!