FileCopy does't work

1 view (last 30 days)
Life is Wonderful
Life is Wonderful on 7 Sep 2019
Commented: Life is Wonderful on 18 Sep 2019
Copying files from source directory to destination directory is NOT working.
Below is my source code. Please help me
WorkingDir = 'C:\Users\shastry\MathWorks\test_that_results_hatch_B1CYKd';
OutputFolder = fullfile(WorkingDir, 'Tmp');
if ~exist(OutputFolder, 'dir')
mkdir(OutputFolder);
end
Param.Source = WorkingDir; %'C:\Users...';
Param.Target = OutputFolder; %'C:\Users...';
Files=dir(fullfile(Param.Source,'**\*.*'));
Files = Files(~[Files.isdir]); % Remove folders from list;
for k = 1:length(Files)
if contains(Files(k).folder,'debug')
if contains(Files(k).name,'test_that')
CurrentFile = Files(k).name;
exist(CurrentFile,'file') % 1=exist 0=doesn't exist
RenameFile= regexprep(CurrentFile,{'\.'},{''}); % Replace dot from Files.name
exist(RenameFile,'file') % 1=exist 0=doesn't exist
copyfile(fullfile(Param.Source, CurrentFile), fullfile(Param.Target, RenameFile));% copy sourcefolder file to destnationfolder file
end
end
end
  4 Comments
Life is Wonderful
Life is Wonderful on 7 Sep 2019
I have rewritten the code as below , it copies the ranamed file to OutputFolder directory
WorkingDir = 'C:\Users\shastry\MathWorks\test_that_results_hatch_B1CYKd';
OutputFolder = fullfile(WorkingDir, 'Tmp');
if ~exist(OutputFolder, 'dir')
mkdir(OutputFolder);
end
Param.Source = WorkingDir; %'C:\Users...';
Param.Target = OutputFolder; %'C:\Users...';
Files=dir(fullfile(Param.Source,'**\*.*'));
Files = Files(~[Files.isdir]); % Remove folders from list;
for k = 1:length(Files)
%debug subfolder
if contains(Files(k).folder,'debug') % look for folder with debug name
if contains(Files(k).name,'test_that') % look for file with test_that name
RenameFile = regexprep(Files(k).name,{'\.'},{''}); % Replace dot Files.name
[SUCCESS,MESSAGE,MESSAGEID] = copyfile(fullfile(Files(k).folder ,Files(k).name), fullfile(Param.Target, RenameFile));% copy sourcefolder file to destnationfolder file
end
end
% result- subfolder
if contains(Files(k).folder,'results-') % look for folder with results-
if contains(Files(k).name,'autoserv') % look for file with autoserv
RenameFile = regexprep(Files(k).name,{'\.'},{''}); % Replace dot Files.name
[SUCCESS,MESSAGE,MESSAGEID] = copyfile(fullfile(Files(k).folder ,Files(k).name), fullfile(Param.Target, RenameFile));% copy sourcefolder file to destnationfolder file
end
end
%firmware_ subfolder
if contains(Files(k).folder,'firmware_') % look for folder with firmware_
if contains(Files(k).name,'firmware_') % look for file with test_that name
RenameFile = regexprep(Files(k).name,{'\.'},{''}); % Replace dot Files.name
[SUCCESS,MESSAGE,MESSAGEID] = copyfile(fullfile(Files(k).folder ,Files(k).name), fullfile(Param.Target, RenameFile));% copy sourcefolder file to destnationfolder file
end
end
end
Guillaume
Guillaume on 8 Sep 2019
That looks much better, you're now using the actual folder where the file is found.
I assume your problem is solved?
I don't understand your comment about the . in the file name. Matlab IO functions don't care one bit about the format of the filename, you can have as many or as few . in the filename, it won't change anything.
Note that it would be clearer if you passed char arrays to to regexprep instead of scalar cell arrays:
RenameFile = regexprep(Files(k).name, '\.', '');
%or
RenameFile = strrep(Files(k).name, '.', '');
and the whole lot, if it's needed would be better written only once before the ifs.
Also the double ifs could be written as just one:
if contains(Files(k).folder,'debug') && contains(Files(k).name,'test_that')

Sign in to comment.

Accepted Answer

Guillaume
Guillaume on 9 Sep 2019
I don't remember the details of your previous question, but clearly the code I gave you is meant to work with data imported a particular way. If you change the way you import the data, it's not going to work. Reading the code above Content must be a scalar structure where each field is itself a scalar structure with just one field which is a table.
In your mat file, Content is a cell array, not a structure as expected. So yes, you'll get an error but not the one you mention (it's fieldnames that would complain first). The cell array can trivially be converted into a structure but the substructures contain more than one table. As it is, the code would only use the first table. The code would have to be modified to cope with more than one table per structure, but it's unclear what you want to do.
Furthermore, the Content description you pasted above doesn't match your attached mat file. Above, Content is indeed a structure as expected, but the fields contain cell arrays, not structures. So, yes you'll get a struct2cell error from that.
I think I told you before, if the format of your input keep on changing don't expect the solutions you're given to work anymore. It's important to be consistant.
  18 Comments
Guillaume
Guillaume on 17 Sep 2019
Edited: Guillaume on 17 Sep 2019
I don't know yet. It may not be simple.
In any case, it's a completely different question from what you originally asked, so you should start a new one.The two timetable examples I gave above would probably be a good start for that new question.
Life is Wonderful
Life is Wonderful on 18 Sep 2019
Thanks a ton for all learnings i received from you!

Sign in to comment.

More Answers (0)

Categories

Find more on Data Type Conversion 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!