% tidy up
clear all; 
close all;

% check for input.in
if exist('.\input.in', 'file') == 2
    parentinputfile = 1;
else
    parentinputfile = 0;
end %if

% check for setrunparameters.m
if exist('.\setrunparameters.m', 'file') == 2
    parentparameterfile = 1;
else
    parentparameterfile = 0;
end %if

% get directory listing
list = dir;

% loop through list, entering each folder
for i = 1:length(list)
    
    % check if folder
    if list(i).isdir == 1 && ~strcmpi(list(i).name, '.') && ~strcmpi(list(i).name, '..')

        % enter folder
        cd(list(i).name);
        
        % display folder name
        disp(list(i).name);
        disp('');
        
        % copy input file if required
        if parentinputfile == 1
            if exist('.\input.in', 'file') ~= 2
                disp('copying parent input.in file');
                try
                    copyfile('..\input.in','.')
                catch err
                    disp('error 342828 copying input.in file');
                end %try
            end %if
        end %if
                
        % copy parameter file if required
        if parentparameterfile == 1
            if exist('.\setrunparameters.m', 'file') ~= 2
                disp('copying parent setrunparameters.m file');
                try
                    copyfile('..\setrunparameters.m','.')
                catch err
                    disp('error 024789 copying setrunparameters.m file');
                end %try
            end %if
        end %if
        
        % run
        try
            run;
        catch err
            msg = ['run failed in folder ' pwd];
            try
                twit(msg);
            catch err2
            end %try
            disp(msg);
        end %try
        
        % return to parent directory
        cd('..');
        
    end
    
end

% notify completion
msg = ['batch complete in folder ' pwd ' for user ' getenv('USERNAME')];
try
    twit(msg);
catch err
end %try
disp('batch complete.');