function NewFileName = autoblksNewMbcFileName(WorkDir, BaseName, Ext)
% Create new unique file name in WorkDir

% Copyright 2018 The MathWorks, Inc.

if isfolder(WorkDir)
    FindFiles = dir([WorkDir, '/*', Ext]);
    FileNames = {FindFiles.name};
    MatchedNames = FileNames(startsWith(FileNames, [BaseName,'_']));
    if isempty(MatchedNames)
        NewIdx = 1;
    else
        SearchStartExt = strfind(MatchedNames, Ext);
        FileNumber = zeros(size(MatchedNames));
        for i = 1:length(MatchedNames)
            MidFileName = MatchedNames{i}(((length(BaseName)+2):(SearchStartExt{i}(1)-1)));
            CurrIdx = str2double(MidFileName);
            if isfinite(CurrIdx)
                FileNumber(i) = CurrIdx;
            end
        end
        NewIdx = max(max(FileNumber))+1;
    end
    NewFileName = [BaseName, '_', num2str(NewIdx), Ext];
else
    NewFileName = [BaseName, '_1', Ext];
end