How to move files to one level up and append the name of the folder in which they are contained?

10 views (last 30 days)
Hello,
My folders and files are arranged as follows:
myFolder/0.0001/zNormal.vtp
myFolder/0.0002/zNormal.vtp
myFolder/0.0003/zNormal.vtp
myFolder/0.0004/zNormal.vtp
myFolder/0.0005/zNormal.vtp
myFolder/0.0006/zNormal.vtp
myFolder/0.0007/zNormal.vtp
myFolder/0.0008/zNormal.vtp
myFolder/0.0009/zNormal.vtp
myFolder/0.001/zNormal.vtp
I'd like to move all the 'zNormal.vtp' files to one level up and append the name of the folders in which they are contained.
Example:
myFolder/0.0001/zNormal.vtp --> myFolder/zNormal_0.0001.vtp.
So the 0.0001 folder would now remain empty
Any help would be much appreciated

Answers (1)

Jan
Jan on 24 Jan 2021
BaseFolder = 'myFolder';
FileList = dir(fullfile(BaseFolder, '**', '*.vtp'));
for iFile = 1:numel(FileList)
File = FileList(iFile);
oldFile = fullfile(File.folder, File.name);
newFile = fullfile(BaseFolder, [File.Folder, '_', File.name]);
movefile(oldFile, newFile);
end

Categories

Find more on Automotive 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!