Renaming a lot of files

Hello,
I've made a 1000 copies of a file in the current directory. For example
RW.a_process
Copy of RW.a_process
Copy (2) of RW.a_process.....
Copy (1000) of RW.a_process
Would anybody know how I could rename them to be
RW.a_process
RW1.a_process
RW2.a_process....
RW1000.a_process
Many thanks
John

1 Comment

Jan
Jan on 18 Mar 2012
It would be *much* easier to create the file with the correct file name from the beginning. Considering "RW", "Copy of RW" and "Copy (i) of RW" is possible, but not necessary.

Sign in to comment.

 Accepted Answer

Sahyadri Vibhu
Sahyadri Vibhu on 20 Mar 2012

0 votes

You could use http://www.bulkrenameutility.co.uk/Main_Intro.php if you are using WIndows or something similar if you are using other OS

More Answers (2)

These functions may come in helpful:
dir() to get the existing filenames.
strfind() to find parentheses.
sscanf() or str2double() to extract the number (optional since you can work with the extracted string directly).
sprintf() to create the new name.
movefile() to make a copy with the new name.
delete() to delete the file with the old name.

4 Comments

John
John on 18 Mar 2012
hello,
This is what I've come up with so far. Would you know how I to save files incrementally?
Many thanks
% Get all .a_process files in the current folder
files = dir('*.a_process');
% Loop through each
for id = 1:length(files)
%save as RW1.a_process...RW2.a_process
end
http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
John
John on 18 Mar 2012
Hello,
I kinda have it working, except it is saving it as .mat files? How would I keep the original file extension?
Thank you
% Get all .a_process files in the current folder
BaseName='RW';
files = dir('*.a_process');
% Loop through each
for k = 1:length(files)
FileName=[BaseName,num2str(k)]
save(Filename)
end
Not very robust at all. You will encounter problems with that code. I suggest you look at my code.

Sign in to comment.

Allright, since you at least took a shot at it, here's the code:
files = {'RW.a_process',...
'Copy of RW.a_process',...
'Copy (2) of RW.a_process',...
'Copy (1000) of RW.a_process'}
for k = 1 : length(files)
oldFileName = files{k}
leftParenthesisLocation = strfind(oldFileName, 'Copy (');
if leftParenthesisLocation >= 1
% Handle cases of Copy (nnn) of RW.a_process
rightParenthesisLocation = strfind(oldFileName(leftParenthesisLocation:end), ')');
if rightParenthesisLocation > 1
strNumber = oldFileName(leftParenthesisLocation+6:rightParenthesisLocation-1)
% Get name to the right of the right parenthesis.
newFileName = oldFileName(rightParenthesisLocation+5:end);
[folder, baseFileName, ext] = fileparts(newFileName);
newFileName = sprintf('%s%s%s', baseFileName, strNumber, ext);
fprintf('New Filename = %s\n', newFileName); % Print blank line.
end
else
% Handle case of Copy of RW.a_process
leftLocation = strfind(oldFileName, 'Copy of ');
if ~isempty(leftLocation)
% Get name to the right of the "Copy of ".
[folder, baseFileName, ext] = fileparts(oldFileName(leftLocation+8:end));
strNumber = 1;
newFileName = sprintf('%s%d%s', baseFileName, strNumber, ext);
fprintf('New Filename = %s\n', newFileName); % Print blank line.
end
end
end
I think you can figure out where to put the movefile(oldFileName, newFileName) and delete(oldFileName) in. Make sure you call exist(newFileName, 'file') before you delete the old one otherwise you may blow away your only copy!

6 Comments

John
John on 19 Mar 2012
Hello,
Thank you for your help.
I'm still struggling to get it working. Do I have to type all the "Copy (n) of RW.a_process" file names at the start of the code?
I tried stepping through the code to determine where to put movefile(oldFileName, newFileName). Do I put it in twice after where it prints a blank line? because a newFileName has been determined.
Thank you
No. that was just for the example. You can use dir(). See the FAQ: http://matlab.wikia.com/wiki/FAQ#How_can_I_process_a_sequence_of_files.3F
John
John on 19 Mar 2012
Do you mean
files = dir('*.a_process');
It giving me an error:
??? Cell contents reference from a non-cell array object.
Error in ==> rename at 3
oldFileName = files{k}
Thank you
John
John on 19 Mar 2012
Sorry, I can I ask you more more question please?
If I'm using this code on file with no file extensions, does it need to change, for example
Copy (2) of RW
Copy (3) of RW
I've stepped through the code line by line and it seems to be working fine except when it gets to movefile(oldFileName, newFileName) it gives an error
??? Error using ==> movefile
No matching files were found.
Error in ==> rename at 14
movefile(oldFileName, newFileName)
files = {'Copy (2) of RW'}
for k = 1 : length(files)
oldFileName = files{k}
leftParenthesisLocation = strfind(oldFileName, 'Copy (');
if leftParenthesisLocation >= 1
% Handle cases of Copy (nnn) of RW.a_cycle
rightParenthesisLocation = strfind(oldFileName(leftParenthesisLocation:end), ')');
if rightParenthesisLocation > 1
strNumber = oldFileName(leftParenthesisLocation+6:rightParenthesisLocation-1)
% Get name to the right of the right parenthesis.
newFileName = oldFileName(rightParenthesisLocation+5:end);
[folder, baseFileName, ext] = fileparts(newFileName);
newFileName = sprintf('%s%s%s', baseFileName, strNumber, ext);
fprintf('New Filename = %s\n', newFileName); % Print blank line.
movefile(oldFileName, newFileName)
end
end
end
John
John on 19 Mar 2012
Hello Image Analyst,
Sorry for bothering you again. If you have time to answer my questions quickly that would be great. It's nearly working just not quite there.
Thank you
John
Did you include the file extension? It probably has one, even though you may be asking windows to hide it from you. I always turn off that windows option - I like knowing what extensions the files have, especially when you have files with the same base filename but different extensions. Try files = {'Copy (2) of RW.dat'} or whatever it is.

Sign in to comment.

Categories

Tags

Community Treasure Hunt

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

Start Hunting!