copyfile skips files?

11 次查看(过去 30 天)
kova
kova 2020-4-18
编辑: dpb 2020-4-20
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 个评论
kova
kova 2020-4-18
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 2020-4-19
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})

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2020-4-19
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 个评论
dpb
dpb 2020-4-20
编辑:dpb 2020-4-20
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 2020-4-20
编辑:dpb 2020-4-20
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...

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

标签

产品


版本

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by