Compare file names and delete some of them

5 views (last 30 days)
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!

Answers (1)

Walter Roberson
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
  1 Comment
Shannon Jones
Shannon Jones on 26 Aug 2015
Hi Walter, thanks so much for your quick response. I'm trying to figure out what the code does here... I just tested the code with some sample files in the "Run" and "Output" folders. I'm looking at the output for the "basename_tryagain" variable. It's printing all of the file names in the "Run" folder, regardless of whether they match with a file in the "Output" folder. What is the intended output from this code? A list of file names that I should keep in the "Run" folder to retry? Thanks!

Sign in to comment.

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!