copyfile skips files?

13 views (last 30 days)
kova
kova on 18 Apr 2020
Edited: dpb on 20 Apr 2020
Hello everyone!
I want to create copies of specific files in a new directory. They are coming out of over 500 different subfolder, so I don't want to do it manually.
I have loaded and accessed data within all of them, so I know they exist. For some reason, not all files make it to the destination folder, but I'm not getting an error message. I also have plenty of space on my drive, so that can't be the problem either.
ddir = uigetdir;
AllFiles = dir(fullfile(ddir, '**', 'DT_data*mat'));
destination = 'C:\Users\XXXXX\Desktop\tempFigData';
for i = 1:length(AllFiles)
sourcefile = fullfile(AllFiles(i).folder, AllFiles(i).name);
copyfile(sourcefile, destination);
end
Does anyone have an idea what could be the problem here?
  3 Comments
kova
kova on 18 Apr 2020
Thank you!
I'm sorry if I'm not giving the info you need, I'm very much a novice! ;-)
I will definitely start adding status messages to my scripts!
In this case however, it didn't return anything. Just to check, I tried it the other way round, without negating status, (which did return a message) and made it count for how many files this was the case. It counted 515 (the number I want), while the destination directory actually only contains 508 files.
I don't know, if this is what you meant, but dir() returns a struct with name and folder as char.
The filenames are automatically generated (they're output files from experiments), so in this case I know they don't contain any spaces. I can load the files using fullfile(), so I think that should be fine.
dpb
dpb on 19 Apr 2020
Do a dir() on the target destination and compare to the AllFiles original.
dDest=dir(fullfile(destination, '**', 'DT_data*mat'));
SrcDestDiff=setdiff({AllFiles.name},{dDest.name})

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 19 Apr 2020
Try this:
% Get top level folder.
ddir = uigetdir(pwd);
filePattern = fullfile(ddir, '**', 'DT_data*.mat');
AllFiles = dir(filePattern);
destination = 'C:\Users\XXXXX\Desktop\tempFigData';
if ~isfolder(destination)
mkdir(destination);
end
fprintf('Found %d files.\n', length(AllFiles));
if length(AllFiles) == 0
warningMessage = sprintf('Did not find any files matching %s', filePattern);
uiwait(errordlg(warningMessage));
return;
end
for i = 1:length(AllFiles)
sourceFileName = fullfile(AllFiles(i).folder, AllFiles(i).name);
destinationFileName = fullfile(destination, AllFiles(i).name);
fprintf('Copying file %s\n to %s\n', sourceFileName, destinationFileName);
% copyfile(sourceFileName, destination);
end
Now what do you see?
  7 Comments
dpb
dpb on 20 Apr 2020
Edited: dpb on 20 Apr 2020
He's already done that in response to my first Comment above, IA. Reported no errors, no messages for all 515.
<https://www.mathworks.com/matlabcentral/answers/518934-copyfile-skips-files#comment_830520>
dpb
dpb on 20 Apr 2020
Edited: dpb on 20 Apr 2020
Just for grins, @kova, how about attach (use the paperclip icon) a .mat file that contains the two dir() arrays above (AllFiles and dDest)? Make detailed probing easier if folks had local copies...

Sign in to comment.

Categories

Find more on Programming in Help Center and File Exchange

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!