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

8 visualizaciones (últimos 30 días)
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 comentarios
Jonas
Jonas el 22 de Feb. de 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
Ali
Ali el 22 de Feb. de 2024
@Jonas negative some may ne empty some may have files some may have more folder

Iniciar sesión para comentar.

Respuesta aceptada

Stephen23
Stephen23 el 22 de Feb. de 2024
Editada: Stephen23 el 22 de Feb. de 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'}
  2 comentarios
Stephen23
Stephen23 el 22 de Feb. de 2024
PS: if you also want to exclude files from the output, perhaps like this:
S(strncmp({S.name},'.',1)|~[S.isdir]) = [];

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre File Operations en Help Center y File Exchange.

Productos


Versión

R2022b

Community Treasure Hunt

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

Start Hunting!

Translated by