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

6 次查看(过去 30 天)
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.

采纳的回答

Jan
Jan 2013-4-5
编辑:Jan 2013-4-5
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.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 File Operations 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by