How to rename images from a series of folder

2 views (last 30 days)
Hi All, I worte a code to rename the images in the folders. This is my code.
mainDirectory = 'C:\Users\md\Desktop\NewFolder';
subDirectory = dir([mainDirectory '/N*']);
for m = 1 : length(subDirectory)
a=char('mainDirectory '\' subDirectory(m))');
subFolder = dir(a,'\*.tif');
fileNames = {subFolder.name};
for iFile = 1 : numel( subFolder )
newName = fullfile(subDirectory, sprintf( 'D_1_20%2d.tif',(14-iFile) ) );
movefile( fullfile(subDirectory, fileNames{ iFile }), newName );
end
end
when I run the code, it is saying that too many input arguments. I don't know what I am doing wrong. also there is an error in this line
subFolder = dir(a,'\*.tif');
Thanks in advance.

Accepted Answer

Marta Salas
Marta Salas on 28 Mar 2014
mainDirectory = 'C:\Users\md\Desktop\NewFolder';
subDirectory = dir([mainDirectory '/N*']);
for m = 1 : length(subDirectory)
subFolder = dir(fullfile(mainDirectory, subDirectory(m).name,'*.tif'));
fileNames = {subFolder.name};
for iFile = 1 : numel( subFolder )
newName = fullfile(mainDirectory, subDirectory(m).name, sprintf( 'D_1_20%2d.tif',(14-iFile) ) );
movefile( fullfile(mainDirectory, subDirectory(m).name, fileNames{ iFile }), newName );
end
end
  3 Comments
Le Dung
Le Dung on 24 Dec 2018
what is the meaning of '/N*' in the command line:
subDirectory = dir([mainDirectory '/N*']);
When i use the cmmand line, matlab return:
0 x 1 struc with 5 fields
but, when i dont use '/N*', matlab return:
11x1 struc with 5 fields
Is '/N*' for folders and not take into account is for files?
Could you explain to me ?
Walter Roberson
Walter Roberson on 24 Dec 2018
The original poster in 2014, md, happened to be interested in only the subfolders whose names began with the letter N. It is not some kind of special command switch: it is just part of the name.
Another way that it could have been coded would be
subDirectory = dir( fullfile(mainDirectory, 'N*') );
You might have been misled by the fact that a / was used when you expected to see \ for directory names. It turns out that internally, MS DOS has always used / as the directory separator, but that way way back for compatibility with CP/M command line structure that used / as the way to indicate command options, the shell used \ to indicate directory boundaries. But that was only for presentation purposes, and internally it uses / . Therefore in MS Windows, 'C:\Users\md\Desktop\NewFolder' is the same as 'C:/Users/md/Desktop/NewFolder'

Sign in to comment.

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!