copy file not working as it should
Show older comments
hello there, i have a code that somehow just skips the copy file line and did not move file as it should. i tried adding [ status, message, messageId] to see whats going on and it did not return anything. it goes through the other lines fine, but it didnt copy it anywhere, much less the intended file. if someone can point out what i did wrong that will be very helpful. thanks!
mainfolder2 ='/Users/sesiliamaidelin/Downloads/summer project/Copy_of_CL2 Reviewer Network'; % Subfolders contains bitmap images
nufolderpath = '/Users/sesiliamaidelin/Downloads/summer project/nufolder'; % Define the path name of nufolder
filesAndFolders2 = dir([mainfolder2 '/**']); % Get all subfolder and files names
alllist2={filesAndFolders2.name}; % Convert to cell
bmpidx = cellfun(@(x) contains(x,'.bmp'),alllist2); % Index to extract dicom files only
bmplist = alllist2(bmpidx) % List of dicom files only
bmppath = {filesAndFolders2(bmpidx).folder}; % Extract the path of each dicom files
%%
for jj = 1 : numel(dcmlist)
[filepath, name, ext]= fileparts(dcmlist(jj));
ptt= digitsPattern + regexprep( dcmlist{jj},'.dcm$',' ')+'_';
for kk = 1 :numel(bmplist)
matchedIdx = contains(bmplist(kk), ptt)
if matchedIdx == true
movefile(fullfile(bmppath(kk),bmplist(kk)),fullfile( nufolderpath, name))
else
fprintf('file %s does not have a matching dicom file\n ', bmplist{kk});
end
end
end
2 Comments
Cris LaPierre
on 16 Jul 2021
What is your value of matchedIdx? Is it ever true?
sesilia maidelin
on 16 Jul 2021
Accepted Answer
More Answers (1)
Jan
on 16 Jul 2021
Use the debugger to find out, what's going on:
for kk = 1 :numel(bmplist)
if contains(bmplist{kk}, ptt)
sourceFile = fullfile(bmppath{kk}, bmplist{kk});
destFile = fullfile(nufolderpath, name);
[succ, msg] = movefile(sourceFile, destFile);
if ~isfile(destFile)
error('File not copied?! %s -> %s', sourceFile, destFile);
end
else
fprintf('file %s does not have a matching dicom file\n', bmplist{kk});
end
end
Now set a breakpoint in the IF block and step through the code line by line. Is MOVEFILE really reached?
Such lines a fragile:
ptt= digitsPattern + regexprep( dcmlist{jj},'.dcm$',' ')+'_';
Adding CHARs lead to unexpected results, if the former argument is not a string:
'A' + '_'
% 160, no 'A_'
11 Comments
sesilia maidelin
on 16 Jul 2021
Jan
on 16 Jul 2021
This means, that contains(bmplist{kk}, ptt) is not true. What is ptt?
sesilia maidelin
on 16 Jul 2021
Walter Roberson
on 16 Jul 2021
I would point out that 2016051200072.bmp does not contain any blanks. Your ppt would not be 00072 : it would be "one or more digits" followed by something extracted from dcmlist with .dcm changed to blank, followed by underscore ... and 2016051200072.bmp does not have blanks and does not have underscore.
sesilia maidelin
on 16 Jul 2021
Walter Roberson
on 16 Jul 2021
I am confused about exactly what would be in dcmlist and exactly what filenames in bmplist need to be matched.
Would dcmlist contain '00072' and you need to move all DATE '00072._Frame' <something> '.bmp' files ?
Or is the dcmlist input by date like 20160512 and you need to move all files that start with that? Or something else?
sesilia maidelin
on 18 Jul 2021
sesilia maidelin
on 18 Jul 2021
Walter Roberson
on 18 Jul 2021
You know how long the DATE portion is, you know how long the folder name portion is: instead of doing pattern matching, just extract the relevant portion of the name and do the comparison.
sesilia maidelin
on 19 Jul 2021
Jan
on 20 Jul 2021
Asking many question about Matlab is the purpose of this forum. So your are welcome.
It is still not clear, what "doesn't work" means. Please replace:
movefile(fullfile(bmppath{kk},bmplist{kk}),fullfile(nufolderpath,name,'reviewer network'))
by:
sourceFile = fullfile(bmppath{kk},bmplist{kk});
destFolder = fullfile(nufolderpath,name,'reviewer network');
[status, msg] = movefile(sourceFile, destFolder);
if status ~= 1
error('Cannot copy file %s to %s: %s', sourceFile, destFile, msg);
end
Maybe the destination folder is write protected? Under Windows this does not mean, that you cannot write into this folder... (I hate it). Then:
[status, msg] = movefile(sourceFile, destFolder, 'f');
Please run this and explain exactly, what you observe. I guess, that the copy works as wanted, but you expect it to appear on another location.
Categories
Find more on Matrix Indexing 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!