MATLAB Answers

FileCopy does't work

1 view (last 30 days)
Matlab
Matlab on 7 Sep 2019
Commented: Matlab 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

Show 1 older comment
Matlab
Matlab on 7 Sep 2019
it doesn't do what you want but since you haven't told us what it's supposed to do nor what it actually does, it's difficult to help.
Goal :The goal is to move files from every Subfolder "debug" to a new folder "OutputFolder"
attempt to copy the same file from the Param.Source directory
I want to copy "CurrentFile" (test_that.DEBUG) from the Subdirectory (test_that_results_hatch_B1CYKd\debug) "debug" to new directory "OutputFolder" (test_that_results_hatch_B1CYKd\Tmp).
Note: CurrentFile (test_that.DEBUG) has dot in file name which I don't want to have ( Since filereading is not supported . I want to pass filename as input argument to a function[readtable] ). So I have to remove dot from CurrentFile name and rename CurrentFile to RenameFile (test_thatDEBUG).
I assume you get 0 displayed in the command window
I know it's NOT working . I added it for debug purpose :-)
RenameFile= regexprep(CurrentFile,{'\.'},{''}) % Replace dot from Files.name
Remove dot from the current file name
Many thanks for your kind help. For reference I have added *.mat file. I am sure now I can move forward.
Matlab
Matlab 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

Matlab
Matlab on 17 Sep 2019
OK, Then how to proceed? Please help me. Thanks!
Guillaume
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.
Matlab
Matlab on 18 Sep 2019
Thanks a ton for all learnings i received from you!

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!