Read filenames from folder A and rename files from folder B as filenames if folder A

1 view (last 30 days)
Hi, I would like to rename files (more than 600) in a folder. Particularly read the file names from folder A and rename the files from folder B as folder A file-names. I tried to use movefile, but did not work.
for i = 1:Num;
% read filename from folder A
tempA = ReadHis(filesA(i).name); % read his file
fprintf(1,'File read %s\n',filesA(i).name); %Matlab read the files name wise
filenameA = filesA(i).name
% read filename from folder B (old)
tempB = ReadHis(filesB(i).name); % read his file
fprintf(1,'File read %s\n',filesB(i).name); %Matlab read the files name wise
filenameB = filesB(i).name
movefile(filenameB,filenameA);
end
??? Error using ==> movefile No matching files were found.
Any help or suggestion will be appreciable.

Accepted Answer

Jan
Jan on 5 Apr 2013
Edited: Jan on 5 Apr 2013
movefile(source, dest) moves the file from source to dest. The error message means, that the source file is not found.
You did not show or explain it, but it seems like the structs filesA and filesB come from the dir() command. Then note, that the fields name contain the file name only, but not the absolute path with the folder name. Then the file "filesB(i).name" are searched in the currently active folder as replied by cd.
When the filesA are insider C:\Temp\FolderA and for filesB in C\Temp\FolderB, add these names to get absolute files:
pathA = 'C:\Temp\FolderA'
pathB = 'C:\Temp\FolderB'
filesA = dir(fullfile(pathA, '*.ext'));
filesB = dir(fullfile(pathB, '*.ext'));
...
movefile(fullfile(pathB, filenameB), fullfile(pathA, filenameA))
Btw, I did not understand which file should be renamed by which strategy.

More Answers (0)

Categories

Find more on Downloads in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!