Rename Data File to Folder Name and Extract The Data into Another Folder

Hello hi, i would like to ask the courtesy of MATLAB community to help me with this query of mine. I have the data in a subfolder of the folder, and i just want the data inside the subfolder alongside the folder (no subfolder, middle folder thing) with the name of subfolder. I tried every approach but i cant seem to get the perfect algorithm.
Example:
BMI (folder) ---> Patient20 (subfolder) ---> Results (data)
** i dont want the middle subfolder, i just need data with the name of subfolder.
** the image attached is the subfolder that i dont want
Thank you.

Answers (1)

Here is a conceptual outline of how this could be accomplished with MATLAB code:
  1. List all subfolders within the main folder.
  2. For each subfolder:
a. List all files.
b. For each file:
i. Construct the new file name based on the subfolder's name.
ii. Move (or copy) the file to the main folder with the new name.
Note:
Before running keep a backup of your orignal folder just in case.
mainFolderPath = 'path_to_main_folder'; % Replace with the path to your main folder
fprintf('Looking in main folder: %s\n', mainFolderPath);
files = dir(mainFolderPath);
dirFlags = [files.isdir] & ~strcmp({files.name}, '.') & ~strcmp({files.name}, '..');
subFolders = files(dirFlags);
for i = 1:length(subFolders)
fprintf('Processing subfolder: %s\n', subFolders(i).name);
subFolderPath = fullfile(mainFolderPath, subFolders(i).name);
dataFiles = dir(fullfile(subFolderPath, '*')); % Replace '*' with specific file type if needed
for j = 1:length(dataFiles)
if ~dataFiles(j).isdir
oldFileName = fullfile(subFolderPath, dataFiles(j).name);
newFileName = fullfile(mainFolderPath, [subFolders(i).name, '_', dataFiles(j).name]);
fprintf('Moving file: %s to %s\n', oldFileName, newFileName);
movefile(oldFileName, newFileName); % Use movefile or copyfile as needed
fprintf('Move complete.\n');
end
end
end
fprintf('All files have been processed.\n');
This script assumes that you want to move the files. If you want to copy them instead, replace movefile with copyfile. Be sure to replace 'path_to_main_folder' with the actual path to your main folder.
Please adjust the script as per your exact folder structure and requirements. If you have a different or more complex pattern for renaming the files, you would need to modify the script accordingly.
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.

2 Comments

My Hierarchy:
Test (folder) ---> Testing_1 (subfolder) ---> TextFile_1.txt (data)
mainFolderPath = 'C:\Users\TUTORIAL\Desktop\Test'; % Replace with the path to your main folder
After running the code snippet:
Test (folder) ---> Testing_1_TextFile_1.txt (data)
Testing_1 (subfolder)

Sign in to comment.

Asked:

on 28 Dec 2023

Edited:

on 28 Dec 2023

Community Treasure Hunt

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

Start Hunting!