Rename files within multiple subfolders (within multiple subfolders) to the name of the first subfolder.

12 views (last 30 days)
Hi!
I am new to matlab and have a parentfolder with multiple subfolders with names such as C1_sub1_I, C2_sub2_M etc. Within each subfolder I have another subfolder with some files. I want to extract log files from each subfolder (within each subfolder) and rename them to the name of the first subfolder (C1_sub1_I, C2_sub2_M etc).
So the levels are: parentfolder -> subfolder with the name I want -> another folder -> my files.
I have a script to read the log files but I just cant figure out how to rename them after the first subfolder, or if its possible to delete the second subfile first and then rename them.
This is my script to load the log files using a function (importPresentationLog), which works fine:
%import files:
dir_path = fullfile('C:\Users\johan\Skrivbord\test');
mydir=dir(fullfile(dir_path,'**/*middle.log'));
for i = 1:numel(mydir);
currentFile = fullfile(mydir(i).folder,mydir(i).name);
[mydir(i).name, out1] = importPresentationLog(currentFile);
end
Would very much appreciate some help! Thank you in advance!!
Maria

Accepted Answer

Guillaume
Guillaume on 5 Jul 2019
Edited: Guillaume on 5 Jul 2019
You just need to break down the mydir.folder into its different folders and use the one before last to rename your file.:
dir_path = fullfile('C:\Users\johan\Skrivbord\test');
mydir = dir(fullfile(dir_path, '**/*middle.log'));
for i = 1:numel(mydir);
folders = strsplit(mydir(i).folder, filesep); %split path into individual folders
newname = [folders{end-1}, '.log'];
movefile(fullfile(mydir(i).folder, mydir(i).name), newname)); %rename file
end

More Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 5 Jul 2019
Why not do something like:
target_dir = fullfile('C:\Users\johan\Skrivbord\test');
source_root = fullfile('C:\Users\johan\Skrivbord\test');
cd(source_root)
dFiles = dir('.');
for iFile = 1:numel(dFiles),
if your_file_matching_condition
target_name = % if you want some specific format of the file to copy to
movefile(dFiles(iFile).name,fullfile(target_dir,target_name))
elseif isdir(dFiles(iFile))
cd(dFiles(iFile).name)
dF2 = dir('.');
for iF2 = 1:numel(dF2),
if your_file_matching_condition
target_name = % if you want some specific format of the file to copy to
movefile(dF2(iF2).name,fullfile(target_dir,target_name))
elseif isdir(dF2(iF2))
% Well you might put this into a function where you recursively
% walk down into directories, and copy the files you want.
end
end
cd('..')
end
end
That's a bit clunky to do by yourself, there are a couple of recursive dir-functions at the file exchange: Recursive-dir
A couple of those, (dir2, dirrec, recdir?) have the capability to give you files matching a regular-expression-pattern with path and all - that would be much easier to use, call, lets say dir2 loop over the output and copy/move the matching files one by one.
This is also a task best suited for unix find, (and something similar exist in MS-powertools?):
find ./ -name \*middle.log -print0 | xargs -0 cp -p /Users/johan/Skrivbord/test
HTH

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2019a

Community Treasure Hunt

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

Start Hunting!