function [ all_e2_info ] = NBO_E2_Vals_Folder( folder_list )

%********************************Summary********************************%
%   Runs the NBO_E2_Vals_File code on every .log file in a folder       %
%	and outputs the info from all files in one massive cell array  	    %
%*********************************Input*********************************%
%	1) folder_list = 1xn (row) cell array containing the full path to   %
%                    all folders containing the .log files              %
%*********************************Output********************************%
%   1) all_e2_info =nxm cell array containing loads of info. Each       %
%                   folder gets ~(n*6)-1 columns organised as follows:  %
%             row1/2 = column names                                     %
%             col 1 = list of filenames imported                        %
%             col 2->n = sum of all e2 values for all combos of         %
%                        intermolecular donor/acceptor interactions     %
%             col n-->(n*2)-1 = e(2) values for the strongest intermol. %
%                               donor/acceptor interaction for each     %
%                               unit pair                               %
%             col (n*2)-1-->(n*3)-1 = info on the strongest e2 D/A int. %
%                                     for all intermol. D/A combos      %
%             col(n*3)-1-->n(*6)-1 = same format as previous 3, but     %
%                                    with only D/A interactions with    %
%                                    hydrogen acting as an acceptor     %
%                                    considered                         %
%             These arrays are horizontally concat. for each folder.    %
%		      NOTE:only INTER-unit/molecular values used in this code   %
%             Values are all in kJ mol^-1 EXCEPT for the parts that're  %
%             full input line from the .log file ("info on the          %
%             strongest e2 D/A int") -labeld info at top of col.        %
%***********************************************************************% 
%*********************************Issues********************************%
%   1) Suspect that it may fail for numb_units>10 (due to the           %
%      search_string_matrix not working in file_code. Suspect it needs  %
%      changing to search for a reg. expr. to solve                     %
%   2) initially set so that maximum number of units (as defined        %
%      by the NBO analysis) is average of 4 per file. Change            % 
%      max_numb_frags in Main if using bigger systems.                  %
%   3) ALL files in a folder MUST have the same number of units         %
%   4)Only tried for systems with 2 units/fragments so far (ion pairs)  %
%      so be careful if using for larger systems (i see no reason it    %
%      wont work properly, but its untested)                            %
%***********************************************************************% 
%**************************History**************************************%
%    26th May: added new output_file.txt                                %
%***********************************************************************%



    %********************************Summary********************************%
    %	Takes a list of folders and generates a matrix containing the       %
    %   full paths to all files with a certain extension                    %
    %*********************************Input*********************************%
    %	1) folder_list = 1xn (row) cell array containing the full path to   %
    %                    all folders                                        %
    %   2) ext = string containing extension of files of interest (e.g      %
    %            '.txt'                                                     %
    %*********************************Output********************************%
    %   1) file_list_matrix =nxm cell array, each col=different folder,     %
    %                        each row contains full path of a file(i.e col  %
    %                        1 is all files in folder 1)                    %
    %*********************************Issues?*******************************%
    %    1) May not work with >100 files per folder currently. Modify       %
    %       max_numb_files variable if thats the case                       %
    %***********************************************************************%
    function [file_list_matrix] = Gen_Filelist(folder_list,ext)
        max_numb_files = 100;  %maximum number of files per folder. just used to initialise a massive matrix
        file_list_matrix = cell(max_numb_files,size(folder_list,2));
        %Extracting file names
        for i=1:size(folder_list,2)
            %get list of all files in folder
            filelist_struct= dir(folder_list{1,i});
            counter=1;
            for j=1:size(filelist_struct,1);
                if filelist_struct(j).isdir ~= 1 && isempty(strfind(filelist_struct(j).name,ext))~=1  &&  isempty(strfind(filelist_struct(j).name,'.swp'))==1;
                    file_list{counter} = filelist_struct(j).name;
                    counter=counter+1;
                end
            end
            file_list = strcat(folder_list{1,i},'\',file_list);
            file_list_matrix(1:size(file_list,2),i) = file_list';
            clear file_list;
        end
        % rmoving emty cells
        max_rows = 0;	%max number of non-empty rows
        for j=1:size(file_list_matrix,2);
            for i=1:size(file_list_matrix,1);
                if isempty(file_list_matrix{i,j});
                    if (i-1) > max_rows
                        max_rows = i-1;
                    end
                    break	;
                end
            end
        end
        file_list_matrix = file_list_matrix (1:max_rows,:);
    end







    function [] = Main()
        [file_list_matrix] = Gen_Filelist(folder_list,'.log')
        
        
        %Initialising the final output_matrix
        max_numb_frags = 2;	%used to initialise columns,higher=slower but less chance of breakage
        all_e2_info = cell(size(file_list_matrix,1) + 2, ((((max_numb_frags^2)-3)*6)+1)*size(folder_list,2));
        start_pos = 1;	%Column to start adding to in all_e2_info for each folder

        %Looping through/extracting all info from all folders/files
        for folder=1:size(file_list_matrix,2)
            curr_folder_name = strsplit(folder_list{1,folder},'\');
            curr_folder_name = curr_folder_name{end};
            all_e2_info{1,start_pos} = curr_folder_name;
            all_e2_info{2,start_pos} = 'filename';
            
            for file=1:size(file_list_matrix,1)
                
                if isempty(file_list_matrix{file,folder}) 
                    break;
                end
                %Actually getting all the useful info from .log file
                [ unit_list,all_e2_sum_matrix,all_e2_max_matrix,all_e2_max_info,h_e2_sum_matrix,h_e2_max_matrix,h_e2_max_info ] = NBO_E2_Vals_File( file_list_matrix{file,folder} );
                numb_frags = size(unit_list,1);
                curr_col = start_pos;	%tracks which column we should add to next. Modified WITHIN each folder (rather
                
                %Adding filename info
                curr_filename = strsplit(file_list_matrix{file,folder},'\');
                curr_filename = curr_filename{end};
                all_e2_info{file+2,curr_col} = curr_filename;
                
                %****SECTION 1****%
                %Getting labels for all possible unit combos. Diagonal elements (e.g unit 1--> unit 1) ignored.
                numb_units = size(unit_list,1);
                all_combos = cell(1,((numb_units^2)-numb_units));
                counter=1;
                for i =1:size (unit_list,1);
                    for j=1:size(unit_list,1)
                        if i~=j
                            curr_combo = strcat(unit_list{i,1},'->',unit_list{j,1});
                            all_combos{1,counter} = curr_combo;
                            counter=counter+1;
                        end
                    end
                end
                
                %****SECTION 2****%
                %Extracting sum of all e2 values for all combos
                e2_sum_all = cell(1,((numb_units^2)-numb_units));
                curr_col = curr_col+1;
                counter=1;
                for i =1:size (all_e2_sum_matrix,1);
                    for j=1:size(all_e2_sum_matrix,2)
                        if i~=j
                            e2_sum_all{1,counter} = num2str(all_e2_sum_matrix(i,j));
                            counter=counter+1;
                        end
                    end
                end
                all_e2_info{1,curr_col} = 'sum E2 vals (All)';
                all_e2_info(2,curr_col:curr_col + size(all_combos,2)-1) = all_combos(1,:);	%unit labels
                all_e2_info(file+2,curr_col:curr_col + size(e2_sum_all,2)-1) = e2_sum_all(1,:);	%actual values
                curr_col = curr_col + size(all_e2_sum_matrix,2);
                
                %****SECTION 3****%
                %Extracting e(2) values for the strongest intermol. D/A interaction between each unit combo
                e2_strongest_all = cell(1,((numb_units^2)-numb_units));
                counter=1;
                for i =1:size (all_e2_max_matrix,1);
                    for j=1:size(all_e2_max_matrix,2)
                        if i~=j
                            e2_strongest_all{1,counter} = num2str(all_e2_max_matrix(i,j));
                            counter=counter+1;
                        end
                    end
                end
                all_e2_info{1,curr_col} = 'strongest E2 vals (All)';
                all_e2_info(2,curr_col:curr_col + size(all_combos,2)-1) = all_combos(1,:);
                all_e2_info(file+2,curr_col:curr_col + size(e2_strongest_all,2)-1) = e2_strongest_all(1,:);
                curr_col = curr_col + size(e2_strongest_all,2);
                
                %****SECTION 4****%
                %Extracting full line describing the e(2) values for the strongest intermol. D/A interaction between each unit combo
                e2_strongest_info_all = cell(1,((numb_units^2)-numb_units));
                counter=1;
                for i =1:size (all_e2_max_info,1);
                    for j=1:size(all_e2_max_info,2)
                        if i~=j
                            e2_strongest_info_all{1,counter} = all_e2_max_info{i,j};
                            counter=counter+1;
                        end
                    end
                end
                all_e2_info{1,curr_col} = 'strongest E2 vals INFO (All)';
                all_e2_info(2,curr_col:curr_col + size(all_combos,2)-1) = all_combos(1,:);
                all_e2_info(file+2,curr_col:curr_col + size(e2_strongest_info_all,2)-1) = e2_strongest_info_all(1,:);
                curr_col = curr_col + size(e2_strongest_info_all,2);
                
                %****SECTION 5****%
                %Extracting sum of all e2 ints which involve hydrogen acting as an acceptor
                e2_sum_hydrogen = cell(1,((numb_units^2)-numb_units));
                counter=1;
                for i =1:size (h_e2_sum_matrix,1);
                    for j=1:size(h_e2_sum_matrix,2)
                        if i~=j
                            e2_sum_hydrogen{1,counter} = num2str(h_e2_sum_matrix(i,j));
                            counter=counter+1;
                        end
                    end
                end
                all_e2_info{1,curr_col} = 'sum E2 vals (Hydrogen)';
                all_e2_info(2,curr_col:curr_col + size(all_combos,2)-1) = all_combos(1,:);
                all_e2_info(file+2,curr_col:curr_col + size(e2_sum_hydrogen,2)-1) = e2_sum_hydrogen(1,:);
                
                curr_col = curr_col + size(h_e2_sum_matrix,2);
                
                %****SECTION 6****%
                %Extracting e(2) values for the strongest intermol. D/A interaction between each unit combo,
                %with hydrogen acting as the acceptor
                
                e2_strongest_hydrogen = cell(1,((numb_units^2)-numb_units));
                counter=1;
                for i =1:size (h_e2_max_matrix,1);
                    for j=1:size(h_e2_max_matrix,2)
                        if i~=j
                            e2_strongest_hydrogen{1,counter} = num2str(h_e2_max_matrix(i,j));
                            counter=counter+1;
                        end
                    end
                end
                all_e2_info{1,curr_col} = 'strongest E2 vals (Hydrogen)';
                all_e2_info(2,curr_col:curr_col + size(all_combos,2)-1) = all_combos(1,:);
                all_e2_info(file+2,curr_col:curr_col + size(e2_strongest_hydrogen,2)-1) = e2_strongest_hydrogen(1,:);
                curr_col = curr_col + size(e2_strongest_hydrogen,2);
                
                
                %****SECTION 7****%
                %extracting e2 info the strongest intermol. D/A interaction where H is acting as an acceptor
                e2_strongest_info_hydrogen = cell(1,((numb_units^2)-numb_units));
                counter=1;
                for i =1:size (h_e2_max_info,1);
                    for j=1:size(h_e2_max_info,2)
                        if i~=j
                            e2_strongest_info_hydrogen{1,counter} = h_e2_max_info{i,j};
                            counter=counter+1;
                        end
                    end
                end
                all_e2_info{1,curr_col} = 'strongest E2 vals INFO (Hydrogen)';
                all_e2_info(2,curr_col:curr_col + size(all_combos,2)-1) = all_combos(1,:);
                all_e2_info(file+2,curr_col:curr_col + size(e2_strongest_info_hydrogen,2)-1) = e2_strongest_info_hydrogen(1,:);
                curr_col = curr_col + size(e2_strongest_info_hydrogen,2);
                
                next_folder_start_pos = curr_col+1; %For files from the next folder in folder_list.
                curr_col = start_pos;  %THIS NEEDS TO BE last line of each file loop
            end
            start_pos = next_folder_start_pos;
            
        end
        %Making the ouput comma delimited
        
        output_table = all_e2_info;
        
        for i=1:size(all_e2_info,1)
            for j=1:size(all_e2_info,2)
                all_e2_info{i,j} = strcat(all_e2_info{i,j},',');
            end
        end
        
        %Making output file
        out_name = 'output_file.txt'
        writetable(cell2table(output_table),out_name);
        
    end





tic
Main
toc


end

