par;
fprintf('\r%-40s\n','batch reset orientation ')

% get the subdirectories in the main directory
for sb = 1:length(PAR.subjects) % for each subject
    %go get the sessions
    for ses=1:length(PAR.sess)

        str   = sprintf('sub #%3d/%3d: %-5s, %-10s',sb,PAR.nsubs,PAR.subjects{sb},PAR.sess{ses} );
        fprintf('\r%-40s  %30s',str,' ')
        fprintf('%s%30s',repmat(sprintf('\b'),1,30),'...reseting')  %-#
             
        %now get all images to reset orientation
        for c=1:ncond
            % get files in this directory
            P=spm_select('ExtFPList', char(PAR.condirs{sb,c}), ['^' PAR.fmriprefix{c} '\w*\.nii$'], 1:1000);

            if isempty(P)
                P=spm_select('FPList', char(PAR.condirs{sb,c}), ['^' PAR.fmriprefix{c} '\w*\.img$']);
                if isempty(P)
                    disp('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n NO epi file\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
                    continue;
                end
            end;
            ASLtbx_resetimgorg(P);
          
        end
         
        P=spm_select('FPList', char(PAR.structdir{sb}), ['^' PAR.structprefs '.*\.nii$']);
        
        if isempty(P)
            P=spm_select('FPList', char(PAR.structdir{sb}), ['^' PAR.structprefs '.*\.img$']);
            if isempty(P)
                disp('!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n NO struct file\n!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
                continue;
            end
        end;
        %P=strvcat(P, spm_select('FPList', char(PAR.structdir{sb}), ['^t1_noskull\.nii$']));
        ASLtbx_resetimgorg(P);
         fprintf('%s%30s',repmat(sprintf('\b'),1,30),'...done')  %-#

    end
end
