fileparts() and ignoring the first output gives you only the basic file name with no directory and no file extension. When you movefile() specifying only that basic file name, you are not telling it which directory to look in, so it would have no chance of finding the files unless your uigetdir() happened to select the current directory. And even if you did happen to be working with the current directory, the fact that you discarded the file extension is a problem.
You are also not naming a destination directory, which again is important because you do not want to move them into the current directory.
There is also a risk because your file names might already include numbered files.
selpath = uigetdir;
outdir = fullfile(selpath, 'renamed');
num_in_out = length( dir( fullfile(outdir, '*.jpg')) );
if ~exist(outdir, 'dir'); mkdir(outdir); end
imagefiles = dir(fullfile(selpath, '*.jpg'));
filenames = fullfile(selpath, {imagefiles.name});
for id = 1:length(filenames)
thisfile = filenames{id};
outfile = fullfile(outdir, sprintf('%d.jpg', id+num_in_out));
movefile(thisfile, outfile);
end
This is designed to be able to resume if it is interrupted: it counts the number of files already in the output directory and continues numbering from there.
Best Answer