Compare file names and delete some of them
5 views (last 30 days)
Show older comments
Hello, I am executing code for a list of 700 run files that are saved into a folder I've called "Run". Each run file has a unique name which follows this format: "RUN_variable_model_date.yml". Once a run file is complete, it writes an Excel file with the same naming format, only with extension .xlsx (instead of .yml) and saves it into a folder called "Output". The problem is that my code is skipping over several of the run files. I need to figure out which of the 700 run files have completed successfully and which I need to rerun. I think I need to create a loop that will compare the file names in the "Run" and "Output" folders and delete any run files that have a match in the "Output" folder. Does anyone have any sample code I might use to get started, or is there a particular Matlab tool that is designed to help with this type of problem? Thanks!
0 Comments
Answers (1)
Walter Roberson
on 25 Aug 2015
run_folder = 'Run';
out_folder = 'Output';
in_ext = 'yml';
out_ext = 'xlsx';
dinfo_run = dir( fullfile(run_folder, ['*.' in_ext]) );
basename_run = regexprep( {dinfo_run.name}, ['\.' in_ext '$'], '');
dinfo_out = dir( fullfile(out_folder, ['*.' out_ext]) );
basename_out = regexprep( {dinfo_out.name}, ['\.' out_ext '$'], '');
output_already = ismember(basename_run, basename_out);
basename_tryagain = basename_run(~output_already);
for K = 1 : length(basename_tryagain)
thisbase = basename_tryagain{K};
thisfile_in = fullfile(run_folder, [thisbase '.' in_ext]);
thisfile_out = fullfile(out_folder, [thisbase '.' out_ext]);
%now process thisfile_in creating thisfile_out
end
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!