Split File paths by '\' but file paths have varying subfolders and thus lengths

8 views (last 30 days)
the aim of this function is to read and display all the subfolders from a given filepath (and subsequent sub folders within those sub folders and so on). I have gotten this to work and i get an X by 1 cell of all the filepaths to every subfolder.
fileList = getAllFiles('C:\Users\first.surname\Downloads\SA123 - Test Script Project');
splitlist = {};
for i =1:length(fileList)
splitlist = [splitlist ;split(fileList{i,1},"\",2)]
end
function fileList = getAllFiles(dirName)
dirData = dir(dirName); %# Get the data for the current directory
dirIndex = [dirData.isdir]; %# Find the index for directories
fileList = {dirData(dirIndex).name}'; %'# Get a list of the files
fileList(ismember(fileList,{'.','..'})) = [];
if ~isempty(fileList)
fileList = cellfun(@(x) fullfile(dirName,x),... %# Prepend path to files
fileList,'UniformOutput',false);
end
subDirs = {dirData(dirIndex).name}; %# Get a list of the subdirectories
validIndex = ~ismember(subDirs,{'.','..'}); %# Find index of subdirectories
%# that are not '.' or '..'
for iDir = find(validIndex) %# Loop over valid subdirectories
nextDir = fullfile(dirName,subDirs{iDir}); %# Get the subdirectory path
fileList = [fileList; getAllFiles(nextDir)]; %# Recursively call getAllFiles
end
end
the issue i pose when using either
fileparts()
or
split()
is that due to some subfolders having children and some not, the dimension of the array is never the same andit fails after the iteration of the subfolders in ths folder only
an example of the folder structure is
dirName\SubFolder1\ChildFolder1\
Invalid expression. Check for missing or extra characters.
dirName\SubFolder1\ChildFolder2\
dirName\SubFolder2\
dirName\SubFolder3\ChildFolder1\ChildFolder1\
i'm not sure how to overcome this (possible to force the dimension of the array and pad the empties with "" cells? maybe)
FileList
splitList
  2 Comments
Jonas
Jonas on 22 Feb 2024
do all subfolders contain at least one file? then you could use subdir from FEX for getting all files from a specific direction and below and get the folder entries of all files

Sign in to comment.

Accepted Answer

Stephen23
Stephen23 on 22 Feb 2024
Edited: Stephen23 on 22 Feb 2024
Your code is very complex. You can simplify it by letting DIR recursively parse the folder structure.
Lets try it here:
mkdir Name/SubFolder1/ChildFolder1
mkdir Name/SubFolder1/ChildFolder2
mkdir Name/SubFolder2
mkdir Name/SubFolder3/ChildFolder1/ChildFolder1
P = './Name'; % absolute or relative path to the parent folder
C = {};
S = dir(fullfile(P,'**','*')); % recursive folder search
S(strncmp({S.name},'.',1)) = [];
for k = 1:numel(S)
V = regexp(S(k).folder,'[^/\\]+','match');
V{end+1} = S(k).name;
C(k,1:numel(V)) = V;
end
C(cellfun(@isempty,C)) = {''}; % optional
display(C)
C = 7×6 cell array
{'users'} {'mss.system.cFaraB'} {'Name'} {'SubFolder1'} {0×0 char } {0×0 char } {'users'} {'mss.system.cFaraB'} {'Name'} {'SubFolder2'} {0×0 char } {0×0 char } {'users'} {'mss.system.cFaraB'} {'Name'} {'SubFolder3'} {0×0 char } {0×0 char } {'users'} {'mss.system.cFaraB'} {'Name'} {'SubFolder1'} {'ChildFolder1'} {0×0 char } {'users'} {'mss.system.cFaraB'} {'Name'} {'SubFolder1'} {'ChildFolder2'} {0×0 char } {'users'} {'mss.system.cFaraB'} {'Name'} {'SubFolder3'} {'ChildFolder1'} {0×0 char } {'users'} {'mss.system.cFaraB'} {'Name'} {'SubFolder3'} {'ChildFolder1'} {'ChildFolder1'}

More Answers (0)

Categories

Find more on File Operations in Help Center and File Exchange

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!