How to rename many files efficiently?

3 views (last 30 days)
Yoel Lax
Yoel Lax on 9 Jul 2013
I'm running Matlab 7.5.0 (R2007b) in Unix, and I would like to rename all files in a directory by appending something to the filename, e.g. "A" would become "A_1", "B" would become "B_1" etc.
Note that the original files are not mat-files, but this shouldn't make a difference.
Right now I'm doing this using a loop like this:
for i = 1 : length(FileList)
movefile([MyDir FileList{i}] , [MyDir FileList{i} '_1']);
end
However, this is very slow. For 3500 files it takes around 15 minutes, or about 0.25 seconds per file.
Does anyone know a faster way to do this?
P.S. I've also tried executing a unix command from Matlab within the loop, of the sort '!mv ...', but that wasn't faster either.
  2 Comments
Matt Kindig
Matt Kindig on 9 Jul 2013
Edited: Matt Kindig on 9 Jul 2013
It's not Matlab, but I have used this freeware program (Renamer: http://www.den4b.com/?x=products&product=renamer) for many years to do this. It is rather flexible (like it can run regexp, for example), and works well for this task. It is also rather fast on a large number of files.
Yoel Lax
Yoel Lax on 9 Jul 2013
Thanks but I need to do this from within Matlab, b/c renaming the files is only an intermediate step of a much larger script.

Sign in to comment.

Answers (1)

Azzi Abdelmalek
Azzi Abdelmalek on 9 Jul 2013
system('rename old_name new_name')
  1 Comment
Yoel Lax
Yoel Lax on 9 Jul 2013
How is this different from
!mv oldname newname
Using your solution, I would still need to loop over all filenames, and that's going to be really slow.

Sign in to comment.

Categories

Find more on File Operations in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!