"1. Is not recursive."
That's true; however, it is intended to operate on all folders starting with "S" at once, and find .nii files starting with "r" in each such folder.
SourcePath = 'D:\folder1\folder2\folder3';
dinfo = dir(fullfile(SourcePath, 'S*', 'r*.nii'));
"2.the expression must be changed [from '\\r(\w+\.nii)' to '\\r(\w*)']"
That's not true.
'\\r(\w+\.nii)' matches a backslash followed by a lowercase "r" followed by one or more alphanumeric or underscore characters followed by ".nii". Therefore it matches file names starting with "r" and ending with ".nii", as long as the rest of the file name contains only alphanumeric characters or underscores.
'\\r(\w*)' matches a backslash followed by a lowercase "r" followed by zero or more alphanumeric or underscore characters. It's not limited to matching file names ending with ".nii", and in fact it's not even limited to matching file names (i.e., without the "\.nii" specified, it can match directory names too).
Consider:
regexprep('C:\Users\Docs\S1000\rkehwr.nii','\\r(\w*)','\\f$1')
ans = 'C:\Users\Docs\S1000\fkehwr.nii'
regexprep('C:\Users\rocs\S1000\rkehwr.nii','\\r(\w*)','\\f$1')
ans = 'C:\Users\focs\S1000\fkehwr.nii'
The original works in both of those cases, because it is constrained by "\.nii" to match names of .nii files only:
regexprep('C:\Users\Docs\S1000\rkehwr.nii','\\r(\w+\.nii)','\\f$1')
ans = 'C:\Users\Docs\S1000\fkehwr.nii'
regexprep('C:\Users\rocs\S1000\rkehwr.nii','\\r(\w+\.nii)','\\f$1')
ans = 'C:\Users\rocs\S1000\fkehwr.nii'
But if you have file names that contain non-alphanumeric/underscore characters, they would be missed:
regexprep('C:\Users\Docs\S1000\rkehwr (Copy).nii','\\r(\w+\.nii)','\\f$1')
ans = 'C:\Users\Docs\S1000\rkehwr (Copy).nii'
regexprep('C:\Users\rocs\S1000\rk#hwr.nii','\\r(\w+\.nii)','\\f$1')
ans = 'C:\Users\rocs\S1000\rk#hwr.nii'
regexprep('C:\rUsers\Docs\S1000\rke=wr.nii','\\r(\w+\.nii)','\\f$1')
ans = 'C:\rUsers\Docs\S1000\rke=wr.nii'
A more general regular expression would be '\\r([^\\]*\.nii)', which will match a backslash followed by a lowercase "r" followed by zero or more non-backslash characters (that's the [^\\]* part) followed by ".nii". This way you're guaranteed to match the name of any .nii file that starts with "r". (Use non-backslash to match any base file name and avoid matching parent directory names.)
To show this regular expression on some of the previous examples:
regexprep('C:\Users\Docs\S1000\rkehwr.nii','\\r([^\\]*\.nii)','\\f$1')
ans = 'C:\Users\Docs\S1000\fkehwr.nii'
regexprep('C:\Users\rocs\S1000\rkehwr.nii','\\r([^\\]*\.nii)','\\f$1')
ans = 'C:\Users\rocs\S1000\fkehwr.nii'
regexprep('C:\Users\rDocs\S1000\rkehwr (Copy).nii','\\r([^\\]*\.nii)','\\f$1')
ans = 'C:\Users\rDocs\S1000\fkehwr (Copy).nii'
"3. I do not manage to move/change the filenames."
Use cellfun since the file names are stored in cell arrays. (And you can omit "'uniform',0" since movefile returns a scalar.)
cellfun(@(OLD,NEW) movefile(OLD, NEW), filenames, newfilenames);
For completeness, the whole code (again, this code operates on all S* folders - do not put this code inside a loop) would be:
SourcePath = 'D:\folder1\folder2\folder3';
dinfo = dir(fullfile(SourcePath, 'S*', 'r*.nii'));
filenames = fullfile( {dinfo.folder}, {dinfo.name} );
newfilenames = regexprep(filenames, '\\r([^\\]*\.nii)', '\\f$1');
cellfun(@(OLD,NEW) movefile(OLD, NEW), filenames, newfilenames);