9 Feb 2018

Get the latest release folder from a directory

收藏到CSDN网摘
Sometimes, we might need to find the latest release folder from a software repository according to the time stamp of the folder property. This could be done by extracting all the time stamps and sort them accordingly. MATLAB provides several function to do this nicely.

function out = GetLatestRelease(in)
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% input:
%  in:   the software repository with all release folders
%
% output:
%  out:  the path of the latest release
%
% %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

out = '';
try
   % all sub folders
   dirs = dir(in);
   issub = [dirs(:).isdir];
   subs = dirs(issub);
   % get rid of . and ..
   subs = subs(3:end);
   % name with 'release'
   names = {subs.name};
   isrelease = cellfun(@(x)contains(x,'release'),names);
   subs = subs(isrelease);
   % order by date
   Sfield = fieldnames(subs);
   % find which is the date field
   idx = find(cellfun('length',regexp(Sfield,'^date$'))==1);
   % sort by date
   dates = {subs.date};
   [~,ord] = sort(datenum(dates,'dd-mm-yyyy HH:MM:SS'));
   subs = subs(ord);
   % get the latest one
   names = {subs.name};
   out = strcat(subs(1).folder,'\',names{end});
   % get the path for matlab version, project specific routine
   dirs = dir(out);
   issub = [dirs(:).isdir];
   subs = dirs(issub);
   subs = subs(3:end);
   names = {subs.name};
   lnames = cellfun(@(x)lower(x),names,'uniformoutput',false);
   idx = find(cellfun('length',regexp(lnames,'for_matlab'))==1);
   out = strcat(out,'\',names{5});
catch
   error('Cannot get latest release from the folder:\n%s\n',in);
end
end

No comments :

Post a Comment