movefile and copyfile give no result, why?
4 views (last 30 days)
Show older comments
Dear,
I try to sort out files based on a specific number in the file (same row), in looping through the files. After selecting this number I want to place the file in a specific folder. At the moment I try the code with one file. But neither the movefile neither the copyfile seem to be working, Matlab gives status=1 and now error messages. Can someone help me out, because I don't understand where the problem lays.
>> A
A =
04102902.MWD
>> status=copyfile(A,'../Subterra/Typexx')
status = 1
I am using R2014b (8.4.0.150421).
The first part op the code
Files = dir('*.MWD');
for ii = 1:length(Files)
FID=fopen(Files(ii).name);
L5cell = textscan(FID, '%f', 1, 'HeaderLines', 4, 'CollectOutput', 1);
L5=cell2mat(L5cell);
B=struct2cell(Files(ii));
A=cell2mat(B(1,1));
status = fclose(FID);
if L5==0;
copyfile(A,'../Subterra/Type00');
elseif L5==1;
copyfile(A,'../Subterra/Type01');
%etc. (I have 11 types)
end
end
Regards,
Jeroen
2 Comments
Guillaume
on 7 Mar 2016
Instead of putting blank lines between each line of code, select all the code and click the {} Code button. That will make your post much more readable.
Jan
on 7 Mar 2016
Using trivial names for variables is always a good idea:
B = struct2cell(Files(ii));
A = cell2mat(B(1,1));
Better:
FileName = Files(ii).name;
Answers (2)
Guillaume
on 7 Mar 2016
I have no idea why copyfile would fail to copy a file and still return a success status. In the code you've posted, you don't test for the status of copyfile so is it the actual code you're using?
An obvious question is: Are you sure you're looking for the files in the correct folder? My recommendation would be to use full paths everywhere instead of relative paths to the current directory:
sourcepath = 'c:\somewhere\on\your\drive'
files = dir(fullfile(sourcepath, '*.MWD'));
for file = files' %a neater way to iterate over the files
fid = fopen(fullfile(sourcepath, file.name)
%... rest of the code
Also, hopefully you're not using 11 elseif in your code. If all the folders where you want to move code are all Typexx, then:
[status, message] = copyfile(fullfile(sourcepath, file.name), ...
fullfile(sourcepath, sprintf('../Subterra/Type%02d', L5)));
if ~status
error('Failed to copy file with error: %s', message);
end
If the destination is arbitrary:
destfolders = {'../Subterra/Type00', '../Subterra/Type01', 'other/folder', 'etc.'};
[status, message] = copyfile(fullfile(sourcepath, file.name), ...
fullfile(sourcepath, destfolders{L5+1}));
5 Comments
Guillaume
on 7 Mar 2016
Edited: Guillaume
on 7 Mar 2016
Please, please, use the {}Code button to format your code properly instead of adding blank lines, or just put two spaces before each line.
I've just realised what the cause of the issue is, the destination for copyfile is just a folder name, it's missing a filename, this should fix it:
copyfile(fullfile(sourcepath, file.name) ...
fullfile(sourcepath, sprintf('../Type%02d', L5), file.name));
Walter Roberson
on 7 Mar 2016
copyfile does not require a destination file name, only a folder name.
"copyfile('source','destination') copies the file or folder named source to the file or folder destination"
Walter Roberson
on 7 Mar 2016
You would get errors if no directory ../Subterra existed, or the directories ../Subterra/Typexx do not exist.
0 Comments
See Also
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!