FileCopy does't work

조회 수: 1 (최근 30일)
Life is Wonderful
Life is Wonderful 2019년 9월 7일
댓글: Life is Wonderful 2019년 9월 18일
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
Life is Wonderful
Life is Wonderful 2019년 9월 7일
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 2019년 9월 8일
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')

댓글을 달려면 로그인하십시오.

채택된 답변

Guillaume
Guillaume 2019년 9월 9일
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
Guillaume
Guillaume 2019년 9월 17일
편집: Guillaume 2019년 9월 17일
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 2019년 9월 18일
Thanks a ton for all learnings i received from you!

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Type Conversion에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by