Renaming files in a folder

Hello,
I'm written the following code to rename files in a folder. However, for whatever reason it is not working. Any idea where I've gone wrong?
I assume the program is self-explanatory, but let me know if you need any clarifications on the task.
function renameFiles
path=('D:\Documents and Settings\Administrator\Desktop\Data 31052012\75mj\all times\');
files=dir(path)
prefix=('75mJ');
time=0;
for id=3:24;
newName=([prefix num2str(time) 'ns'])
movefile(files(id).name, sprintf('%s.bmp',newName));
time=time+2;
end
Thanks!

3 Comments

Please add some detail about what you mean by "not working". Does the code run to completion, but doesn't do what you expect? Or is it crashing and giving an error message? If the latter, on what line does it fail, and what is the error message?
Your code does not exclude directories in the renaming.
Do not assume that the directories "." and ".." are the first two entries in the list: that is not promised by any of the operating systems that MATLAB runs on.
A
A on 4 Jun 2012
Error is:
Error using movefile
No matching files were found.
Error in renameFiles (line 10)

Sign in to comment.

 Accepted Answer

The directory name is not included in the .name field returned by dir(). You need to add it back on.
movefile( fullfile(path, files(id).name), fullfile(path, sprintf('%s.bmp',newName)) );

9 Comments

A
A on 4 Jun 2012
Hmm, the program creates a new folder with the proper name, and copies all of the files in the original folder into it. What I would like the program to do is iterate through each file in the folder and assign it a new name. How can I fix my code to do this? Thanks for your help!
The code you have now (after my change) should already be renaming. There is also nothing in the code you show that would create a directory.
Please show the current version of your function.
A
A on 4 Jun 2012
The trouble, I believe, is coming from the first two files "." and "..", as well as the final file "thumbs.db". How can I get around this problem?
A
A on 4 Jun 2012
Current code is exactly as above, with your addition. It works well for id>3
After your line
files=dir(path)
add
files([files.isdir]) = [];
That will get rid of all of the directories, including "." and ".." . You would then use
for id = 1 : length(files)
What do you want done for "thumbs.db" ? What file extension do the original files have? What would you like done for any file that has a different extension?
A
A on 5 Jun 2012
Thanks Walter! I iterated through all but the last file number to avoid the thumbs.db file. The rest of the files were .tif
The easier way to do that would be to use
files = dir( fullfile(path, '*.tif') );
I have a similar problem and my code is as shown below. I would like to rename the files in a folder:
if true
% code
endfilePattern = fullfile('D:/intern/Data', '*.nc');
theFiles = dir(filePattern);
for i = 1:length(theFiles)
[PathStr,FileName,FileExt]=fileparts(theFiles(i).name);disp(FileName)
old = FileName(20:59);
new = "";
FileName = replace(FileName,old,new);
FileName = strcat(FileName,FileExt);disp(FileName)
movefile(fullfile(filePattern,theFiles(i).name),fullfile(filePattern,FileName));
end
I am getting an error: Error using movefile No matching files were found. Any suggestions will be helpful.
hi.. Mr.Walter Roberson, i was using the code like this and it also were run but i didn't get the name files as i want. if i wanted to change those file's number output into ONLY 1,2,3,4,5,and so on.. what should i do, please ? FYI, i tried to change the folder name like this..
% Get all files in the current folder clear all;
files = dir(fullfile('C:\Users\ASUS\Desktop\2', '*.tif'));
% Loop through each
for id = 1:length(files)
% Get the file name (minus the extension)
[~, f] = fileparts(files(id).name);
% Convert to number
num = str2double(f);
if ~isnan(num)
% If numeric, rename
APath = fullfile('C:\Users\ASUS\Desktop\2', files(id).name);
movefile(APath, sprintf('%d.tif', num));
end
end
and the output files name were like this :
2.100000e+00.tif
2.100100e+00.tif
2.100200e+00.tif
2.100300e+00.tif
2.100400e+00.tif
2.100500e+00.tif
etc..
or if i want to change the output only like
1a
2a
3a
5b
6b
7b
etc..
could you please give me an idea, pease ?
thank you so much :)

Sign in to comment.

More Answers (0)

Categories

Tags

Asked:

A
A
on 4 Jun 2012

Commented:

on 11 Dec 2018

Community Treasure Hunt

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

Start Hunting!